@constructor-io/constructorio-ui-quizzes 1.3.11 → 1.4.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 +20 -20
- package/lib/cjs/components/BackButton/BackButton.js +12 -7
- package/lib/cjs/components/CTAButton/CTAButton.js +4 -3
- package/lib/cjs/components/CioQuiz/actions.js +1 -0
- package/lib/cjs/components/CioQuiz/index.js +16 -9
- package/lib/cjs/components/CioQuiz/quizApiReducer.js +15 -9
- package/lib/cjs/components/CioQuiz/quizLocalReducer.js +36 -8
- package/lib/cjs/components/ControlBar/ControlBar.js +6 -4
- package/lib/cjs/components/CoverTypeQuestion/CoverTypeQuestion.js +2 -7
- package/lib/cjs/components/OpenTextTypeQuestion/OpenTextTypeQuestion.js +4 -32
- package/lib/cjs/components/RedoButton/RedoButton.js +11 -4
- package/lib/cjs/components/ResultCard/ResultCard.js +3 -18
- package/lib/cjs/components/ResultContainer/ResultContainer.js +2 -2
- package/lib/cjs/components/ResultCtaButton/ResultCtaButton.js +8 -7
- package/lib/cjs/components/ResultHeroCard/ResultHeroCard.js +36 -0
- package/lib/cjs/components/SelectTypeQuestion/SelectTypeQuestion.js +6 -56
- package/lib/cjs/components/SessionPromptModal/SessionPromptModal.js +18 -6
- package/lib/cjs/components/ZeroResults/ZeroResults.js +5 -4
- package/lib/cjs/constants.js +40 -5
- package/lib/cjs/hooks/useConsoleErrors.js +2 -1
- package/lib/cjs/hooks/usePropsGetters/index.js +85 -0
- package/lib/cjs/hooks/usePropsGetters/useCoverQuestionProps.js +13 -0
- package/lib/cjs/hooks/usePropsGetters/useNextQuestionButtonProps.js +24 -0
- package/lib/cjs/hooks/usePropsGetters/useOpenTextInputProps.js +47 -0
- package/lib/cjs/hooks/usePropsGetters/usePreviousQuestionButtonProps.js +18 -0
- package/lib/cjs/hooks/usePropsGetters/useSelectInputProps.js +84 -0
- package/lib/cjs/hooks/useQuiz.js +14 -28
- package/lib/cjs/hooks/useQuizEvents/index.js +21 -16
- package/lib/cjs/hooks/useQuizEvents/useHydrateQuizLocalState.js +18 -0
- package/lib/cjs/hooks/useQuizEvents/useQuizAddToCart.js +2 -2
- package/lib/cjs/hooks/useQuizEvents/useQuizAnswerChangeHandler.js +48 -0
- package/lib/cjs/hooks/useQuizEvents/useQuizBackClick.js +5 -5
- package/lib/cjs/hooks/useQuizEvents/useQuizNextClick.js +13 -39
- package/lib/cjs/hooks/useQuizEvents/useQuizResetClick.js +20 -0
- package/lib/cjs/hooks/useQuizEvents/useQuizResultClick.js +2 -2
- package/lib/cjs/hooks/useQuizState/index.js +21 -0
- package/lib/cjs/hooks/{useQuizApiState.js → useQuizState/useQuizApiState.js} +15 -29
- package/lib/cjs/hooks/useQuizState/useQuizLocalState.js +30 -0
- package/lib/cjs/index.js +25 -0
- package/lib/cjs/services/index.js +3 -3
- package/lib/cjs/stories/Quiz/tests/mocks.js +69 -14
- package/lib/cjs/utils.js +23 -1
- package/lib/mjs/components/BackButton/BackButton.js +12 -7
- package/lib/mjs/components/CTAButton/CTAButton.js +4 -3
- package/lib/mjs/components/CioQuiz/actions.js +1 -0
- package/lib/mjs/components/CioQuiz/index.js +16 -9
- package/lib/mjs/components/CioQuiz/quizApiReducer.js +19 -7
- package/lib/mjs/components/CioQuiz/quizLocalReducer.js +36 -7
- package/lib/mjs/components/ControlBar/ControlBar.js +6 -4
- package/lib/mjs/components/CoverTypeQuestion/CoverTypeQuestion.js +2 -7
- package/lib/mjs/components/OpenTextTypeQuestion/OpenTextTypeQuestion.js +5 -32
- package/lib/mjs/components/RedoButton/RedoButton.js +11 -4
- package/lib/mjs/components/ResultCard/ResultCard.js +3 -15
- package/lib/mjs/components/ResultContainer/ResultContainer.js +2 -2
- package/lib/mjs/components/ResultCtaButton/ResultCtaButton.js +8 -7
- package/lib/mjs/components/ResultHeroCard/ResultHeroCard.js +31 -0
- package/lib/mjs/components/SelectTypeQuestion/SelectTypeQuestion.js +6 -55
- package/lib/mjs/components/SessionPromptModal/SessionPromptModal.js +18 -6
- package/lib/mjs/components/ZeroResults/ZeroResults.js +5 -4
- package/lib/mjs/constants.js +39 -4
- package/lib/mjs/hooks/useConsoleErrors.js +2 -1
- package/lib/mjs/hooks/usePropsGetters/index.js +72 -0
- package/lib/mjs/hooks/usePropsGetters/useCoverQuestionProps.js +10 -0
- package/lib/mjs/hooks/usePropsGetters/useNextQuestionButtonProps.js +20 -0
- package/lib/mjs/hooks/usePropsGetters/useOpenTextInputProps.js +43 -0
- package/lib/mjs/hooks/usePropsGetters/usePreviousQuestionButtonProps.js +14 -0
- package/lib/mjs/hooks/usePropsGetters/useSelectInputProps.js +79 -0
- package/lib/mjs/hooks/useQuiz.js +13 -21
- package/lib/mjs/hooks/useQuizEvents/index.js +21 -16
- package/lib/mjs/hooks/useQuizEvents/useHydrateQuizLocalState.js +16 -0
- package/lib/mjs/hooks/useQuizEvents/useQuizAddToCart.js +2 -2
- package/lib/mjs/hooks/useQuizEvents/useQuizAnswerChangeHandler.js +45 -0
- package/lib/mjs/hooks/useQuizEvents/useQuizBackClick.js +5 -5
- package/lib/mjs/hooks/useQuizEvents/useQuizNextClick.js +12 -38
- package/lib/mjs/hooks/useQuizEvents/useQuizResetClick.js +18 -0
- package/lib/mjs/hooks/useQuizEvents/useQuizResultClick.js +2 -2
- package/lib/mjs/hooks/useQuizState/index.js +18 -0
- package/lib/mjs/hooks/{useQuizApiState.js → useQuizState/useQuizApiState.js} +15 -28
- package/lib/mjs/hooks/useQuizState/useQuizLocalState.js +26 -0
- package/lib/mjs/index.js +14 -0
- package/lib/mjs/services/index.js +1 -1
- package/lib/mjs/stories/Quiz/tests/mocks.js +75 -12
- package/lib/mjs/utils.js +17 -0
- package/lib/styles.css +13 -2
- package/lib/types/components/BackButton/BackButton.d.ts +2 -2
- package/lib/types/components/CTAButton/CTAButton.d.ts +1 -0
- package/lib/types/components/CioQuiz/actions.d.ts +5 -6
- package/lib/types/components/CioQuiz/context.d.ts +13 -6
- package/lib/types/components/CioQuiz/quizApiReducer.d.ts +2 -2
- package/lib/types/components/CioQuiz/quizLocalReducer.d.ts +3 -5
- package/lib/types/components/ControlBar/ControlBar.d.ts +0 -4
- package/lib/types/components/OpenTextTypeQuestion/OpenTextTypeQuestion.d.ts +2 -6
- package/lib/types/components/RedoButton/RedoButton.d.ts +1 -1
- package/lib/types/components/ResultCtaButton/ResultCtaButton.d.ts +1 -2
- package/lib/types/components/ResultHeroCard/ResultHeroCard.d.ts +7 -0
- package/lib/types/components/SelectTypeQuestion/SelectTypeQuestion.d.ts +3 -0
- package/lib/types/components/ZeroResults/ZeroResults.d.ts +2 -5
- package/lib/types/constants.d.ts +5 -4
- package/lib/types/hooks/useConsoleErrors.d.ts +2 -2
- package/lib/types/hooks/usePropsGetters/index.d.ts +18 -0
- package/lib/types/hooks/usePropsGetters/useCoverQuestionProps.d.ts +2 -0
- package/lib/types/hooks/usePropsGetters/useNextQuestionButtonProps.d.ts +4 -0
- package/lib/types/hooks/usePropsGetters/useOpenTextInputProps.d.ts +2 -0
- package/lib/types/hooks/usePropsGetters/usePreviousQuestionButtonProps.d.ts +3 -0
- package/lib/types/hooks/usePropsGetters/useSelectInputProps.d.ts +2 -0
- package/lib/types/hooks/useQuizEvents/index.d.ts +4 -16
- package/lib/types/hooks/useQuizEvents/useHydrateQuizLocalState.d.ts +4 -0
- package/lib/types/hooks/useQuizEvents/useQuizAnswerChangeHandler.d.ts +5 -0
- package/lib/types/hooks/useQuizEvents/useQuizBackClick.d.ts +2 -1
- package/lib/types/hooks/useQuizEvents/useQuizNextClick.d.ts +2 -1
- package/lib/types/hooks/useQuizEvents/useQuizResetClick.d.ts +4 -0
- package/lib/types/hooks/useQuizState/index.d.ts +16 -0
- package/lib/types/hooks/useQuizState/useQuizApiState.d.ts +12 -0
- package/lib/types/hooks/useQuizState/useQuizLocalState.d.ts +8 -0
- package/lib/types/index.d.ts +11 -0
- package/lib/types/services/index.d.ts +1 -1
- package/lib/types/stories/Quiz/tests/mocks.d.ts +5 -2
- package/lib/types/types.d.ts +121 -11
- package/lib/types/utils.d.ts +4 -0
- package/package.json +1 -1
- package/lib/cjs/hooks/useQuizLocalState.js +0 -54
- package/lib/mjs/hooks/useQuizLocalState.js +0 -48
- package/lib/types/hooks/useQuizApiState.d.ts +0 -11
- package/lib/types/hooks/useQuizLocalState.d.ts +0 -10
package/lib/types/types.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { QuizResultData, NextQuestionResponse, QuizResultsResponse } from '@constructor-io/constructorio-client-javascript/lib/types';
|
|
2
|
+
import { QuizResultData, NextQuestionResponse, QuizResultsResponse, Nullable, QuestionOption } 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
|
+
import { QuestionTypes } from './components/CioQuiz/actions';
|
|
5
6
|
export type { QuestionOption, QuestionImages, Question, NextQuestionResponse, QuizResultsResponse, BrowseResultData, BrowseRequestType, QuizzesParameters, QuizzesResultsParameters, } from '@constructor-io/constructorio-client-javascript/lib/types';
|
|
6
7
|
export type QuizResultDataPartial = Partial<QuizResultData>;
|
|
7
8
|
export interface ResultsProps {
|
|
@@ -34,40 +35,137 @@ export interface IQuizProps {
|
|
|
34
35
|
}
|
|
35
36
|
export interface QuizReturnState {
|
|
36
37
|
answers: {
|
|
37
|
-
inputs:
|
|
38
|
+
inputs: AnswerInputState;
|
|
38
39
|
isLastAnswer: boolean;
|
|
39
40
|
};
|
|
40
41
|
quiz: {
|
|
41
42
|
requestState: RequestStates;
|
|
42
43
|
versionId?: string;
|
|
43
44
|
sessionId?: string;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
results?: QuizResultsResponse;
|
|
45
|
+
currentQuestion?: CurrentQuestion | undefined;
|
|
46
|
+
results?: QuizResultsResponse | undefined;
|
|
47
47
|
resultsFilters?: string[];
|
|
48
|
-
isFirstQuestion?: boolean;
|
|
49
48
|
};
|
|
50
49
|
}
|
|
50
|
+
export type AnswerInputState = {
|
|
51
|
+
[key: string]: {
|
|
52
|
+
type: InputQuestionsTypes;
|
|
53
|
+
value: string | string[];
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
export type InputQuestionsTypes = QuestionTypes.OpenText | QuestionTypes.Cover | QuestionTypes.SingleSelect | QuestionTypes.MultipleSelect;
|
|
57
|
+
export type CurrentQuestion = NextQuestionResponse & {
|
|
58
|
+
isFirstQuestion: boolean;
|
|
59
|
+
isOpenQuestion: boolean;
|
|
60
|
+
isCoverQuestion: boolean;
|
|
61
|
+
isSingleQuestion: boolean;
|
|
62
|
+
isMultipleQuestion: boolean;
|
|
63
|
+
isSelectQuestion: boolean;
|
|
64
|
+
};
|
|
51
65
|
export declare namespace QuizEventsReturn {
|
|
52
|
-
type
|
|
66
|
+
type QuizAnswerChanged = (payload?: string | string[]) => void;
|
|
67
|
+
type NextQuestion = () => void;
|
|
53
68
|
type PreviousQuestion = () => void;
|
|
54
69
|
type ResetQuiz = () => void;
|
|
55
70
|
type ResultClick = (result: QuizResultDataPartial, position: number) => void;
|
|
56
71
|
type AddToCart = (e: React.MouseEvent<HTMLElement>, result: QuizResultDataPartial, price?: number) => void;
|
|
57
72
|
type HydrateQuiz = () => void;
|
|
58
|
-
type
|
|
59
|
-
type
|
|
73
|
+
type HasSessionStorageState = () => boolean;
|
|
74
|
+
type ResetSessionStorageState = () => void;
|
|
60
75
|
}
|
|
61
76
|
export interface QuizEventsReturn {
|
|
62
77
|
nextQuestion: QuizEventsReturn.NextQuestion;
|
|
78
|
+
quizAnswerChanged: QuizEventsReturn.QuizAnswerChanged;
|
|
63
79
|
previousQuestion: QuizEventsReturn.PreviousQuestion;
|
|
64
80
|
resetQuiz: QuizEventsReturn.ResetQuiz;
|
|
65
81
|
resultClick: QuizEventsReturn.ResultClick;
|
|
66
82
|
addToCart: QuizEventsReturn.AddToCart;
|
|
67
83
|
hydrateQuiz: QuizEventsReturn.HydrateQuiz;
|
|
68
|
-
|
|
69
|
-
|
|
84
|
+
hasSessionStorageState: QuizEventsReturn.HasSessionStorageState;
|
|
85
|
+
resetSessionStorageState: QuizEventsReturn.ResetSessionStorageState;
|
|
86
|
+
}
|
|
87
|
+
export interface OpenTextInputProps {
|
|
88
|
+
className: string;
|
|
89
|
+
placeholder: string;
|
|
90
|
+
value: string;
|
|
91
|
+
onChange: React.ChangeEventHandler<HTMLInputElement>;
|
|
92
|
+
onKeyDown: React.KeyboardEventHandler<HTMLInputElement>;
|
|
93
|
+
}
|
|
94
|
+
export interface CoverQuestionProps {
|
|
95
|
+
}
|
|
96
|
+
export interface NextQuestionButtonProps {
|
|
97
|
+
className: string;
|
|
98
|
+
type: 'submit' | 'reset' | 'button' | undefined;
|
|
99
|
+
disabled?: boolean;
|
|
100
|
+
onClick: React.MouseEventHandler<HTMLElement>;
|
|
101
|
+
}
|
|
102
|
+
export interface PreviousQuestionButtonProps {
|
|
103
|
+
className: string;
|
|
104
|
+
type: 'submit' | 'reset' | 'button' | undefined;
|
|
105
|
+
onClick: React.MouseEventHandler<HTMLElement>;
|
|
106
|
+
style?: Record<string, string>;
|
|
107
|
+
}
|
|
108
|
+
export interface ResetQuizButtonProps {
|
|
109
|
+
className: string;
|
|
110
|
+
type: 'submit' | 'reset' | 'button' | undefined;
|
|
111
|
+
onClick: React.MouseEventHandler<HTMLElement>;
|
|
112
|
+
style?: Record<string, string>;
|
|
113
|
+
}
|
|
114
|
+
export interface HydrateQuizButtonProps {
|
|
115
|
+
className: string;
|
|
116
|
+
type: 'submit' | 'reset' | 'button' | undefined;
|
|
117
|
+
onClick: React.MouseEventHandler<HTMLElement>;
|
|
118
|
+
style?: Record<string, string>;
|
|
119
|
+
}
|
|
120
|
+
export interface AddToCartButtonProps {
|
|
121
|
+
className: string;
|
|
122
|
+
type: 'submit' | 'reset' | 'button' | undefined;
|
|
123
|
+
onClick: React.MouseEventHandler<HTMLElement>;
|
|
124
|
+
style?: Record<string, string>;
|
|
125
|
+
}
|
|
126
|
+
export interface QuizImageProps {
|
|
127
|
+
className?: string;
|
|
128
|
+
src?: Nullable<string>;
|
|
129
|
+
alt?: Nullable<string>;
|
|
130
|
+
}
|
|
131
|
+
export interface QuizResultPropsLink {
|
|
132
|
+
href?: string;
|
|
133
|
+
onClick: React.MouseEventHandler<HTMLElement>;
|
|
134
|
+
onKeyDown: React.KeyboardEventHandler<HTMLElement>;
|
|
135
|
+
}
|
|
136
|
+
export interface QuizResultPropsButton {
|
|
137
|
+
className: string;
|
|
138
|
+
role: 'button';
|
|
139
|
+
tabIndex: number;
|
|
140
|
+
onClick: React.MouseEventHandler<HTMLElement>;
|
|
141
|
+
onKeyDown: React.KeyboardEventHandler<HTMLElement>;
|
|
142
|
+
}
|
|
143
|
+
export type QuizResultProps = QuizResultPropsLink | QuizResultPropsButton;
|
|
144
|
+
export interface SelectInputProps {
|
|
145
|
+
className: string;
|
|
146
|
+
onClick: React.MouseEventHandler<HTMLElement>;
|
|
147
|
+
onKeyDown: React.KeyboardEventHandler<HTMLElement>;
|
|
148
|
+
role: 'button';
|
|
149
|
+
tabIndex: number;
|
|
150
|
+
key: number;
|
|
151
|
+
}
|
|
152
|
+
export type GetOpenTextInputProps = () => OpenTextInputProps;
|
|
153
|
+
export type GetCoverQuestionProps = () => CoverQuestionProps;
|
|
154
|
+
export type GetSelectInputProps = (option: QuestionOption) => SelectInputProps;
|
|
155
|
+
export type GetNextQuestionButtonProps = () => NextQuestionButtonProps;
|
|
156
|
+
export type GetPreviousQuestionButtonProps = () => PreviousQuestionButtonProps;
|
|
157
|
+
export type GetResetQuizButtonProps = () => ResetQuizButtonProps;
|
|
158
|
+
export type GetHydrateQuizButtonProps = () => HydrateQuizButtonProps;
|
|
159
|
+
export type GetAddToCartButtonProps = (result: QuizResultDataPartial, price?: number) => AddToCartButtonProps;
|
|
160
|
+
export type GetQuizImageProps = () => QuizImageProps;
|
|
161
|
+
export type GetSelectQuestionImageProps = (option: QuestionOption) => QuizImageProps;
|
|
162
|
+
export interface QuizResultOptions<T = 'button' | 'link'> {
|
|
163
|
+
result: QuizResultDataPartial;
|
|
164
|
+
position: number;
|
|
165
|
+
type?: T;
|
|
70
166
|
}
|
|
167
|
+
export type GetQuizResultButtonProps = (options: QuizResultOptions<'button'>) => QuizResultPropsButton;
|
|
168
|
+
export type GetQuizResultLinkProps = (options: QuizResultOptions<'link'>) => QuizResultPropsLink;
|
|
71
169
|
export interface PrimaryColorStyles {
|
|
72
170
|
'--primary-color-h': string;
|
|
73
171
|
'--primary-color-s': string;
|
|
@@ -77,6 +175,18 @@ export interface UseQuizReturn {
|
|
|
77
175
|
cioClient?: ConstructorIOClient;
|
|
78
176
|
state: QuizReturnState;
|
|
79
177
|
events: QuizEventsReturn;
|
|
178
|
+
getOpenTextInputProps: GetOpenTextInputProps;
|
|
179
|
+
getNextQuestionButtonProps: GetNextQuestionButtonProps;
|
|
180
|
+
getPreviousQuestionButtonProps: GetPreviousQuestionButtonProps;
|
|
181
|
+
getQuizImageProps: GetQuizImageProps;
|
|
182
|
+
getSelectQuestionImageProps: GetSelectQuestionImageProps;
|
|
183
|
+
getSelectInputProps: GetSelectInputProps;
|
|
184
|
+
getCoverQuestionProps: GetCoverQuestionProps;
|
|
185
|
+
getResetQuizButtonProps: GetResetQuizButtonProps;
|
|
186
|
+
getHydrateQuizButtonProps: GetHydrateQuizButtonProps;
|
|
187
|
+
getAddToCartButtonProps: GetAddToCartButtonProps;
|
|
188
|
+
getQuizResultButtonProps: GetQuizResultButtonProps;
|
|
189
|
+
getQuizResultLinkProps: GetQuizResultLinkProps;
|
|
80
190
|
primaryColorStyles: PrimaryColorStyles;
|
|
81
191
|
}
|
|
82
192
|
export type UseQuiz = (quizProps: IQuizProps) => UseQuizReturn;
|
package/lib/types/utils.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { FilterExpression } from '@constructor-io/constructorio-client-javascript/lib/types';
|
|
2
2
|
import { QuestionTypes } from './components/CioQuiz/actions';
|
|
3
|
+
import { QuizLocalReducerState } from './components/CioQuiz/quizLocalReducer';
|
|
3
4
|
import { PrimaryColorStyles, QuestionImages } from './types';
|
|
4
5
|
export declare const renderImages: (images: Partial<QuestionImages>, cssClasses?: string) => JSX.Element | "";
|
|
5
6
|
export declare const getStoryParams: (storyCode: any, templateCode: any, importCode: any) => {
|
|
@@ -37,6 +38,9 @@ export declare const getQuestionTypes: (questionType?: `${QuestionTypes}`) => {
|
|
|
37
38
|
export declare function getPreferredColorScheme(): string;
|
|
38
39
|
export declare function isFunction(fn: any): boolean;
|
|
39
40
|
export declare const getFilterValuesFromExpression: (exp: FilterExpression | null) => string[];
|
|
41
|
+
export declare const getStateFromSessionStorage: (quizStateKey: string) => QuizLocalReducerState | null;
|
|
42
|
+
export declare const resetQuizSessionStorageState: (quizStateKey: string) => () => void;
|
|
43
|
+
export declare const logger: (action: any) => void;
|
|
40
44
|
export declare function sleep(ms: any): Promise<unknown>;
|
|
41
45
|
export declare function rgbToHsl(r: number, g: number, b: number): number[];
|
|
42
46
|
export declare function convertPrimaryColorsToString(primaryColorStyles: PrimaryColorStyles): string;
|
package/package.json
CHANGED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
const actions_1 = require("../components/CioQuiz/actions");
|
|
6
|
-
const quizLocalReducer_1 = tslib_1.__importStar(require("../components/CioQuiz/quizLocalReducer"));
|
|
7
|
-
const constants_1 = require("../constants");
|
|
8
|
-
const useQuizLocalState = (sessionStateKey) => {
|
|
9
|
-
const [quizLocalState, dispatch] = (0, react_1.useReducer)(quizLocalReducer_1.default, quizLocalReducer_1.initialState);
|
|
10
|
-
const quizStateKey = sessionStateKey || constants_1.quizSessionStateKey;
|
|
11
|
-
const getStateFromSessionStorage = () => {
|
|
12
|
-
var _a;
|
|
13
|
-
const state = (_a = window === null || window === void 0 ? void 0 : window.sessionStorage) === null || _a === void 0 ? void 0 : _a.getItem(quizStateKey);
|
|
14
|
-
if (state) {
|
|
15
|
-
return JSON.parse(state);
|
|
16
|
-
}
|
|
17
|
-
return null;
|
|
18
|
-
};
|
|
19
|
-
(0, react_1.useEffect)(() => {
|
|
20
|
-
var _a, _b;
|
|
21
|
-
// don't save state if initial state
|
|
22
|
-
if ((_a = quizLocalState === null || quizLocalState === void 0 ? void 0 : quizLocalState.answers) === null || _a === void 0 ? void 0 : _a.length) {
|
|
23
|
-
(_b = window === null || window === void 0 ? void 0 : window.sessionStorage) === null || _b === void 0 ? void 0 : _b.setItem(quizStateKey, JSON.stringify(quizLocalState));
|
|
24
|
-
}
|
|
25
|
-
}, [quizLocalState, quizStateKey]);
|
|
26
|
-
const resetQuizLocalState = () => {
|
|
27
|
-
dispatch({
|
|
28
|
-
type: actions_1.QuestionTypes.Reset,
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
const hydrateQuizLocalState = () => {
|
|
32
|
-
const quizState = getStateFromSessionStorage();
|
|
33
|
-
if (quizState) {
|
|
34
|
-
dispatch({
|
|
35
|
-
type: actions_1.QuestionTypes.Hydrate,
|
|
36
|
-
payload: quizState,
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
const resetQuizStoredState = () => {
|
|
41
|
-
var _a;
|
|
42
|
-
(_a = window === null || window === void 0 ? void 0 : window.sessionStorage) === null || _a === void 0 ? void 0 : _a.removeItem(quizStateKey);
|
|
43
|
-
};
|
|
44
|
-
const hasQuizStoredState = () => getStateFromSessionStorage() !== null;
|
|
45
|
-
return {
|
|
46
|
-
quizLocalState,
|
|
47
|
-
resetQuizLocalState,
|
|
48
|
-
hydrateQuizLocalState,
|
|
49
|
-
hasQuizStoredState,
|
|
50
|
-
resetQuizStoredState,
|
|
51
|
-
dispatchLocalState: dispatch,
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
exports.default = useQuizLocalState;
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { useEffect, useReducer } from 'react';
|
|
2
|
-
import { QuestionTypes } from '../components/CioQuiz/actions';
|
|
3
|
-
import quizLocalReducer, { initialState, } from '../components/CioQuiz/quizLocalReducer';
|
|
4
|
-
import { quizSessionStateKey } from '../constants';
|
|
5
|
-
const useQuizLocalState = (sessionStateKey) => {
|
|
6
|
-
const [quizLocalState, dispatch] = useReducer(quizLocalReducer, initialState);
|
|
7
|
-
const quizStateKey = sessionStateKey || quizSessionStateKey;
|
|
8
|
-
const getStateFromSessionStorage = () => {
|
|
9
|
-
const state = window?.sessionStorage?.getItem(quizStateKey);
|
|
10
|
-
if (state) {
|
|
11
|
-
return JSON.parse(state);
|
|
12
|
-
}
|
|
13
|
-
return null;
|
|
14
|
-
};
|
|
15
|
-
useEffect(() => {
|
|
16
|
-
// don't save state if initial state
|
|
17
|
-
if (quizLocalState?.answers?.length) {
|
|
18
|
-
window?.sessionStorage?.setItem(quizStateKey, JSON.stringify(quizLocalState));
|
|
19
|
-
}
|
|
20
|
-
}, [quizLocalState, quizStateKey]);
|
|
21
|
-
const resetQuizLocalState = () => {
|
|
22
|
-
dispatch({
|
|
23
|
-
type: QuestionTypes.Reset,
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
const hydrateQuizLocalState = () => {
|
|
27
|
-
const quizState = getStateFromSessionStorage();
|
|
28
|
-
if (quizState) {
|
|
29
|
-
dispatch({
|
|
30
|
-
type: QuestionTypes.Hydrate,
|
|
31
|
-
payload: quizState,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
const resetQuizStoredState = () => {
|
|
36
|
-
window?.sessionStorage?.removeItem(quizStateKey);
|
|
37
|
-
};
|
|
38
|
-
const hasQuizStoredState = () => getStateFromSessionStorage() !== null;
|
|
39
|
-
return {
|
|
40
|
-
quizLocalState,
|
|
41
|
-
resetQuizLocalState,
|
|
42
|
-
hydrateQuizLocalState,
|
|
43
|
-
hasQuizStoredState,
|
|
44
|
-
resetQuizStoredState,
|
|
45
|
-
dispatchLocalState: dispatch,
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
export default useQuizLocalState;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import ConstructorIOClient from '@constructor-io/constructorio-client-javascript';
|
|
2
|
-
import { ActionAnswerQuestion } from '../components/CioQuiz/actions';
|
|
3
|
-
import { QuizLocalReducerState } from '../components/CioQuiz/quizLocalReducer';
|
|
4
|
-
import { ResultsPageOptions } from '../types';
|
|
5
|
-
declare const useFetchQuiz: (quizId: string, quizLocalState: QuizLocalReducerState, dispatchLocalState: React.Dispatch<ActionAnswerQuestion>, resultsPageOptions: ResultsPageOptions, quizVersionIdProp: string | undefined, cioClient: ConstructorIOClient) => {
|
|
6
|
-
cioClient: ConstructorIOClient;
|
|
7
|
-
quizApiState: import("../components/CioQuiz/quizApiReducer").QuizAPIReducerState;
|
|
8
|
-
isFirstQuestion: boolean;
|
|
9
|
-
resetQuizApiState: () => void;
|
|
10
|
-
};
|
|
11
|
-
export default useFetchQuiz;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { QuizLocalReducerState } from '../components/CioQuiz/quizLocalReducer';
|
|
2
|
-
declare const useQuizLocalState: (sessionStateKey?: string) => {
|
|
3
|
-
quizLocalState: QuizLocalReducerState;
|
|
4
|
-
resetQuizLocalState: () => void;
|
|
5
|
-
hydrateQuizLocalState: () => void;
|
|
6
|
-
hasQuizStoredState: () => boolean;
|
|
7
|
-
resetQuizStoredState: () => void;
|
|
8
|
-
dispatchLocalState: import("react").Dispatch<import("../components/CioQuiz/actions").ActionAnswerQuestion>;
|
|
9
|
-
};
|
|
10
|
-
export default useQuizLocalState;
|