@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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-ui-quizzes",
3
- "version": "1.9.0",
3
+ "version": "1.9.2",
4
4
  "description": "Constructor.io Quizzes UI library for web applications",
5
5
  "author": "constructor.io",
6
6
  "license": "MIT",
@@ -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", { 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' }))));
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;
@@ -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 [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
- },
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
- [favorited, addToFavorites]);
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)
@@ -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: `(item) => console.dir(item)`,
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)`,
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-ui-quizzes",
3
- "version": "1.9.0",
3
+ "version": "1.9.2",
4
4
  "description": "Constructor.io Quizzes UI library for web applications",
5
5
  "author": "constructor.io",
6
6
  "license": "MIT",
@@ -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", { 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' }))));
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;
@@ -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 { 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]);
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'} ${favorited?.[result?.data?.id || '0'] ? 'favorited' : ''}`,
5
+ className: `${'cio-result-card-favorites-button'} ${isResultFavorited(result) ? 'favorited' : ''}`,
9
6
  onClick: (e) => {
10
- addToFavorites(e, result, price, !favorited?.[result?.data?.id || '0']);
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
- [favorited, addToFavorites]);
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)
@@ -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: `(item) => console.dir(item)`,
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: red;
626
+ fill: rgb(255, 51, 51);
627
+ stroke: transparent;
618
628
  }
619
629
 
620
- .cio-quiz .cio-result-card-favorites-button svg:hover {
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;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { QuizResultData, NextQuestionResponse, QuizResultsResponse, Nullable, QuestionOption, Question } from '@constructor-io/constructorio-client-javascript/lib/types';
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?: Nullable<string>;
161
- alt?: Nullable<string>;
161
+ src?: string;
162
+ alt?: string;
162
163
  }
163
164
  export interface QuizResultPropsLink {
164
165
  href?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-ui-quizzes",
3
- "version": "1.9.0",
3
+ "version": "1.9.2",
4
4
  "description": "Constructor.io Quizzes UI library for web applications",
5
5
  "author": "constructor.io",
6
6
  "license": "MIT",