@constructor-io/constructorio-ui-quizzes 1.5.0 → 1.5.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.
Files changed (34) hide show
  1. package/dist/constructorio-ui-quizzes-bundled.js +15 -15
  2. package/lib/cjs/components/CioQuiz/index.js +3 -1
  3. package/lib/cjs/components/ResultCard/ResultCard.js +3 -1
  4. package/lib/cjs/components/ResultFavoritesButton/ResultFavoritesButton.js +18 -0
  5. package/lib/cjs/constants.js +2 -0
  6. package/lib/cjs/hooks/usePropsGetters/index.js +4 -1
  7. package/lib/cjs/hooks/usePropsGetters/useAddToFavoritesButtonProps.js +25 -0
  8. package/lib/cjs/hooks/useQuizEvents/index.js +4 -1
  9. package/lib/cjs/hooks/useQuizEvents/useQuizAddToFavorites.js +22 -0
  10. package/lib/cjs/services/index.js +4 -1
  11. package/lib/cjs/stories/Quiz/tests/mocks.js +3 -1
  12. package/lib/cjs/utils.js +1 -0
  13. package/lib/mjs/components/CioQuiz/index.js +3 -1
  14. package/lib/mjs/components/ResultCard/ResultCard.js +3 -1
  15. package/lib/mjs/components/ResultFavoritesButton/ResultFavoritesButton.js +14 -0
  16. package/lib/mjs/constants.js +2 -0
  17. package/lib/mjs/hooks/usePropsGetters/index.js +4 -1
  18. package/lib/mjs/hooks/usePropsGetters/useAddToFavoritesButtonProps.js +18 -0
  19. package/lib/mjs/hooks/useQuizEvents/index.js +4 -1
  20. package/lib/mjs/hooks/useQuizEvents/useQuizAddToFavorites.js +20 -0
  21. package/lib/mjs/services/index.js +4 -1
  22. package/lib/mjs/stories/Quiz/tests/mocks.js +9 -1
  23. package/lib/mjs/utils.js +1 -0
  24. package/lib/styles.css +149 -132
  25. package/lib/types/components/CioQuiz/context.d.ts +3 -1
  26. package/lib/types/components/ResultFavoritesButton/ResultFavoritesButton.d.ts +11 -0
  27. package/lib/types/constants.d.ts +2 -2
  28. package/lib/types/hooks/usePropsGetters/index.d.ts +2 -1
  29. package/lib/types/hooks/usePropsGetters/useAddToFavoritesButtonProps.d.ts +2 -0
  30. package/lib/types/hooks/useQuizEvents/useQuizAddToFavorites.d.ts +5 -0
  31. package/lib/types/services/index.d.ts +1 -1
  32. package/lib/types/types.d.ts +6 -0
  33. package/lib/types/utils.d.ts +1 -0
  34. package/package.json +1 -1
@@ -13,7 +13,7 @@ const SessionPromptModal_1 = tslib_1.__importDefault(require("../SessionPromptMo
13
13
  const utils_1 = require("../../utils");
14
14
  function CioQuiz(props) {
15
15
  var _a;
16
- const { cioClient, state, events: { hydrateQuiz, hasSessionStorageState, resetSessionStorageState }, getAddToCartButtonProps, getCoverQuestionProps, getHydrateQuizButtonProps, getNextQuestionButtonProps, getOpenTextInputProps, getPreviousQuestionButtonProps, getQuizImageProps, getQuizResultButtonProps, getQuizResultLinkProps, getResetQuizButtonProps, getSelectInputProps, primaryColorStyles, } = (0, useQuiz_1.default)(props);
16
+ const { cioClient, state, events: { hydrateQuiz, hasSessionStorageState, resetSessionStorageState }, getAddToCartButtonProps, getAddToFavoritesButtonProps, getCoverQuestionProps, getHydrateQuizButtonProps, getNextQuestionButtonProps, getOpenTextInputProps, getPreviousQuestionButtonProps, getQuizImageProps, getQuizResultButtonProps, getQuizResultLinkProps, getResetQuizButtonProps, getSelectInputProps, primaryColorStyles, } = (0, useQuiz_1.default)(props);
17
17
  const [showSessionPrompt, setShowSessionPrompt] = (0, react_1.useState)(false);
18
18
  const { resultsPageOptions, sessionStateOptions } = props;
19
19
  (0, react_1.useEffect)(() => {
@@ -34,6 +34,7 @@ function CioQuiz(props) {
34
34
  cioClient,
35
35
  state,
36
36
  getAddToCartButtonProps,
37
+ getAddToFavoritesButtonProps,
37
38
  getCoverQuestionProps,
38
39
  getHydrateQuizButtonProps,
39
40
  getNextQuestionButtonProps,
@@ -45,6 +46,7 @@ function CioQuiz(props) {
45
46
  getResetQuizButtonProps,
46
47
  getSelectInputProps,
47
48
  customClickItemCallback: !!(resultsPageOptions === null || resultsPageOptions === void 0 ? void 0 : resultsPageOptions.onQuizResultClick),
49
+ customAddToFavoritesCallback: !!(resultsPageOptions === null || resultsPageOptions === void 0 ? void 0 : resultsPageOptions.onAddToFavoritesClick),
48
50
  primaryColorStyles,
49
51
  };
50
52
  if (state.quiz.requestState === constants_1.RequestStates.Loading) {
@@ -3,11 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const react_1 = tslib_1.__importStar(require("react"));
5
5
  const ResultCtaButton_1 = tslib_1.__importDefault(require("../ResultCtaButton/ResultCtaButton"));
6
+ const ResultFavoritesButton_1 = tslib_1.__importDefault(require("../ResultFavoritesButton/ResultFavoritesButton"));
6
7
  const context_1 = tslib_1.__importDefault(require("../CioQuiz/context"));
7
8
  function ResultCard(props) {
8
9
  var _a, _b, _c, _d;
9
10
  const { result, salePriceKey, regularPriceKey, resultPosition, ratingCountKey, ratingScoreKey, renderResultCardPriceDetails, } = props;
10
- const { customClickItemCallback, getQuizResultButtonProps, getQuizResultLinkProps } = (0, react_1.useContext)(context_1.default);
11
+ const { customAddToFavoritesCallback, customClickItemCallback, getQuizResultButtonProps, getQuizResultLinkProps, } = (0, react_1.useContext)(context_1.default);
11
12
  const salePrice = salePriceKey && ((_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a[salePriceKey]);
12
13
  const regularPrice = regularPriceKey && ((_b = result === null || result === void 0 ? void 0 : result.data) === null || _b === void 0 ? void 0 : _b[regularPriceKey]);
13
14
  const ratingCount = ratingCountKey && ((_c = result === null || result === void 0 ? void 0 : result.data) === null || _c === void 0 ? void 0 : _c[ratingCountKey]);
@@ -39,6 +40,7 @@ function ResultCard(props) {
39
40
  const resultCardContentWithoutLink = () => getQuizResultButtonProps && (react_1.default.createElement("div", Object.assign({}, getQuizResultButtonProps({ result, position: resultPosition, type: 'button' })), resultCardContent()));
40
41
  const resultCardContentWithLink = () => getQuizResultLinkProps && (react_1.default.createElement("a", Object.assign({ className: 'cio-result-card-anchor', rel: 'noreferrer', target: '_blank' }, getQuizResultLinkProps({ result, position: resultPosition, type: 'link' })), resultCardContent()));
41
42
  return (react_1.default.createElement("div", { className: 'cio-result-card' },
43
+ customAddToFavoritesCallback && (react_1.default.createElement(ResultFavoritesButton_1.default, { item: result, price: salePrice || regularPrice })),
42
44
  !customClickItemCallback ? resultCardContentWithLink() : resultCardContentWithoutLink(),
43
45
  react_1.default.createElement(ResultCtaButton_1.default, { item: result, price: salePrice || regularPrice })));
44
46
  }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const react_1 = tslib_1.__importStar(require("react"));
5
+ const context_1 = tslib_1.__importDefault(require("../CioQuiz/context"));
6
+ function ResultFavoritesButton(props) {
7
+ const { item, price } = props;
8
+ const { getAddToFavoritesButtonProps } = (0, react_1.useContext)(context_1.default);
9
+ if (getAddToFavoritesButtonProps) {
10
+ return (
11
+ // eslint-disable-next-line react/button-has-type
12
+ react_1.default.createElement("button", Object.assign({}, getAddToFavoritesButtonProps(item, price)),
13
+ react_1.default.createElement("svg", { xmlns: 'http://www.w3.org/2000/svg', viewBox: '-2 -2 35 35' },
14
+ react_1.default.createElement("path", { d: 'M 15 28.925781 L 14.808594 28.8125 C 14.789062 28.800781 12.699219 27.566406 10.136719 25.472656 C 7.765625 23.539062 4.488281 20.453125 2.335938 16.730469 C 0.785156 14.046875 0 11.367188 0 8.765625 C 0 7.632812 0.203125 6.570312 0.601562 5.601562 C 0.988281 4.675781 1.542969 3.855469 2.257812 3.171875 C 3.664062 1.820312 5.589844 1.074219 7.6875 1.074219 C 8.960938 1.074219 10.109375 1.277344 11.105469 1.675781 C 12.054688 2.058594 12.867188 2.617188 13.523438 3.339844 C 14.195312 4.085938 14.691406 4.992188 15 6.027344 C 15.308594 4.992188 15.804688 4.085938 16.476562 3.339844 C 17.132812 2.617188 17.945312 2.058594 18.894531 1.675781 C 19.890625 1.277344 21.039062 1.074219 22.3125 1.074219 C 24.410156 1.074219 26.339844 1.820312 27.742188 3.171875 C 28.457031 3.855469 29.011719 4.675781 29.398438 5.601562 C 29.796875 6.570312 30 7.632812 30 8.765625 C 30 11.371094 29.214844 14.046875 27.664062 16.730469 C 26.710938 18.375 25.460938 20.035156 23.949219 21.664062 C 22.746094 22.960938 21.371094 24.242188 19.863281 25.472656 C 17.300781 27.566406 15.210938 28.800781 15.191406 28.8125 Z M 7.6875 1.832031 C 5.789062 1.832031 4.042969 2.5 2.78125 3.714844 C 1.457031 4.988281 0.757812 6.734375 0.757812 8.765625 C 0.757812 11.234375 1.507812 13.785156 2.992188 16.351562 C 4.144531 18.347656 6.40625 21.449219 10.601562 24.878906 C 12.679688 26.574219 14.433594 27.695312 15 28.042969 C 15.566406 27.695312 17.324219 26.574219 19.398438 24.878906 C 21.714844 22.984375 24.914062 19.96875 27.007812 16.351562 C 28.492188 13.785156 29.242188 11.234375 29.242188 8.765625 C 29.246094 6.734375 28.542969 4.988281 27.21875 3.714844 C 25.957031 2.5 24.210938 1.832031 22.3125 1.832031 C 20.023438 1.832031 18.25 2.507812 17.039062 3.847656 C 15.953125 5.050781 15.378906 6.75 15.378906 8.765625 L 14.621094 8.765625 C 14.621094 6.75 14.046875 5.050781 12.960938 3.847656 C 11.75 2.507812 9.976562 1.832031 7.6875 1.832031 Z M 7.6875 1.832031' }))));
15
+ }
16
+ return null;
17
+ }
18
+ exports.default = ResultFavoritesButton;
@@ -14,6 +14,7 @@ exports.componentDescription = `- import \`CioQuiz\` to render in your JSX.
14
14
  - To use this component, \`quizId\`, \`resultsPageOptions\`, and one of \`apiKey\` or \`cioJsClient\` are required.
15
15
  - \`resultsPageOptions\` lets you configure the results page
16
16
  - \`onAddToCartClick\` is a callback function that will be called when the "Add to cart" button is clicked
17
+ - \`onAddToFavoritesClick\` is an optional callback function that will be called when the "Add To Favorites" heart icon is clicked
17
18
  - \`onQuizResultClick\` is an optional callback function that will be called when the result card is clicked. The default behavior is redirecting the user to the item's URL
18
19
  - \`onQuizResultsLoaded\` is an optional callback function that will be called when the quiz results are loaded
19
20
  - \`resultCardRegularPriceKey\` is a parameter that specifies the metadata field name for the regular price
@@ -49,6 +50,7 @@ const {
49
50
  state: QuizReturnState, // Quiz state
50
51
  events: [{...}], // array of quiz events
51
52
  getAddToCartButtonProps: () => ({...})), // prop getter for jsx add to cart button for quiz result,
53
+ getAddToFavoritesButtonProps: () => ({...})), // prop getter for jsx add to favorites button for quiz result,
52
54
  getCoverQuestionProps: () => ({...})), // prop getter for jsx quiz cover question,
53
55
  getHydrateQuizButtonProps: () => ({...})), // prop getter for jsx hydrate quiz button,
54
56
  getNextQuestionButtonProps: () => ({...})), // prop getter for jsx next button to traverse the quiz,
@@ -7,9 +7,10 @@ const useCoverQuestionProps_1 = tslib_1.__importDefault(require("./useCoverQuest
7
7
  const useOpenTextInputProps_1 = tslib_1.__importDefault(require("./useOpenTextInputProps"));
8
8
  const useNextQuestionButtonProps_1 = tslib_1.__importDefault(require("./useNextQuestionButtonProps"));
9
9
  const usePreviousQuestionButtonProps_1 = tslib_1.__importDefault(require("./usePreviousQuestionButtonProps"));
10
+ const useAddToFavoritesButtonProps_1 = tslib_1.__importDefault(require("./useAddToFavoritesButtonProps"));
10
11
  const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
11
12
  var _a, _b, _c;
12
- const { quizAnswerChanged, nextQuestion, previousQuestion, resetQuiz, hydrateQuiz, addToCart, resultClick, } = quizEvents;
13
+ const { quizAnswerChanged, nextQuestion, previousQuestion, resetQuiz, hydrateQuiz, addToCart, addToFavorites, resultClick, } = quizEvents;
13
14
  const getOpenTextInputProps = (0, useOpenTextInputProps_1.default)(quizAnswerChanged, nextQuestion, (_a = quizApiState.quizCurrentQuestion) === null || _a === void 0 ? void 0 : _a.next_question, quizLocalState.answerInputs);
14
15
  const getCoverQuestionProps = (0, useCoverQuestionProps_1.default)(quizAnswerChanged, (_b = quizApiState.quizCurrentQuestion) === null || _b === void 0 ? void 0 : _b.next_question);
15
16
  const getSelectInputProps = (0, useSelectInputProps_1.default)(quizAnswerChanged, nextQuestion, (_c = quizApiState.quizCurrentQuestion) === null || _c === void 0 ? void 0 : _c.next_question, quizLocalState.answerInputs);
@@ -30,6 +31,7 @@ const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
30
31
  type: 'button',
31
32
  onClick: (e) => addToCart(e, result, price),
32
33
  }), [addToCart]);
34
+ const getAddToFavoritesButtonProps = (0, useAddToFavoritesButtonProps_1.default)(addToFavorites);
33
35
  const quizResultClickDown = (0, react_1.useCallback)((event, result, position) => {
34
36
  if ((event === null || event === void 0 ? void 0 : event.key) === ' ' || (event === null || event === void 0 ? void 0 : event.key) === 'Enter') {
35
37
  if (resultClick) {
@@ -78,6 +80,7 @@ const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
78
80
  getResetQuizButtonProps,
79
81
  getHydrateQuizButtonProps,
80
82
  getAddToCartButtonProps,
83
+ getAddToFavoritesButtonProps,
81
84
  getQuizResultButtonProps,
82
85
  getQuizResultLinkProps,
83
86
  };
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ function useAddToFavoritesButtonProps(addToFavorites) {
5
+ const [favorited, setFavorited] = (0, react_1.useState)({});
6
+ const toggleIdFavorited = (0, react_1.useCallback)((id) => {
7
+ setFavorited(Object.assign(Object.assign({}, favorited), { [id]: !(favorited === null || favorited === void 0 ? void 0 : favorited[id]) }));
8
+ }, [favorited]);
9
+ const getAddToFavoritesButtonProps = (0, react_1.useCallback)((result, price) => {
10
+ var _a;
11
+ return ({
12
+ className: `${'cio-result-card-favorites-button'} ${(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']) ? 'favorited' : ''}`,
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
+ },
21
+ // eslint-disable-next-line react-hooks/exhaustive-deps
22
+ [favorited, addToFavorites]);
23
+ return getAddToFavoritesButtonProps;
24
+ }
25
+ exports.default = useAddToFavoritesButtonProps;
@@ -10,10 +10,11 @@ const useQuizNextClick_1 = tslib_1.__importDefault(require("./useQuizNextClick")
10
10
  const useQuizResetClick_1 = tslib_1.__importDefault(require("./useQuizResetClick"));
11
11
  const useHydrateQuizLocalState_1 = tslib_1.__importDefault(require("./useHydrateQuizLocalState"));
12
12
  const utils_1 = require("../../utils");
13
+ const useQuizAddToFavorites_1 = tslib_1.__importDefault(require("./useQuizAddToFavorites"));
13
14
  const useQuizEvents = (quizOptions, cioClient, quizState) => {
14
15
  const { quizApiState, dispatchLocalState, dispatchApiState, hasQuizStoredState, quizStateKey, quizLocalState, } = quizState;
15
16
  const { resultsPageOptions } = quizOptions;
16
- const { onAddToCartClick, onQuizResultClick, onQuizResultsLoaded } = resultsPageOptions;
17
+ const { onAddToCartClick, onQuizResultClick, onQuizResultsLoaded, onAddToFavoritesClick } = resultsPageOptions;
17
18
  // Quiz answer change
18
19
  const quizAnswerChanged = (0, useQuizAnswerChangeHandler_1.default)(quizApiState, dispatchLocalState);
19
20
  // Quiz Next button click callback
@@ -24,6 +25,7 @@ const useQuizEvents = (quizOptions, cioClient, quizState) => {
24
25
  const addToCart = (0, useQuizAddToCart_1.default)(cioClient, quizApiState, onAddToCartClick);
25
26
  // Quiz result click callback
26
27
  const resultClick = (0, useQuizResultClick_1.default)(cioClient, quizApiState, onQuizResultClick);
28
+ const addToFavorites = (0, useQuizAddToFavorites_1.default)(cioClient, quizApiState, onAddToFavoritesClick);
27
29
  // Quiz results loaded event
28
30
  (0, useQuizResultsLoaded_1.default)(cioClient, quizApiState, onQuizResultsLoaded);
29
31
  // Quiz reset
@@ -32,6 +34,7 @@ const useQuizEvents = (quizOptions, cioClient, quizState) => {
32
34
  const hydrateQuizLocalState = (0, useHydrateQuizLocalState_1.default)(quizStateKey, dispatchLocalState);
33
35
  return {
34
36
  addToCart,
37
+ addToFavorites,
35
38
  resultClick,
36
39
  quizAnswerChanged,
37
40
  previousQuestion,
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const services_1 = require("../../services");
5
+ const utils_1 = require("../../utils");
6
+ const useQuizAddToFavorites = (cioClient, quizApiState, onAddToFavoritesClick) => {
7
+ const quizAddToFavoritesHandler = (0, react_1.useCallback)((e, result, price, sendEvent = true) => {
8
+ e.preventDefault();
9
+ if (quizApiState.quizResults) {
10
+ // Tracking call
11
+ if (sendEvent) {
12
+ (0, services_1.trackQuizConversion)(cioClient, quizApiState.quizResults, result, price, 'add_to_wishlist');
13
+ }
14
+ // User custom callback function
15
+ if ((0, utils_1.isFunction)(onAddToFavoritesClick)) {
16
+ onAddToFavoritesClick(result);
17
+ }
18
+ }
19
+ }, [quizApiState, cioClient, onAddToFavoritesClick]);
20
+ return quizAddToFavoritesHandler;
21
+ };
22
+ exports.default = useQuizAddToFavorites;
@@ -53,7 +53,9 @@ const trackQuizResultClick = (cioClient, quizResults, result, position) => {
53
53
  }
54
54
  };
55
55
  exports.trackQuizResultClick = trackQuizResultClick;
56
- const trackQuizConversion = (cioClient, quizResults, result, price) => {
56
+ const trackQuizConversion = (cioClient, quizResults, result, price, type
57
+ // eslint-disable-next-line max-params
58
+ ) => {
57
59
  var _a, _b;
58
60
  if (quizResults.request) {
59
61
  const { quiz_id, quiz_session_id, quiz_version_id, request: { section }, } = quizResults;
@@ -66,6 +68,7 @@ const trackQuizConversion = (cioClient, quizResults, result, price) => {
66
68
  section,
67
69
  variationId: (_b = result.data) === null || _b === void 0 ? void 0 : _b.variation_id,
68
70
  revenue: (price && String(price)) || undefined,
71
+ type,
69
72
  });
70
73
  }
71
74
  };
@@ -144,7 +144,8 @@ const useMockContextValue = (question) => {
144
144
  getCoverQuestionProps,
145
145
  getOpenTextInputProps,
146
146
  getSelectInputProps,
147
- getAddToCartButtonProps: () => (Object.assign(Object.assign({}, mockElementProps), { className: 'cio-question-cta-button' })),
147
+ getAddToCartButtonProps: () => (Object.assign(Object.assign({}, mockElementProps), { className: 'cio-result-card-cta-button' })),
148
+ getAddToFavoritesButtonProps: () => (Object.assign(Object.assign({}, mockElementProps), { className: 'cio-result-card-favorites-button' })),
148
149
  getHydrateQuizButtonProps: () => (Object.assign(Object.assign({}, mockElementProps), { className: 'cio-question-cta-button' })),
149
150
  getNextQuestionButtonProps: () => (Object.assign(Object.assign({}, mockElementProps), { className: 'cio-question-cta-button' })),
150
151
  getPreviousQuestionButtonProps: () => (Object.assign(Object.assign({}, mockElementProps), { className: 'cio-question-back-button' })),
@@ -158,6 +159,7 @@ const useMockContextValue = (question) => {
158
159
  '--primary-color-l': '14',
159
160
  },
160
161
  customClickItemCallback: false,
162
+ customAddToFavoritesCallback: true,
161
163
  };
162
164
  };
163
165
  exports.useMockContextValue = useMockContextValue;
package/lib/cjs/utils.js CHANGED
@@ -45,6 +45,7 @@ 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: `(item) => console.dir(item)`,
48
49
  onQuizResultsLoaded: `(results) => console.dir(results)`,
49
50
  cioJsClient: `cioJsClient`,
50
51
  };
@@ -9,7 +9,7 @@ import useQuiz from '../../hooks/useQuiz';
9
9
  import SessionPromptModal from '../SessionPromptModal/SessionPromptModal';
10
10
  import { convertPrimaryColorsToString, renderImages } from '../../utils';
11
11
  export default function CioQuiz(props) {
12
- const { cioClient, state, events: { hydrateQuiz, hasSessionStorageState, resetSessionStorageState }, getAddToCartButtonProps, getCoverQuestionProps, getHydrateQuizButtonProps, getNextQuestionButtonProps, getOpenTextInputProps, getPreviousQuestionButtonProps, getQuizImageProps, getQuizResultButtonProps, getQuizResultLinkProps, getResetQuizButtonProps, getSelectInputProps, primaryColorStyles, } = useQuiz(props);
12
+ const { cioClient, state, events: { hydrateQuiz, hasSessionStorageState, resetSessionStorageState }, getAddToCartButtonProps, getAddToFavoritesButtonProps, getCoverQuestionProps, getHydrateQuizButtonProps, getNextQuestionButtonProps, getOpenTextInputProps, getPreviousQuestionButtonProps, getQuizImageProps, getQuizResultButtonProps, getQuizResultLinkProps, getResetQuizButtonProps, getSelectInputProps, primaryColorStyles, } = useQuiz(props);
13
13
  const [showSessionPrompt, setShowSessionPrompt] = useState(false);
14
14
  const { resultsPageOptions, sessionStateOptions } = props;
15
15
  useEffect(() => {
@@ -30,6 +30,7 @@ export default function CioQuiz(props) {
30
30
  cioClient,
31
31
  state,
32
32
  getAddToCartButtonProps,
33
+ getAddToFavoritesButtonProps,
33
34
  getCoverQuestionProps,
34
35
  getHydrateQuizButtonProps,
35
36
  getNextQuestionButtonProps,
@@ -41,6 +42,7 @@ export default function CioQuiz(props) {
41
42
  getResetQuizButtonProps,
42
43
  getSelectInputProps,
43
44
  customClickItemCallback: !!resultsPageOptions?.onQuizResultClick,
45
+ customAddToFavoritesCallback: !!resultsPageOptions?.onAddToFavoritesClick,
44
46
  primaryColorStyles,
45
47
  };
46
48
  if (state.quiz.requestState === RequestStates.Loading) {
@@ -1,9 +1,10 @@
1
1
  import React, { useContext } from 'react';
2
2
  import ResultCtaButton from '../ResultCtaButton/ResultCtaButton';
3
+ import ResultFavoritesButton from '../ResultFavoritesButton/ResultFavoritesButton';
3
4
  import QuizContext from '../CioQuiz/context';
4
5
  export default function ResultCard(props) {
5
6
  const { result, salePriceKey, regularPriceKey, resultPosition, ratingCountKey, ratingScoreKey, renderResultCardPriceDetails, } = props;
6
- const { customClickItemCallback, getQuizResultButtonProps, getQuizResultLinkProps } = useContext(QuizContext);
7
+ const { customAddToFavoritesCallback, customClickItemCallback, getQuizResultButtonProps, getQuizResultLinkProps, } = useContext(QuizContext);
7
8
  const salePrice = salePriceKey && result?.data?.[salePriceKey];
8
9
  const regularPrice = regularPriceKey && result?.data?.[regularPriceKey];
9
10
  const ratingCount = ratingCountKey && result?.data?.[ratingCountKey];
@@ -32,6 +33,7 @@ export default function ResultCard(props) {
32
33
  const resultCardContentWithoutLink = () => getQuizResultButtonProps && (React.createElement("div", { ...getQuizResultButtonProps({ result, position: resultPosition, type: 'button' }) }, resultCardContent()));
33
34
  const resultCardContentWithLink = () => getQuizResultLinkProps && (React.createElement("a", { className: 'cio-result-card-anchor', rel: 'noreferrer', target: '_blank', ...getQuizResultLinkProps({ result, position: resultPosition, type: 'link' }) }, resultCardContent()));
34
35
  return (React.createElement("div", { className: 'cio-result-card' },
36
+ customAddToFavoritesCallback && (React.createElement(ResultFavoritesButton, { item: result, price: salePrice || regularPrice })),
35
37
  !customClickItemCallback ? resultCardContentWithLink() : resultCardContentWithoutLink(),
36
38
  React.createElement(ResultCtaButton, { item: result, price: salePrice || regularPrice })));
37
39
  }
@@ -0,0 +1,14 @@
1
+ import React, { useContext } from 'react';
2
+ import QuizContext from '../CioQuiz/context';
3
+ export default function ResultFavoritesButton(props) {
4
+ const { item, price } = props;
5
+ const { getAddToFavoritesButtonProps } = useContext(QuizContext);
6
+ if (getAddToFavoritesButtonProps) {
7
+ return (
8
+ // eslint-disable-next-line react/button-has-type
9
+ React.createElement("button", { ...getAddToFavoritesButtonProps(item, price) },
10
+ React.createElement("svg", { xmlns: 'http://www.w3.org/2000/svg', viewBox: '-2 -2 35 35' },
11
+ React.createElement("path", { d: 'M 15 28.925781 L 14.808594 28.8125 C 14.789062 28.800781 12.699219 27.566406 10.136719 25.472656 C 7.765625 23.539062 4.488281 20.453125 2.335938 16.730469 C 0.785156 14.046875 0 11.367188 0 8.765625 C 0 7.632812 0.203125 6.570312 0.601562 5.601562 C 0.988281 4.675781 1.542969 3.855469 2.257812 3.171875 C 3.664062 1.820312 5.589844 1.074219 7.6875 1.074219 C 8.960938 1.074219 10.109375 1.277344 11.105469 1.675781 C 12.054688 2.058594 12.867188 2.617188 13.523438 3.339844 C 14.195312 4.085938 14.691406 4.992188 15 6.027344 C 15.308594 4.992188 15.804688 4.085938 16.476562 3.339844 C 17.132812 2.617188 17.945312 2.058594 18.894531 1.675781 C 19.890625 1.277344 21.039062 1.074219 22.3125 1.074219 C 24.410156 1.074219 26.339844 1.820312 27.742188 3.171875 C 28.457031 3.855469 29.011719 4.675781 29.398438 5.601562 C 29.796875 6.570312 30 7.632812 30 8.765625 C 30 11.371094 29.214844 14.046875 27.664062 16.730469 C 26.710938 18.375 25.460938 20.035156 23.949219 21.664062 C 22.746094 22.960938 21.371094 24.242188 19.863281 25.472656 C 17.300781 27.566406 15.210938 28.800781 15.191406 28.8125 Z M 7.6875 1.832031 C 5.789062 1.832031 4.042969 2.5 2.78125 3.714844 C 1.457031 4.988281 0.757812 6.734375 0.757812 8.765625 C 0.757812 11.234375 1.507812 13.785156 2.992188 16.351562 C 4.144531 18.347656 6.40625 21.449219 10.601562 24.878906 C 12.679688 26.574219 14.433594 27.695312 15 28.042969 C 15.566406 27.695312 17.324219 26.574219 19.398438 24.878906 C 21.714844 22.984375 24.914062 19.96875 27.007812 16.351562 C 28.492188 13.785156 29.242188 11.234375 29.242188 8.765625 C 29.246094 6.734375 28.542969 4.988281 27.21875 3.714844 C 25.957031 2.5 24.210938 1.832031 22.3125 1.832031 C 20.023438 1.832031 18.25 2.507812 17.039062 3.847656 C 15.953125 5.050781 15.378906 6.75 15.378906 8.765625 L 14.621094 8.765625 C 14.621094 6.75 14.046875 5.050781 12.960938 3.847656 C 11.75 2.507812 9.976562 1.832031 7.6875 1.832031 Z M 7.6875 1.832031' }))));
12
+ }
13
+ return null;
14
+ }
@@ -11,6 +11,7 @@ export const componentDescription = `- import \`CioQuiz\` to render in your JSX.
11
11
  - To use this component, \`quizId\`, \`resultsPageOptions\`, and one of \`apiKey\` or \`cioJsClient\` are required.
12
12
  - \`resultsPageOptions\` lets you configure the results page
13
13
  - \`onAddToCartClick\` is a callback function that will be called when the "Add to cart" button is clicked
14
+ - \`onAddToFavoritesClick\` is an optional callback function that will be called when the "Add To Favorites" heart icon is clicked
14
15
  - \`onQuizResultClick\` is an optional callback function that will be called when the result card is clicked. The default behavior is redirecting the user to the item's URL
15
16
  - \`onQuizResultsLoaded\` is an optional callback function that will be called when the quiz results are loaded
16
17
  - \`resultCardRegularPriceKey\` is a parameter that specifies the metadata field name for the regular price
@@ -46,6 +47,7 @@ const {
46
47
  state: QuizReturnState, // Quiz state
47
48
  events: [{...}], // array of quiz events
48
49
  getAddToCartButtonProps: () => ({...})), // prop getter for jsx add to cart button for quiz result,
50
+ getAddToFavoritesButtonProps: () => ({...})), // prop getter for jsx add to favorites button for quiz result,
49
51
  getCoverQuestionProps: () => ({...})), // prop getter for jsx quiz cover question,
50
52
  getHydrateQuizButtonProps: () => ({...})), // prop getter for jsx hydrate quiz button,
51
53
  getNextQuestionButtonProps: () => ({...})), // prop getter for jsx next button to traverse the quiz,
@@ -4,8 +4,9 @@ import useCoverQuestionProps from './useCoverQuestionProps';
4
4
  import useOpenTextInputProps from './useOpenTextInputProps';
5
5
  import useNextQuestionButtonProps from './useNextQuestionButtonProps';
6
6
  import usePreviousQuestionButtonProps from './usePreviousQuestionButtonProps';
7
+ import useAddToFavoritesButtonProps from './useAddToFavoritesButtonProps';
7
8
  const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
8
- const { quizAnswerChanged, nextQuestion, previousQuestion, resetQuiz, hydrateQuiz, addToCart, resultClick, } = quizEvents;
9
+ const { quizAnswerChanged, nextQuestion, previousQuestion, resetQuiz, hydrateQuiz, addToCart, addToFavorites, resultClick, } = quizEvents;
9
10
  const getOpenTextInputProps = useOpenTextInputProps(quizAnswerChanged, nextQuestion, quizApiState.quizCurrentQuestion?.next_question, quizLocalState.answerInputs);
10
11
  const getCoverQuestionProps = useCoverQuestionProps(quizAnswerChanged, quizApiState.quizCurrentQuestion?.next_question);
11
12
  const getSelectInputProps = useSelectInputProps(quizAnswerChanged, nextQuestion, quizApiState.quizCurrentQuestion?.next_question, quizLocalState.answerInputs);
@@ -26,6 +27,7 @@ const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
26
27
  type: 'button',
27
28
  onClick: (e) => addToCart(e, result, price),
28
29
  }), [addToCart]);
30
+ const getAddToFavoritesButtonProps = useAddToFavoritesButtonProps(addToFavorites);
29
31
  const quizResultClickDown = useCallback((event, result, position) => {
30
32
  if (event?.key === ' ' || event?.key === 'Enter') {
31
33
  if (resultClick) {
@@ -65,6 +67,7 @@ const usePropsGetters = (quizEvents, quizApiState, quizLocalState) => {
65
67
  getResetQuizButtonProps,
66
68
  getHydrateQuizButtonProps,
67
69
  getAddToCartButtonProps,
70
+ getAddToFavoritesButtonProps,
68
71
  getQuizResultButtonProps,
69
72
  getQuizResultLinkProps,
70
73
  };
@@ -0,0 +1,18 @@
1
+ import { useState, useCallback } from 'react';
2
+ export default function useAddToFavoritesButtonProps(addToFavorites) {
3
+ const [favorited, setFavorited] = useState({});
4
+ const toggleIdFavorited = useCallback((id) => {
5
+ setFavorited({ ...favorited, [id]: !favorited?.[id] });
6
+ }, [favorited]);
7
+ const getAddToFavoritesButtonProps = useCallback((result, price) => ({
8
+ className: `${'cio-result-card-favorites-button'} ${favorited?.[result?.data?.id || '0'] ? 'favorited' : ''}`,
9
+ onClick: (e) => {
10
+ addToFavorites(e, result, price, !favorited?.[result?.data?.id || '0']);
11
+ toggleIdFavorited(result?.data?.id || '0');
12
+ },
13
+ type: 'button',
14
+ }),
15
+ // eslint-disable-next-line react-hooks/exhaustive-deps
16
+ [favorited, addToFavorites]);
17
+ return getAddToFavoritesButtonProps;
18
+ }
@@ -7,10 +7,11 @@ import useQuizNextClick from './useQuizNextClick';
7
7
  import useQuizResetClick from './useQuizResetClick';
8
8
  import useHydrateQuizLocalState from './useHydrateQuizLocalState';
9
9
  import { resetQuizSessionStorageState } from '../../utils';
10
+ import useQuizAddToFavorites from './useQuizAddToFavorites';
10
11
  const useQuizEvents = (quizOptions, cioClient, quizState) => {
11
12
  const { quizApiState, dispatchLocalState, dispatchApiState, hasQuizStoredState, quizStateKey, quizLocalState, } = quizState;
12
13
  const { resultsPageOptions } = quizOptions;
13
- const { onAddToCartClick, onQuizResultClick, onQuizResultsLoaded } = resultsPageOptions;
14
+ const { onAddToCartClick, onQuizResultClick, onQuizResultsLoaded, onAddToFavoritesClick } = resultsPageOptions;
14
15
  // Quiz answer change
15
16
  const quizAnswerChanged = useQuizAnswerChangeHandler(quizApiState, dispatchLocalState);
16
17
  // Quiz Next button click callback
@@ -21,6 +22,7 @@ const useQuizEvents = (quizOptions, cioClient, quizState) => {
21
22
  const addToCart = useQuizAddToCart(cioClient, quizApiState, onAddToCartClick);
22
23
  // Quiz result click callback
23
24
  const resultClick = useQuizResultClick(cioClient, quizApiState, onQuizResultClick);
25
+ const addToFavorites = useQuizAddToFavorites(cioClient, quizApiState, onAddToFavoritesClick);
24
26
  // Quiz results loaded event
25
27
  useQuizResultsLoaded(cioClient, quizApiState, onQuizResultsLoaded);
26
28
  // Quiz reset
@@ -29,6 +31,7 @@ const useQuizEvents = (quizOptions, cioClient, quizState) => {
29
31
  const hydrateQuizLocalState = useHydrateQuizLocalState(quizStateKey, dispatchLocalState);
30
32
  return {
31
33
  addToCart,
34
+ addToFavorites,
32
35
  resultClick,
33
36
  quizAnswerChanged,
34
37
  previousQuestion,
@@ -0,0 +1,20 @@
1
+ import { useCallback } from 'react';
2
+ import { trackQuizConversion } from '../../services';
3
+ import { isFunction } from '../../utils';
4
+ const useQuizAddToFavorites = (cioClient, quizApiState, onAddToFavoritesClick) => {
5
+ const quizAddToFavoritesHandler = useCallback((e, result, price, sendEvent = true) => {
6
+ e.preventDefault();
7
+ if (quizApiState.quizResults) {
8
+ // Tracking call
9
+ if (sendEvent) {
10
+ trackQuizConversion(cioClient, quizApiState.quizResults, result, price, 'add_to_wishlist');
11
+ }
12
+ // User custom callback function
13
+ if (isFunction(onAddToFavoritesClick)) {
14
+ onAddToFavoritesClick(result);
15
+ }
16
+ }
17
+ }, [quizApiState, cioClient, onAddToFavoritesClick]);
18
+ return quizAddToFavoritesHandler;
19
+ };
20
+ export default useQuizAddToFavorites;
@@ -43,7 +43,9 @@ export const trackQuizResultClick = (cioClient, quizResults, result, position) =
43
43
  });
44
44
  }
45
45
  };
46
- export const trackQuizConversion = (cioClient, quizResults, result, price) => {
46
+ export const trackQuizConversion = (cioClient, quizResults, result, price, type
47
+ // eslint-disable-next-line max-params
48
+ ) => {
47
49
  if (quizResults.request) {
48
50
  const { quiz_id, quiz_session_id, quiz_version_id, request: { section }, } = quizResults;
49
51
  cioClient?.tracker.trackQuizConversion({
@@ -55,6 +57,7 @@ export const trackQuizConversion = (cioClient, quizResults, result, price) => {
55
57
  section,
56
58
  variationId: result.data?.variation_id,
57
59
  revenue: (price && String(price)) || undefined,
60
+ type,
58
61
  });
59
62
  }
60
63
  };
@@ -155,7 +155,14 @@ export const useMockContextValue = (question) => {
155
155
  getCoverQuestionProps,
156
156
  getOpenTextInputProps,
157
157
  getSelectInputProps,
158
- getAddToCartButtonProps: () => ({ ...mockElementProps, className: 'cio-question-cta-button' }),
158
+ getAddToCartButtonProps: () => ({
159
+ ...mockElementProps,
160
+ className: 'cio-result-card-cta-button',
161
+ }),
162
+ getAddToFavoritesButtonProps: () => ({
163
+ ...mockElementProps,
164
+ className: 'cio-result-card-favorites-button',
165
+ }),
159
166
  getHydrateQuizButtonProps: () => ({
160
167
  ...mockElementProps,
161
168
  className: 'cio-question-cta-button',
@@ -178,5 +185,6 @@ export const useMockContextValue = (question) => {
178
185
  '--primary-color-l': '14',
179
186
  },
180
187
  customClickItemCallback: false,
188
+ customAddToFavoritesCallback: true,
181
189
  };
182
190
  };
package/lib/mjs/utils.js CHANGED
@@ -39,6 +39,7 @@ ${templateCode}
39
39
  export const functionStrings = {
40
40
  onAddToCartClick: `(item) => console.dir(item)`,
41
41
  onQuizResultClick: `(result, position) => console.dir(result, position)`,
42
+ onAddToFavoritesClick: `(item) => console.dir(item)`,
42
43
  onQuizResultsLoaded: `(results) => console.dir(results)`,
43
44
  cioJsClient: `cioJsClient`,
44
45
  };