@constructor-io/constructorio-ui-quizzes 1.7.1 → 1.8.0
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 +13 -13
- package/lib/cjs/components/CioQuiz/index.js +5 -8
- package/lib/cjs/constants.js +4 -1
- package/lib/cjs/hooks/useQuiz.js +9 -2
- package/lib/cjs/hooks/useQuizEvents/index.js +6 -7
- package/lib/cjs/hooks/useQuizEvents/useHydrateQuizLocalState.js +5 -5
- package/lib/cjs/hooks/useQuizEvents/useQuizNextClick.js +11 -2
- package/lib/cjs/hooks/useQuizState/index.js +10 -5
- package/lib/cjs/hooks/useQuizState/useQuizLocalState.js +1 -17
- package/lib/cjs/hooks/useQuizState/useSessionStorageState.js +26 -0
- package/lib/cjs/stories/Quiz/tests/mocks.js +5 -1
- package/lib/cjs/utils.js +8 -4
- package/lib/mjs/components/CioQuiz/index.js +5 -8
- package/lib/mjs/constants.js +3 -0
- package/lib/mjs/hooks/useQuiz.js +9 -2
- package/lib/mjs/hooks/useQuizEvents/index.js +6 -7
- package/lib/mjs/hooks/useQuizEvents/useHydrateQuizLocalState.js +5 -5
- package/lib/mjs/hooks/useQuizEvents/useQuizNextClick.js +11 -2
- package/lib/mjs/hooks/useQuizState/index.js +10 -5
- package/lib/mjs/hooks/useQuizState/useQuizLocalState.js +3 -17
- package/lib/mjs/hooks/useQuizState/useSessionStorageState.js +22 -0
- package/lib/mjs/stories/Quiz/tests/mocks.js +6 -2
- package/lib/mjs/utils.js +7 -4
- package/lib/types/constants.d.ts +2 -1
- package/lib/types/hooks/useQuizEvents/useHydrateQuizLocalState.d.ts +1 -1
- package/lib/types/hooks/useQuizEvents/useQuizNextClick.d.ts +2 -2
- package/lib/types/hooks/useQuizState/index.d.ts +2 -4
- package/lib/types/hooks/useQuizState/useQuizLocalState.d.ts +1 -5
- package/lib/types/hooks/useQuizState/useSessionStorageState.d.ts +8 -0
- package/lib/types/types.d.ts +21 -8
- package/lib/types/utils.d.ts +1 -0
- package/package.json +1 -1
package/lib/mjs/utils.js
CHANGED
|
@@ -41,6 +41,7 @@ export const functionStrings = {
|
|
|
41
41
|
onQuizResultClick: `(result, position) => console.dir(result, position)`,
|
|
42
42
|
onAddToFavoritesClick: `(item) => console.dir(item)`,
|
|
43
43
|
onQuizResultsLoaded: `(results) => console.dir(results)`,
|
|
44
|
+
onQuizNextQuestion: `(question) => console.dir(question)`,
|
|
44
45
|
cioJsClient: `cioJsClient`,
|
|
45
46
|
};
|
|
46
47
|
export const stringifyWithDefaults = (obj) => {
|
|
@@ -105,10 +106,12 @@ export const resetQuizSessionStorageState = (quizStateKey) => () => {
|
|
|
105
106
|
window?.sessionStorage?.removeItem(quizStateKey);
|
|
106
107
|
};
|
|
107
108
|
export const logger = (action) => {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
if (process?.env?.LOGGER) {
|
|
110
|
+
console.group(`%cAction:%c ${action.type}`, 'color: red; font-weight: bold;', 'color: green; font-weight: lighter;');
|
|
111
|
+
console.log('%c type:', 'color: #9E9E9E; font-weight: 700;', action.type);
|
|
112
|
+
console.log('%c payload:', 'color: #00A7F7; font-weight: 700;', action.payload);
|
|
113
|
+
console.groupEnd();
|
|
114
|
+
}
|
|
112
115
|
};
|
|
113
116
|
// Function to emulate pausing between interactions
|
|
114
117
|
export function sleep(ms) {
|
package/lib/types/constants.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
export declare const apiKey = "key_wJSdZSiesX5hiVLt";
|
|
2
2
|
export declare const quizId = "coffee-quiz";
|
|
3
3
|
export declare const quizSessionStateKey = "constructorIOQuizState";
|
|
4
|
-
export declare const componentDescription = "- import `CioQuiz` to render in your JSX.\n- This component handles state management, data fetching, and rendering logic.\n- To use this component, `quizId`, `resultsPageOptions`, and one of `apiKey` or `cioJsClient` are required.\n- `resultsPageOptions` lets you configure the results page\n - `onAddToCartClick` is a callback function that will be called when the \"Add to cart\" button is clicked\n - `onAddToFavoritesClick` is an optional callback function that will be called when the \"Add To Favorites\" heart icon is clicked\n - `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\n - `onQuizResultsLoaded` is an optional callback function that will be called when the quiz results are loaded\n - `resultCardRegularPriceKey` is a parameter that specifies the metadata field name for the regular price\n - `resultCardSalePriceKey` is an optional parameter that specifies the metadata field name for the sale price \n - `resultCardRatingCountKey` is an optional parameter that specifies the metadata field name for the ratings count \n - `resultCardRatingScoreKey` is an optional parameter that specifies the metadata field name for the ratings score \n - `renderResultCardPriceDetails` is an optional render function to render custom prices section in result card \n - `numResultsToDisplay` is an optional parameter that determines how many results should be displayed on results page\n- `sessionStateOptions` lets you configure the session modal behavior\n - `showSessionModal` is a boolean used to decide whether to show the session modal. The default behavior is to show the session modal\n - `showSessionModalOnResults` is a boolean to decide whether to show the session modal after reaching the results page. The default behavior is to not show the session modal\n - `sessionStateKey` is a custom string that will be used as a session storage key\n- Use different props to configure the behavior of this component.\n- The following stories show how different props affect the component's behavior\n\n> Note: `cioJsClient` refers to an instance of the [constructorio-client-javascript](https://www.npmjs.com/package/@constructor-io/constructorio-client-javascript)\n";
|
|
4
|
+
export declare const componentDescription = "- import `CioQuiz` to render in your JSX.\n- This component handles state management, data fetching, and rendering logic.\n- To use this component, `quizId`, `resultsPageOptions`, and one of `apiKey` or `cioJsClient` are required.\n- `resultsPageOptions` lets you configure the results page\n - `onAddToCartClick` is a callback function that will be called when the \"Add to cart\" button is clicked\n - `onAddToFavoritesClick` is an optional callback function that will be called when the \"Add To Favorites\" heart icon is clicked\n - `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\n - `onQuizResultsLoaded` is an optional callback function that will be called when the quiz results are loaded\n - `resultCardRegularPriceKey` is a parameter that specifies the metadata field name for the regular price\n - `resultCardSalePriceKey` is an optional parameter that specifies the metadata field name for the sale price \n - `resultCardRatingCountKey` is an optional parameter that specifies the metadata field name for the ratings count \n - `resultCardRatingScoreKey` is an optional parameter that specifies the metadata field name for the ratings score \n - `renderResultCardPriceDetails` is an optional render function to render custom prices section in result card \n - `numResultsToDisplay` is an optional parameter that determines how many results should be displayed on results page\n- `callbacks` lets you pass callback functions that will be called on certain actions\n - `onQuizNextQuestion` is an optional callback function that will be called when user moves to the next question\n- `sessionStateOptions` lets you configure the session modal behavior\n - `showSessionModal` is a boolean used to decide whether to show the session modal. The default behavior is to show the session modal\n - `showSessionModalOnResults` is a boolean to decide whether to show the session modal after reaching the results page. The default behavior is to not show the session modal\n - `sessionStateKey` is a custom string that will be used as a session storage key\n- Use different props to configure the behavior of this component.\n- The following stories show how different props affect the component's behavior\n\n> Note: `cioJsClient` refers to an instance of the [constructorio-client-javascript](https://www.npmjs.com/package/@constructor-io/constructorio-client-javascript)\n";
|
|
5
5
|
export declare const hookDescription = "- import `useCioQuiz` and call this custom hook in a functional component.\n- This hook leaves rendering logic up to you, while handling:\n - state management\n - data fetching\n - keyboard navigation\n - mouse interactions\n - focus and submit event handling\n- Since the markup is controlled by you, the default styles might not be applied if you have a different DOM structure\n- To use this hook, an `apiKey` and `quizId` are required, and `resultsPageOptions` must be passed to the `useCioQuiz` hook to configure behavior. All other values are optional.\n- use the <a href=\"https://kentcdodds.com/blog/how-to-give-rendering-control-to-users-with-prop-getters\" target=\"__blank\">prop getters</a> and other variables returned by this hook (below) to leverage the functionality described above with jsx elements in your react component definitions\n\nCalling the `useCioQuiz` hook returns an object with the following keys:\n\n```jsx\nconst {\n // must be used for a hooks integrations\n state: QuizReturnState, // Quiz state\n events: [{...}], // array of quiz events\n getAddToCartButtonProps: () => ({...})), // prop getter for jsx add to cart button for quiz result,\n getAddToFavoritesButtonProps: () => ({...})), // prop getter for jsx add to favorites button for quiz result,\n getCoverQuestionProps: () => ({...})), // prop getter for jsx quiz cover question,\n getHydrateQuizButtonProps: () => ({...})), // prop getter for jsx hydrate quiz button,\n getNextQuestionButtonProps: () => ({...})), // prop getter for jsx next button to traverse the quiz,\n getPreviousQuestionButtonProps: () => ({...})), // prop getter for jsx back button to traverse the quiz, \n getOpenTextInputProps: () => ({...})), // prop getter for jsx open text input,\n getSelectInputProps: () => ({...})), // prop getter for jsx select input for select type questions,\n getQuizImageProps: () => ({...})), // prop getter for jsx quiz image,\n getQuizResultButtonProps: () => ({...})), // prop getter for jsx result card click as a button,\n getQuizResultLinkProps: () => ({...})), // prop getter for jsx result card click as a link. Should be spread into <a> tags,\n getResetQuizButtonProps: () => ({...})), // prop getter for jsx reset quiz button,\n cioClient, // instance of constructorio-client-javascript\n } = useCioQuiz(args);\n```\n\n> Note: when we say `cioClient`, we are referring to an instance of the [constructorio-client-javascript](https://www.npmjs.com/package/@constructor-io/constructorio-client-javascript)\n";
|
|
6
6
|
export declare const basicDescription = "Pass an `apiKey` and a `quizId` to request questions and quiz results from Constructor's servers";
|
|
7
7
|
export declare const cioJsClientDescription = "If you are already using an instance of the `ConstructorIOClient`, you can pass a `cioJsClient` instead of an `apiKey` to request results from Constructor's servers\n\n> Note: `cioJsClient` refers to an instance of the [constructorio-client-javascript](https://www.npmjs.com/package/@constructor-io/constructorio-client-javascript)";
|
|
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
|
+
export declare const callbacksDescription = "Pass an `apiKey`, a `quizId`, and `callbacks`";
|
|
10
11
|
export declare enum RequestStates {
|
|
11
12
|
Stale = "STALE",
|
|
12
13
|
Loading = "LOADING",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { ActionAnswerQuestion } from '../../components/CioQuiz/actions';
|
|
2
2
|
import { QuizEventsReturn } from '../../types';
|
|
3
|
-
declare const useHydrateQuizLocalState: (
|
|
3
|
+
declare const useHydrateQuizLocalState: (quizSessionStorageStateKey: string, dispatchLocalState: React.Dispatch<ActionAnswerQuestion>) => QuizEventsReturn.NextQuestion;
|
|
4
4
|
export default useHydrateQuizLocalState;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ActionAnswerQuestion } from '../../components/CioQuiz/actions';
|
|
2
2
|
import { QuizAPIReducerState } from '../../components/CioQuiz/quizApiReducer';
|
|
3
3
|
import { QuizLocalReducerState } from '../../components/CioQuiz/quizLocalReducer';
|
|
4
|
-
import { QuizEventsReturn } from '../../types';
|
|
5
|
-
declare const useQuizNextClick: (quizApiState: QuizAPIReducerState, quizLocalState: QuizLocalReducerState, dispatchLocalState: React.Dispatch<ActionAnswerQuestion
|
|
4
|
+
import { QuizEventsReturn, OnQuizNextQuestion } from '../../types';
|
|
5
|
+
declare const useQuizNextClick: (quizApiState: QuizAPIReducerState, quizLocalState: QuizLocalReducerState, dispatchLocalState: React.Dispatch<ActionAnswerQuestion>, onQuizNextQuestion?: OnQuizNextQuestion) => QuizEventsReturn.NextQuestion;
|
|
6
6
|
export default useQuizNextClick;
|
|
@@ -3,15 +3,13 @@ import ConstructorIOClient from '@constructor-io/constructorio-client-javascript
|
|
|
3
3
|
import { ActionAnswerQuestion, ActionQuizAPI } from '../../components/CioQuiz/actions';
|
|
4
4
|
import { QuizAPIReducerState } from '../../components/CioQuiz/quizApiReducer';
|
|
5
5
|
import { QuizLocalReducerState } from '../../components/CioQuiz/quizLocalReducer';
|
|
6
|
-
import { IQuizProps } from '../../types';
|
|
6
|
+
import { IQuizProps, QuizSessionStorageState } from '../../types';
|
|
7
7
|
type UseQuizState = (quizOptions: IQuizProps, cioClient: ConstructorIOClient) => {
|
|
8
8
|
quizApiState: QuizAPIReducerState;
|
|
9
9
|
quizLocalState: QuizLocalReducerState;
|
|
10
10
|
dispatchApiState: React.Dispatch<ActionQuizAPI>;
|
|
11
11
|
dispatchLocalState: React.Dispatch<ActionAnswerQuestion>;
|
|
12
|
-
|
|
13
|
-
skipToResults: boolean;
|
|
14
|
-
quizStateKey: string;
|
|
12
|
+
quizSessionStorageState: QuizSessionStorageState;
|
|
15
13
|
};
|
|
16
14
|
declare const useQuizState: UseQuizState;
|
|
17
15
|
export default useQuizState;
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { ActionAnswerQuestion } from '../../components/CioQuiz/actions';
|
|
2
|
-
|
|
3
|
-
declare const useQuizLocalState: (sessionStateOptions?: SessionStateOptions) => {
|
|
2
|
+
declare const useQuizLocalState: () => {
|
|
4
3
|
quizLocalState: import("../../components/CioQuiz/quizLocalReducer").QuizLocalReducerState;
|
|
5
|
-
hasQuizStoredState: () => boolean;
|
|
6
4
|
dispatchLocalState: (action: ActionAnswerQuestion) => void;
|
|
7
|
-
skipToResults: boolean;
|
|
8
|
-
quizStateKey: string;
|
|
9
5
|
};
|
|
10
6
|
export default useQuizLocalState;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SessionStateOptions } from '../../types';
|
|
2
|
+
import { QuizLocalReducerState } from '../../components/CioQuiz/quizLocalReducer';
|
|
3
|
+
declare const useSessionStorageState: (quizLocalState?: QuizLocalReducerState, sessionStateOptions?: SessionStateOptions, enableHydration?: boolean) => {
|
|
4
|
+
skipToResults: boolean;
|
|
5
|
+
quizSessionStorageStateKey: string;
|
|
6
|
+
hasSessionStorageState: () => boolean;
|
|
7
|
+
};
|
|
8
|
+
export default useSessionStorageState;
|
package/lib/types/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { QuizResultData, NextQuestionResponse, QuizResultsResponse, Nullable, QuestionOption } from '@constructor-io/constructorio-client-javascript/lib/types';
|
|
2
|
+
import { QuizResultData, NextQuestionResponse, QuizResultsResponse, Nullable, 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';
|
|
@@ -30,6 +30,13 @@ export interface SessionStateOptions {
|
|
|
30
30
|
showSessionModalOnResults?: boolean;
|
|
31
31
|
sessionStateKey?: string;
|
|
32
32
|
}
|
|
33
|
+
export type QuestionWithAnswer = Question & {
|
|
34
|
+
answer: AnswerInput;
|
|
35
|
+
};
|
|
36
|
+
export type OnQuizNextQuestion = (question: QuestionWithAnswer) => void;
|
|
37
|
+
export interface Callbacks {
|
|
38
|
+
onQuizNextQuestion?: OnQuizNextQuestion;
|
|
39
|
+
}
|
|
33
40
|
export interface IQuizProps {
|
|
34
41
|
apiKey?: string;
|
|
35
42
|
cioJsClient?: ConstructorIOClient;
|
|
@@ -38,6 +45,8 @@ export interface IQuizProps {
|
|
|
38
45
|
resultsPageOptions: ResultsPageOptions;
|
|
39
46
|
sessionStateOptions?: SessionStateOptions;
|
|
40
47
|
primaryColor?: string;
|
|
48
|
+
enableHydration?: boolean;
|
|
49
|
+
callbacks?: Callbacks;
|
|
41
50
|
}
|
|
42
51
|
export interface QuizReturnState {
|
|
43
52
|
answers: {
|
|
@@ -50,16 +59,22 @@ export interface QuizReturnState {
|
|
|
50
59
|
sessionId?: string;
|
|
51
60
|
currentQuestion?: CurrentQuestion | undefined;
|
|
52
61
|
results?: QuizResultsResponse | undefined;
|
|
53
|
-
skipToResults: boolean;
|
|
54
62
|
selectedOptionsWithAttributes?: string[];
|
|
55
63
|
};
|
|
64
|
+
quizSessionStorageState: QuizSessionStorageState;
|
|
56
65
|
}
|
|
66
|
+
export type AnswerInput = {
|
|
67
|
+
type: InputQuestionsTypes;
|
|
68
|
+
value: string | string[];
|
|
69
|
+
};
|
|
57
70
|
export type AnswerInputState = {
|
|
58
|
-
[key: string]:
|
|
59
|
-
type: InputQuestionsTypes;
|
|
60
|
-
value: string | string[];
|
|
61
|
-
};
|
|
71
|
+
[key: string]: AnswerInput;
|
|
62
72
|
};
|
|
73
|
+
export interface QuizSessionStorageState {
|
|
74
|
+
key: string;
|
|
75
|
+
skipToResults: boolean;
|
|
76
|
+
hasSessionStorageState: () => boolean;
|
|
77
|
+
}
|
|
63
78
|
export type InputQuestionsTypes = QuestionTypes.OpenText | QuestionTypes.Cover | QuestionTypes.SingleSelect | QuestionTypes.MultipleSelect;
|
|
64
79
|
export type CurrentQuestion = NextQuestionResponse & {
|
|
65
80
|
isFirstQuestion: boolean;
|
|
@@ -78,7 +93,6 @@ export declare namespace QuizEventsReturn {
|
|
|
78
93
|
type AddToCart = (e: React.MouseEvent<HTMLElement>, result: QuizResultDataPartial, price?: number) => void;
|
|
79
94
|
type AddToFavorites = (e: React.MouseEvent<HTMLElement>, result: QuizResultDataPartial, price?: number, sendEvent?: boolean) => void;
|
|
80
95
|
type HydrateQuiz = () => void;
|
|
81
|
-
type HasSessionStorageState = () => boolean;
|
|
82
96
|
type ResetSessionStorageState = () => void;
|
|
83
97
|
}
|
|
84
98
|
export interface QuizEventsReturn {
|
|
@@ -90,7 +104,6 @@ export interface QuizEventsReturn {
|
|
|
90
104
|
addToCart: QuizEventsReturn.AddToCart;
|
|
91
105
|
addToFavorites: QuizEventsReturn.AddToFavorites;
|
|
92
106
|
hydrateQuiz: QuizEventsReturn.HydrateQuiz;
|
|
93
|
-
hasSessionStorageState: QuizEventsReturn.HasSessionStorageState;
|
|
94
107
|
resetSessionStorageState: QuizEventsReturn.ResetSessionStorageState;
|
|
95
108
|
}
|
|
96
109
|
export interface OpenTextInputProps {
|
package/lib/types/utils.d.ts
CHANGED