@constructor-io/constructorio-ui-quizzes 1.9.0 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constructorio-ui-quizzes-bundled.js +19 -19
- package/lib/cjs/package.json +1 -1
- package/lib/cjs/src/components/ResultFavoritesButton/ResultFavoritesButton.js +2 -2
- package/lib/cjs/src/components/SkipButton/SkipButton.js +1 -3
- package/lib/cjs/src/constants.js +8 -1
- package/lib/cjs/src/hooks/usePropsGetters/index.js +6 -6
- package/lib/cjs/src/hooks/usePropsGetters/useAddToFavoritesButtonProps.js +10 -18
- package/lib/cjs/src/hooks/useQuiz.js +2 -2
- package/lib/cjs/src/utils.js +9 -1
- package/lib/mjs/package.json +1 -1
- package/lib/mjs/src/components/ResultFavoritesButton/ResultFavoritesButton.js +2 -2
- package/lib/mjs/src/components/SkipButton/SkipButton.js +1 -3
- package/lib/mjs/src/constants.js +7 -0
- package/lib/mjs/src/hooks/usePropsGetters/index.js +6 -6
- package/lib/mjs/src/hooks/usePropsGetters/useAddToFavoritesButtonProps.js +6 -10
- package/lib/mjs/src/hooks/useQuiz.js +2 -2
- package/lib/mjs/src/utils.js +9 -1
- package/lib/styles.css +12 -2
- package/lib/types/src/constants.d.ts +1 -0
- package/lib/types/src/hooks/usePropsGetters/index.d.ts +1 -1
- package/lib/types/src/hooks/usePropsGetters/useAddToFavoritesButtonProps.d.ts +1 -1
- package/lib/types/src/types.d.ts +4 -3
- package/package.json +1 -1
package/lib/cjs/package.json
CHANGED
|
@@ -10,8 +10,8 @@ function ResultFavoritesButton(props) {
|
|
|
10
10
|
return (
|
|
11
11
|
// eslint-disable-next-line react/button-has-type
|
|
12
12
|
react_1.default.createElement("button", Object.assign({}, getAddToFavoritesButtonProps(item, price)),
|
|
13
|
-
react_1.default.createElement("svg", {
|
|
14
|
-
react_1.default.createElement("path", { d: '
|
|
13
|
+
react_1.default.createElement("svg", { width: '21', height: '21', viewBox: '0 0 24 24', strokeWidth: '2', strokeLinecap: 'round', strokeLinejoin: 'round' },
|
|
14
|
+
react_1.default.createElement("path", { d: 'M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z' }))));
|
|
15
15
|
}
|
|
16
16
|
return null;
|
|
17
17
|
}
|
|
@@ -6,8 +6,6 @@ function SkipButton(props) {
|
|
|
6
6
|
const { propsGetters, className = 'cio-button-container' } = props, rest = tslib_1.__rest(props, ["propsGetters", "className"]);
|
|
7
7
|
return (react_1.default.createElement("div", { className: `${className || ''}` }, propsGetters && (
|
|
8
8
|
// eslint-disable-next-line react/button-has-type
|
|
9
|
-
react_1.default.createElement("button", Object.assign({}, rest, propsGetters()),
|
|
10
|
-
"Skip",
|
|
11
|
-
react_1.default.createElement("span", null, " question")))));
|
|
9
|
+
react_1.default.createElement("button", Object.assign({}, rest, propsGetters()), "Skip question"))));
|
|
12
10
|
}
|
|
13
11
|
exports.default = SkipButton;
|
package/lib/cjs/src/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RequestStates = exports.callbacksDescription = exports.changePrimaryColorDescription = exports.smallContainerDescription = exports.cioJsClientDescription = exports.basicDescription = exports.hookDescription = exports.componentDescription = exports.quizSessionStateKey = exports.quizId = exports.apiKey = void 0;
|
|
3
|
+
exports.RequestStates = exports.favoritesDescription = exports.callbacksDescription = exports.changePrimaryColorDescription = exports.smallContainerDescription = exports.cioJsClientDescription = exports.basicDescription = exports.hookDescription = exports.componentDescription = exports.quizSessionStateKey = exports.quizId = exports.apiKey = void 0;
|
|
4
4
|
// Autocomplete key index
|
|
5
5
|
exports.apiKey = 'key_wJSdZSiesX5hiVLt';
|
|
6
6
|
exports.quizId = 'coffee-quiz';
|
|
@@ -92,6 +92,13 @@ In the example below, the \`primaryColor\` prop has been used to override this c
|
|
|
92
92
|
> Advanced Option: Instead of passing a primaryColor prop, you can also override \`--primary-color-h\`, \`--primary-color-s\`, and \`--primary-color-l\` CSS variables within a \`.cio-quiz\` container element. If explicitly given a value in your CSS, then the values of these variables will be used as the HSL values for your quiz.
|
|
93
93
|
`;
|
|
94
94
|
exports.callbacksDescription = `Pass an \`apiKey\`, a \`quizId\`, and \`callbacks\``;
|
|
95
|
+
exports.favoritesDescription = `
|
|
96
|
+
Add \`const [favorites, setFavorites] = useState([]);\` or equivalent to manage the favorite items' state.
|
|
97
|
+
|
|
98
|
+
Pass favorites as an array to \`favoriteItems\` in \`resultsPageOptions\` as a prop to CioQuiz.
|
|
99
|
+
|
|
100
|
+
Pass a callback function to \`onAddToFavoritesClick\` in \`resultsPageOptions\` as a prop to CioQuiz to handle favorites state changes to update \`favorites\` array.
|
|
101
|
+
`;
|
|
95
102
|
var RequestStates;
|
|
96
103
|
(function (RequestStates) {
|
|
97
104
|
RequestStates["Stale"] = "STALE";
|
|
@@ -9,7 +9,7 @@ const useNextQuestionButtonProps_1 = tslib_1.__importDefault(require("./useNextQ
|
|
|
9
9
|
const usePreviousQuestionButtonProps_1 = tslib_1.__importDefault(require("./usePreviousQuestionButtonProps"));
|
|
10
10
|
const useAddToFavoritesButtonProps_1 = tslib_1.__importDefault(require("./useAddToFavoritesButtonProps"));
|
|
11
11
|
const useSkipQuestionButtonProps_1 = tslib_1.__importDefault(require("./useSkipQuestionButtonProps"));
|
|
12
|
-
const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
|
|
12
|
+
const usePropsGetters = (quizEvents, quizApiState, quizLocalState, favoriteItems) => {
|
|
13
13
|
var _a, _b, _c;
|
|
14
14
|
const { quizAnswerChanged, nextQuestion, skipQuestion, previousQuestion, resetQuiz, hydrateQuiz, addToCart, addToFavorites, resultClick, } = quizEvents;
|
|
15
15
|
const getOpenTextInputProps = (0, useOpenTextInputProps_1.default)(quizAnswerChanged, nextQuestion, (_a = quizApiState.quizCurrentQuestion) === null || _a === void 0 ? void 0 : _a.next_question, quizLocalState.answerInputs);
|
|
@@ -33,7 +33,7 @@ const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
|
|
|
33
33
|
type: 'button',
|
|
34
34
|
onClick: (e) => addToCart(e, result, price),
|
|
35
35
|
}), [addToCart]);
|
|
36
|
-
const getAddToFavoritesButtonProps = (0, useAddToFavoritesButtonProps_1.default)(addToFavorites);
|
|
36
|
+
const getAddToFavoritesButtonProps = (0, useAddToFavoritesButtonProps_1.default)(addToFavorites, favoriteItems);
|
|
37
37
|
const quizResultClickDown = (0, react_1.useCallback)((event, result, position) => {
|
|
38
38
|
if ((event === null || event === void 0 ? void 0 : event.key) === ' ' || (event === null || event === void 0 ? void 0 : event.key) === 'Enter') {
|
|
39
39
|
if (resultClick) {
|
|
@@ -59,16 +59,16 @@ const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
|
|
|
59
59
|
const getQuizImageProps = (0, react_1.useCallback)(() => {
|
|
60
60
|
var _a, _b, _c, _d, _e, _f;
|
|
61
61
|
return ({
|
|
62
|
-
src: (_c = (_b = (_a = quizApiState.quizCurrentQuestion) === null || _a === void 0 ? void 0 : _a.next_question) === null || _b === void 0 ? void 0 : _b.images) === null || _c === void 0 ? void 0 : _c.primary_url,
|
|
63
|
-
alt: (_f = (_e = (_d = quizApiState.quizCurrentQuestion) === null || _d === void 0 ? void 0 : _d.next_question) === null || _e === void 0 ? void 0 : _e.images) === null || _f === void 0 ? void 0 : _f.primary_alt,
|
|
62
|
+
src: ((_c = (_b = (_a = quizApiState.quizCurrentQuestion) === null || _a === void 0 ? void 0 : _a.next_question) === null || _b === void 0 ? void 0 : _b.images) === null || _c === void 0 ? void 0 : _c.primary_url) || undefined,
|
|
63
|
+
alt: ((_f = (_e = (_d = quizApiState.quizCurrentQuestion) === null || _d === void 0 ? void 0 : _d.next_question) === null || _e === void 0 ? void 0 : _e.images) === null || _f === void 0 ? void 0 : _f.primary_alt) || undefined,
|
|
64
64
|
});
|
|
65
65
|
}, [quizApiState.quizCurrentQuestion]);
|
|
66
66
|
const getSelectQuestionImageProps = (0, react_1.useCallback)((option) => {
|
|
67
67
|
var _a, _b;
|
|
68
68
|
return ({
|
|
69
69
|
className: 'cio-question-option-image',
|
|
70
|
-
src: (_a = option === null || option === void 0 ? void 0 : option.images) === null || _a === void 0 ? void 0 : _a.primary_url,
|
|
71
|
-
alt: (_b = option === null || option === void 0 ? void 0 : option.images) === null || _b === void 0 ? void 0 : _b.primary_alt,
|
|
70
|
+
src: ((_a = option === null || option === void 0 ? void 0 : option.images) === null || _a === void 0 ? void 0 : _a.primary_url) || undefined,
|
|
71
|
+
alt: ((_b = option === null || option === void 0 ? void 0 : option.images) === null || _b === void 0 ? void 0 : _b.primary_alt) || undefined,
|
|
72
72
|
});
|
|
73
73
|
}, []);
|
|
74
74
|
return {
|
|
@@ -1,25 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const react_1 = require("react");
|
|
4
|
-
function useAddToFavoritesButtonProps(addToFavorites) {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
onClick: (e) => {
|
|
14
|
-
var _a, _b;
|
|
15
|
-
addToFavorites(e, result, price, !(favorited === null || favorited === void 0 ? void 0 : favorited[((_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.id) || '0']));
|
|
16
|
-
toggleIdFavorited(((_b = result === null || result === void 0 ? void 0 : result.data) === null || _b === void 0 ? void 0 : _b.id) || '0');
|
|
17
|
-
},
|
|
18
|
-
type: 'button',
|
|
19
|
-
});
|
|
20
|
-
},
|
|
4
|
+
function useAddToFavoritesButtonProps(addToFavorites, favoriteItems) {
|
|
5
|
+
const isResultFavorited = (result) => { var _a, _b; return ((_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.id) && (favoriteItems === null || favoriteItems === void 0 ? void 0 : favoriteItems.includes((_b = result === null || result === void 0 ? void 0 : result.data) === null || _b === void 0 ? void 0 : _b.id)); };
|
|
6
|
+
const getAddToFavoritesButtonProps = (0, react_1.useCallback)((result, price) => ({
|
|
7
|
+
className: `${'cio-result-card-favorites-button'} ${isResultFavorited(result) ? 'favorited' : ''}`,
|
|
8
|
+
onClick: (e) => {
|
|
9
|
+
addToFavorites(e, result, price, !isResultFavorited(result));
|
|
10
|
+
},
|
|
11
|
+
type: 'button',
|
|
12
|
+
}),
|
|
21
13
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
22
|
-
[
|
|
14
|
+
[addToFavorites]);
|
|
23
15
|
return getAddToFavoritesButtonProps;
|
|
24
16
|
}
|
|
25
17
|
exports.default = useAddToFavoritesButtonProps;
|
|
@@ -9,7 +9,7 @@ const usePrimaryColorStyles_1 = tslib_1.__importDefault(require("./usePrimaryCol
|
|
|
9
9
|
const useQuizEvents_1 = tslib_1.__importDefault(require("./useQuizEvents"));
|
|
10
10
|
const useQuizState_1 = tslib_1.__importDefault(require("./useQuizState"));
|
|
11
11
|
const useQuiz = (quizOptions) => {
|
|
12
|
-
const { apiKey, cioJsClient, primaryColor } = quizOptions;
|
|
12
|
+
const { apiKey, cioJsClient, primaryColor, resultsPageOptions: { favoriteItems }, } = quizOptions;
|
|
13
13
|
// Log console errors for required parameters quizId and resultsPageOptions
|
|
14
14
|
(0, useConsoleErrors_1.default)(quizOptions);
|
|
15
15
|
// Quiz Cio Client
|
|
@@ -21,7 +21,7 @@ const useQuiz = (quizOptions) => {
|
|
|
21
21
|
// Props getters
|
|
22
22
|
const { quizApiState, quizLocalState, quizSessionStorageState } = quizState;
|
|
23
23
|
const { skipToResults } = quizSessionStorageState;
|
|
24
|
-
const propGetters = (0, usePropsGetters_1.default)(quizEvents, quizApiState, quizLocalState);
|
|
24
|
+
const propGetters = (0, usePropsGetters_1.default)(quizEvents, quizApiState, quizLocalState, favoriteItems);
|
|
25
25
|
const primaryColorStyles = (0, usePrimaryColorStyles_1.default)(primaryColor);
|
|
26
26
|
(0, react_1.useEffect)(() => {
|
|
27
27
|
if (skipToResults)
|
package/lib/cjs/src/utils.js
CHANGED
|
@@ -45,7 +45,15 @@ exports.getStoryParams = getStoryParams;
|
|
|
45
45
|
exports.functionStrings = {
|
|
46
46
|
onAddToCartClick: `(item) => console.dir(item)`,
|
|
47
47
|
onQuizResultClick: `(result, position) => console.dir(result, position)`,
|
|
48
|
-
onAddToFavoritesClick: `(
|
|
48
|
+
onAddToFavoritesClick: `(result) => {
|
|
49
|
+
if (result.data) {
|
|
50
|
+
if (!favorites.includes(result.data.id)) {
|
|
51
|
+
setFavorites([...favorites, result.data.id]);
|
|
52
|
+
} else {
|
|
53
|
+
setFavorites(favorites.filter((id) => id !== result.data?.id));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}`,
|
|
49
57
|
onQuizResultsLoaded: `(results) => console.dir(results)`,
|
|
50
58
|
onQuizNextQuestion: `(question) => console.dir(question)`,
|
|
51
59
|
onQuizSkipQuestion: `(question) => console.dir(question)`,
|
package/lib/mjs/package.json
CHANGED
|
@@ -7,8 +7,8 @@ export default function ResultFavoritesButton(props) {
|
|
|
7
7
|
return (
|
|
8
8
|
// eslint-disable-next-line react/button-has-type
|
|
9
9
|
React.createElement("button", { ...getAddToFavoritesButtonProps(item, price) },
|
|
10
|
-
React.createElement("svg", {
|
|
11
|
-
React.createElement("path", { d: '
|
|
10
|
+
React.createElement("svg", { width: '21', height: '21', viewBox: '0 0 24 24', strokeWidth: '2', strokeLinecap: 'round', strokeLinejoin: 'round' },
|
|
11
|
+
React.createElement("path", { d: 'M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z' }))));
|
|
12
12
|
}
|
|
13
13
|
return null;
|
|
14
14
|
}
|
|
@@ -3,8 +3,6 @@ function SkipButton(props) {
|
|
|
3
3
|
const { propsGetters, className = 'cio-button-container', ...rest } = props;
|
|
4
4
|
return (React.createElement("div", { className: `${className || ''}` }, propsGetters && (
|
|
5
5
|
// eslint-disable-next-line react/button-has-type
|
|
6
|
-
React.createElement("button", { ...rest, ...propsGetters() },
|
|
7
|
-
"Skip",
|
|
8
|
-
React.createElement("span", null, " question")))));
|
|
6
|
+
React.createElement("button", { ...rest, ...propsGetters() }, "Skip question"))));
|
|
9
7
|
}
|
|
10
8
|
export default SkipButton;
|
package/lib/mjs/src/constants.js
CHANGED
|
@@ -89,6 +89,13 @@ In the example below, the \`primaryColor\` prop has been used to override this c
|
|
|
89
89
|
> Advanced Option: Instead of passing a primaryColor prop, you can also override \`--primary-color-h\`, \`--primary-color-s\`, and \`--primary-color-l\` CSS variables within a \`.cio-quiz\` container element. If explicitly given a value in your CSS, then the values of these variables will be used as the HSL values for your quiz.
|
|
90
90
|
`;
|
|
91
91
|
export const callbacksDescription = `Pass an \`apiKey\`, a \`quizId\`, and \`callbacks\``;
|
|
92
|
+
export const favoritesDescription = `
|
|
93
|
+
Add \`const [favorites, setFavorites] = useState([]);\` or equivalent to manage the favorite items' state.
|
|
94
|
+
|
|
95
|
+
Pass favorites as an array to \`favoriteItems\` in \`resultsPageOptions\` as a prop to CioQuiz.
|
|
96
|
+
|
|
97
|
+
Pass a callback function to \`onAddToFavoritesClick\` in \`resultsPageOptions\` as a prop to CioQuiz to handle favorites state changes to update \`favorites\` array.
|
|
98
|
+
`;
|
|
92
99
|
export var RequestStates;
|
|
93
100
|
(function (RequestStates) {
|
|
94
101
|
RequestStates["Stale"] = "STALE";
|
|
@@ -6,7 +6,7 @@ import useNextQuestionButtonProps from './useNextQuestionButtonProps';
|
|
|
6
6
|
import usePreviousQuestionButtonProps from './usePreviousQuestionButtonProps';
|
|
7
7
|
import useAddToFavoritesButtonProps from './useAddToFavoritesButtonProps';
|
|
8
8
|
import useSkipQuestionButtonProps from './useSkipQuestionButtonProps';
|
|
9
|
-
const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
|
|
9
|
+
const usePropsGetters = (quizEvents, quizApiState, quizLocalState, favoriteItems) => {
|
|
10
10
|
const { quizAnswerChanged, nextQuestion, skipQuestion, previousQuestion, resetQuiz, hydrateQuiz, addToCart, addToFavorites, resultClick, } = quizEvents;
|
|
11
11
|
const getOpenTextInputProps = useOpenTextInputProps(quizAnswerChanged, nextQuestion, quizApiState.quizCurrentQuestion?.next_question, quizLocalState.answerInputs);
|
|
12
12
|
const getCoverQuestionProps = useCoverQuestionProps(quizAnswerChanged, quizApiState.quizCurrentQuestion?.next_question);
|
|
@@ -29,7 +29,7 @@ const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
|
|
|
29
29
|
type: 'button',
|
|
30
30
|
onClick: (e) => addToCart(e, result, price),
|
|
31
31
|
}), [addToCart]);
|
|
32
|
-
const getAddToFavoritesButtonProps = useAddToFavoritesButtonProps(addToFavorites);
|
|
32
|
+
const getAddToFavoritesButtonProps = useAddToFavoritesButtonProps(addToFavorites, favoriteItems);
|
|
33
33
|
const quizResultClickDown = useCallback((event, result, position) => {
|
|
34
34
|
if (event?.key === ' ' || event?.key === 'Enter') {
|
|
35
35
|
if (resultClick) {
|
|
@@ -50,13 +50,13 @@ const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
|
|
|
50
50
|
onKeyDown: (event) => quizResultClickDown(event, result, position),
|
|
51
51
|
}), [quizResultClickDown, resultClick]);
|
|
52
52
|
const getQuizImageProps = useCallback(() => ({
|
|
53
|
-
src: quizApiState.quizCurrentQuestion?.next_question?.images?.primary_url,
|
|
54
|
-
alt: quizApiState.quizCurrentQuestion?.next_question?.images?.primary_alt,
|
|
53
|
+
src: quizApiState.quizCurrentQuestion?.next_question?.images?.primary_url || undefined,
|
|
54
|
+
alt: quizApiState.quizCurrentQuestion?.next_question?.images?.primary_alt || undefined,
|
|
55
55
|
}), [quizApiState.quizCurrentQuestion]);
|
|
56
56
|
const getSelectQuestionImageProps = useCallback((option) => ({
|
|
57
57
|
className: 'cio-question-option-image',
|
|
58
|
-
src: option?.images?.primary_url,
|
|
59
|
-
alt: option?.images?.primary_alt,
|
|
58
|
+
src: option?.images?.primary_url || undefined,
|
|
59
|
+
alt: option?.images?.primary_alt || undefined,
|
|
60
60
|
}), []);
|
|
61
61
|
return {
|
|
62
62
|
getOpenTextInputProps,
|
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export default function useAddToFavoritesButtonProps(addToFavorites) {
|
|
3
|
-
const
|
|
4
|
-
const toggleIdFavorited = useCallback((id) => {
|
|
5
|
-
setFavorited({ ...favorited, [id]: !favorited?.[id] });
|
|
6
|
-
}, [favorited]);
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
export default function useAddToFavoritesButtonProps(addToFavorites, favoriteItems) {
|
|
3
|
+
const isResultFavorited = (result) => result?.data?.id && favoriteItems?.includes(result?.data?.id);
|
|
7
4
|
const getAddToFavoritesButtonProps = useCallback((result, price) => ({
|
|
8
|
-
className: `${'cio-result-card-favorites-button'} ${
|
|
5
|
+
className: `${'cio-result-card-favorites-button'} ${isResultFavorited(result) ? 'favorited' : ''}`,
|
|
9
6
|
onClick: (e) => {
|
|
10
|
-
addToFavorites(e, result, price, !
|
|
11
|
-
toggleIdFavorited(result?.data?.id || '0');
|
|
7
|
+
addToFavorites(e, result, price, !isResultFavorited(result));
|
|
12
8
|
},
|
|
13
9
|
type: 'button',
|
|
14
10
|
}),
|
|
15
11
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
16
|
-
[
|
|
12
|
+
[addToFavorites]);
|
|
17
13
|
return getAddToFavoritesButtonProps;
|
|
18
14
|
}
|
|
@@ -6,7 +6,7 @@ import usePrimaryColorStyles from './usePrimaryColorStyles';
|
|
|
6
6
|
import useQuizEvents from './useQuizEvents';
|
|
7
7
|
import useQuizState from './useQuizState';
|
|
8
8
|
const useQuiz = (quizOptions) => {
|
|
9
|
-
const { apiKey, cioJsClient, primaryColor } = quizOptions;
|
|
9
|
+
const { apiKey, cioJsClient, primaryColor, resultsPageOptions: { favoriteItems }, } = quizOptions;
|
|
10
10
|
// Log console errors for required parameters quizId and resultsPageOptions
|
|
11
11
|
useConsoleErrors(quizOptions);
|
|
12
12
|
// Quiz Cio Client
|
|
@@ -18,7 +18,7 @@ const useQuiz = (quizOptions) => {
|
|
|
18
18
|
// Props getters
|
|
19
19
|
const { quizApiState, quizLocalState, quizSessionStorageState } = quizState;
|
|
20
20
|
const { skipToResults } = quizSessionStorageState;
|
|
21
|
-
const propGetters = usePropsGetters(quizEvents, quizApiState, quizLocalState);
|
|
21
|
+
const propGetters = usePropsGetters(quizEvents, quizApiState, quizLocalState, favoriteItems);
|
|
22
22
|
const primaryColorStyles = usePrimaryColorStyles(primaryColor);
|
|
23
23
|
useEffect(() => {
|
|
24
24
|
if (skipToResults)
|
package/lib/mjs/src/utils.js
CHANGED
|
@@ -39,7 +39,15 @@ ${templateCode}
|
|
|
39
39
|
export const functionStrings = {
|
|
40
40
|
onAddToCartClick: `(item) => console.dir(item)`,
|
|
41
41
|
onQuizResultClick: `(result, position) => console.dir(result, position)`,
|
|
42
|
-
onAddToFavoritesClick: `(
|
|
42
|
+
onAddToFavoritesClick: `(result) => {
|
|
43
|
+
if (result.data) {
|
|
44
|
+
if (!favorites.includes(result.data.id)) {
|
|
45
|
+
setFavorites([...favorites, result.data.id]);
|
|
46
|
+
} else {
|
|
47
|
+
setFavorites(favorites.filter((id) => id !== result.data?.id));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}`,
|
|
43
51
|
onQuizResultsLoaded: `(results) => console.dir(results)`,
|
|
44
52
|
onQuizNextQuestion: `(question) => console.dir(question)`,
|
|
45
53
|
onQuizSkipQuestion: `(question) => console.dir(question)`,
|
package/lib/styles.css
CHANGED
|
@@ -613,11 +613,21 @@
|
|
|
613
613
|
margin-bottom: 1rem;
|
|
614
614
|
}
|
|
615
615
|
|
|
616
|
+
.cio-quiz .cio-result-card-favorites-button svg {
|
|
617
|
+
fill: none;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
.cio-quiz .cio-result-card-favorites-button:hover svg {
|
|
621
|
+
fill: rgb(255, 51, 51);
|
|
622
|
+
stroke: rgb(255, 51, 51);
|
|
623
|
+
}
|
|
624
|
+
|
|
616
625
|
.cio-quiz .cio-result-card-favorites-button.favorited svg {
|
|
617
|
-
fill:
|
|
626
|
+
fill: rgb(255, 51, 51);
|
|
627
|
+
stroke: transparent;
|
|
618
628
|
}
|
|
619
629
|
|
|
620
|
-
.cio-quiz .cio-result-card-favorites-button svg
|
|
630
|
+
.cio-quiz .cio-result-card-favorites-button svg {
|
|
621
631
|
stroke: black;
|
|
622
632
|
stroke-width: 1px;
|
|
623
633
|
}
|
|
@@ -8,6 +8,7 @@ export declare const cioJsClientDescription = "If you are already using an insta
|
|
|
8
8
|
export declare const smallContainerDescription = "If you are using the provided styles, CioQuiz component will respect the height and width of its parent container and use responsive styles based on the parent container's dimensions";
|
|
9
9
|
export declare const changePrimaryColorDescription = "\nIf you would like to use a different primary color, pass a `primaryColor` string in RGB format ('R, G, B').\n\nAccent colors for borders and different states (hover, focus, active) will be calculated automatically based on the primary color you provided.\n\nBy default, the primary color has a value of \"35, 71, 199\" (Constructor Blue).\n\nIn the example below, the `primaryColor` prop has been used to override this color to \"255, 82, 48\" (Orange).\n\n> Advanced Option: Instead of passing a primaryColor prop, you can also override `--primary-color-h`, `--primary-color-s`, and `--primary-color-l` CSS variables within a `.cio-quiz` container element. If explicitly given a value in your CSS, then the values of these variables will be used as the HSL values for your quiz.\n";
|
|
10
10
|
export declare const callbacksDescription = "Pass an `apiKey`, a `quizId`, and `callbacks`";
|
|
11
|
+
export declare const favoritesDescription = "\nAdd `const [favorites, setFavorites] = useState([]);` or equivalent to manage the favorite items' state.\n\nPass favorites as an array to `favoriteItems` in `resultsPageOptions` as a prop to CioQuiz.\n\nPass a callback function to `onAddToFavoritesClick` in `resultsPageOptions` as a prop to CioQuiz to handle favorites state changes to update `favorites` array. \n";
|
|
11
12
|
export declare enum RequestStates {
|
|
12
13
|
Stale = "STALE",
|
|
13
14
|
Loading = "LOADING",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { GetResetQuizButtonProps, GetCoverQuestionProps, GetNextQuestionButtonProps, GetOpenTextInputProps, GetPreviousQuestionButtonProps, GetQuizImageProps, GetSelectInputProps, QuizEventsReturn, GetHydrateQuizButtonProps, GetAddToCartButtonProps, GetQuizResultButtonProps, GetQuizResultLinkProps, GetSelectQuestionImageProps, GetAddToFavoritesButtonProps, GetSkipQuestionButtonProps } from '../../types';
|
|
2
2
|
import { QuizAPIReducerState } from '../../components/CioQuiz/quizApiReducer';
|
|
3
3
|
import { QuizLocalReducerState } from '../../components/CioQuiz/quizLocalReducer';
|
|
4
|
-
declare const usePropsGetters: (quizEvents: QuizEventsReturn, quizApiState: QuizAPIReducerState, quizLocalState: QuizLocalReducerState) => {
|
|
4
|
+
declare const usePropsGetters: (quizEvents: QuizEventsReturn, quizApiState: QuizAPIReducerState, quizLocalState: QuizLocalReducerState, favoriteItems?: string[]) => {
|
|
5
5
|
getOpenTextInputProps: GetOpenTextInputProps;
|
|
6
6
|
getNextQuestionButtonProps: GetNextQuestionButtonProps;
|
|
7
7
|
getPreviousQuestionButtonProps: GetPreviousQuestionButtonProps;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { GetAddToFavoritesButtonProps, QuizEventsReturn } from '../../types';
|
|
2
|
-
export default function useAddToFavoritesButtonProps(addToFavorites: QuizEventsReturn.AddToFavorites): GetAddToFavoritesButtonProps;
|
|
2
|
+
export default function useAddToFavoritesButtonProps(addToFavorites: QuizEventsReturn.AddToFavorites, favoriteItems?: string[]): GetAddToFavoritesButtonProps;
|
package/lib/types/src/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { QuizResultData, NextQuestionResponse, QuizResultsResponse,
|
|
2
|
+
import { QuizResultData, NextQuestionResponse, QuizResultsResponse, QuestionOption, Question } from '@constructor-io/constructorio-client-javascript/lib/types';
|
|
3
3
|
import ConstructorIOClient from '@constructor-io/constructorio-client-javascript';
|
|
4
4
|
import { RequestStates } from './constants';
|
|
5
5
|
import { QuestionTypes } from './components/CioQuiz/actions';
|
|
@@ -20,6 +20,7 @@ export declare namespace QuizResultsEventsProps {
|
|
|
20
20
|
}
|
|
21
21
|
export interface ResultsPageOptions extends ResultCardProps {
|
|
22
22
|
numResultsToDisplay?: number;
|
|
23
|
+
favoriteItems?: string[];
|
|
23
24
|
onQuizResultsLoaded?: QuizResultsEventsProps.OnQuizResultsLoaded;
|
|
24
25
|
onQuizResultClick?: QuizResultsEventsProps.OnQuizResultClick;
|
|
25
26
|
onAddToCartClick: QuizResultsEventsProps.OnAddToCartClick;
|
|
@@ -157,8 +158,8 @@ export interface AddToCartButtonProps {
|
|
|
157
158
|
}
|
|
158
159
|
export interface QuizImageProps {
|
|
159
160
|
className?: string;
|
|
160
|
-
src?:
|
|
161
|
-
alt?:
|
|
161
|
+
src?: string;
|
|
162
|
+
alt?: string;
|
|
162
163
|
}
|
|
163
164
|
export interface QuizResultPropsLink {
|
|
164
165
|
href?: string;
|