PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.5
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.5
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / assets / src / apps / js / frontend / quiz / store / reducer.js

reducer.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.5, at assets/src/apps/js/frontend/quiz/store/reducer.js

274 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { combineReducers } from '@wordpress/data';
2
3 const { omit, flow, isArray, chunk } = lodash;
4 const { camelCaseDashObjectKeys } = LP;
5 const { get: storageGet, set: storageSet } = LP.localStorage;
6 const STORE_DATA = {};
7
8 export const setItemStatus = ( item, status ) => {
9 const userSettings = {
10 ...item.userSettings,
11 status,
12 };
13
14 return {
15 ...item,
16 userSettings,
17 };
18 };
19
20 const updateUserQuestionAnswer = ( state, action ) => {
21 const { answered, id } = state;
22 const newAnswer = {
23 ...( answered[ action.questionId ] || {} ),
24 answered: action.answers,
25 temp: true,
26 };
27
28 if ( id ) {
29 localStorage.setItem( `LP_Quiz_${ id }_Answered`, JSON.stringify( {
30 ...state.answered,
31 [ action.questionId ]: newAnswer,
32 } ) );
33 }
34
35 return {
36 ...state,
37 answered: {
38 ...state.answered,
39 [ action.questionId ]: newAnswer,
40 },
41 };
42 };
43
44 const updateUserQuestionFibAnswer = ( state, action ) => {
45 const { id } = state;
46 const { questionId, idInput, valueInput } = action;
47
48 if ( state.answered.hasOwnProperty( questionId ) ) {
49 state.answered[ questionId ].answered[ idInput ] = valueInput;
50 } else {
51 state.answered[ action.questionId ] = { answered: { [ idInput ]: valueInput }, temp: true };
52 }
53
54 localStorage.setItem( `LP_Quiz_${ id }_Answered`, JSON.stringify( state.answered ) );
55
56 return state;
57 };
58
59 const markQuestionRendered = ( state, action ) => {
60 const {
61 questionsRendered,
62 } = state;
63
64 if ( isArray( questionsRendered ) ) {
65 questionsRendered.push( action.questionId );
66 return {
67 ...state,
68 questionsRendered: [ ...questionsRendered ],
69 };
70 }
71 return {
72 ...state,
73 questionsRendered: [ action.questionId ],
74 };
75 };
76
77 const resetCurrentPage = ( state, args ) => {
78 if ( args.currentPage ) {
79 storageSet( `Q${ state.id }.currentPage`, args.currentPage );
80 }
81
82 return {
83 ...state,
84 ...args,
85 };
86 };
87
88 const setQuestionHint = ( state, action ) => {
89 const questions = state.questions.map( ( question ) => {
90 return question.id == action.questionId ? { ...question, showHint: action.showHint } : question;
91 } );
92
93 return {
94 ...state,
95 questions: [ ...questions ],
96 };
97 };
98
99 const checkAnswer = ( state, action ) => {
100 const questions = state.questions.map( ( question ) => {
101 if ( question.id !== action.questionId ) {
102 return question;
103 }
104
105 const newArgs = {
106 explanation: action.explanation,
107 };
108
109 if ( action.options ) {
110 newArgs.options = action.options;
111 }
112
113 return { ...question, ...newArgs };
114 } );
115
116 const answered = {
117 ...state.answered,
118 [ action.questionId ]: action.result,
119 };
120
121 let newAnswered = localStorage.getItem( `LP_Quiz_${ state.id }_Answered` );
122
123 if ( newAnswered ) {
124 newAnswered = {
125 ...JSON.parse( newAnswered ),
126 ...answered,
127 };
128
129 localStorage.setItem( `LP_Quiz_${ state.id }_Answered`, JSON.stringify( newAnswered ) );
130 }
131
132 return {
133 ...state,
134 questions: [ ...questions ],
135 answered,
136 checkedQuestions: [ ...state.checkedQuestions, action.questionId ],
137 };
138 };
139
140 const submitQuiz = ( state, action ) => {
141 localStorage.removeItem( `LP_Quiz_${ state.id }_Answered` );
142
143 const questions = state.questions.map( ( question ) => {
144 const newArgs = {};
145 if ( state.reviewQuestions ) {
146 if ( action.results.questions[ question.id ]?.explanation ) {
147 newArgs.explanation = action.results.questions[ question.id ].explanation;
148 }
149
150 if ( action.results.questions[ question.id ]?.options ) {
151 newArgs.options = action.results.questions[ question.id ].options;
152 }
153 }
154
155 return { ...question, ...newArgs };
156 } );
157
158 return resetCurrentPage( state, {
159 submitting: false,
160 currentPage: 1,
161 ...action.results,
162 questions: [ ...questions ],
163 } );
164 };
165
166 const startQuizz = ( state, action ) => {
167 const successResponse = ( action.results.success ) !== undefined ? action.results.success : false;
168 const messageResponse = action.results.message || false;
169
170 const chunks = chunk( action.results.results.questionIds, state.questionsPerPage );
171 state.numPages = chunks.length;
172
173 return resetCurrentPage( state, {
174 checkedQuestions: [],
175 hintedQuestions: [],
176 mode: '',
177 currentPage: 1,
178 ...action.results.results,
179 successResponse,
180 messageResponse,
181 } );
182 };
183
184 export const userQuiz = ( state = STORE_DATA, action ) => {
185 switch ( action.type ) {
186 case 'SET_QUIZ_DATA':
187 if ( 1 > action.data.questionsPerPage ) {
188 action.data.questionsPerPage = 1;
189 }
190
191 const chunks = chunk( state.questionIds || action.data.questionIds, action.data.questionsPerPage );
192
193 action.data.numPages = chunks.length;
194 action.data.pages = chunks;
195
196 return {
197 ...state,
198 ...action.data,
199 currentPage: storageGet( `Q${ action.data.id }.currentPage` ) || action.data.currentPage,
200 };
201 case 'SUBMIT_QUIZ':
202 return {
203 ...state,
204 submitting: true,
205 };
206 case 'START_QUIZ':
207 case 'START_QUIZ_SUCCESS':
208 return startQuizz( state, action );
209 case 'SET_CURRENT_QUESTION':
210 storageSet( `Q${ state.id }.currentQuestion`, action.questionId );
211 return {
212 ...state,
213 currentQuestion: action.questionId,
214 };
215 case 'SET_CURRENT_PAGE':
216 storageSet( `Q${ state.id }.currentPage`, action.currentPage );
217
218 return {
219 ...state,
220 currentPage: action.currentPage,
221 };
222 case 'SUBMIT_QUIZ_SUCCESS':
223 return submitQuiz( state, action );
224 case 'UPDATE_USER_QUESTION_ANSWERS':
225 return state.status === 'started' ? updateUserQuestionAnswer( state, action ) : state;
226 case 'UPDATE_USER_QUESTION_FIB_ANSWERS':
227 return state.status === 'started' ? updateUserQuestionFibAnswer( state, action ) : state;
228 case 'MARK_QUESTION_RENDERED':
229 return markQuestionRendered( state, action );
230 case 'SET_QUIZ_MODE':
231 if ( action.mode == 'reviewing' ) {
232 return resetCurrentPage( state, {
233 mode: action.mode,
234 } );
235 }
236 return {
237 ...state,
238 mode: action.mode,
239 };
240 case 'SET_QUESTION_HINT':
241 return setQuestionHint( state, action );
242 case 'CHECK_ANSWER':
243 return checkAnswer( state, action );
244 case 'SEND_KEY':
245 return {
246 ...state,
247 keyPressed: action.keyPressed,
248 };
249 }
250 return state;
251 };
252
253 export const blocks = flow(
254 combineReducers,
255 ( reducer ) => ( state, action ) => {
256 return reducer( state, action );
257 },
258 ( reducer ) => ( state, action ) => {
259 return reducer( state, action );
260 },
261 ( reducer ) => ( state, action ) => {
262 return reducer( state, action );
263 }
264 )( {
265 a( state = { a: 1 }, action ) {
266 return state;
267 },
268 b( state = { b: 2 }, action ) {
269 return state;
270 },
271 } );
272
273 export default combineReducers( { blocks, userQuiz } );
274