| 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 markQuestionRendered = ( state, action ) => { |
| 45 |
const { |
| 46 |
questionsRendered, |
| 47 |
} = state; |
| 48 |
|
| 49 |
if ( isArray( questionsRendered ) ) { |
| 50 |
questionsRendered.push( action.questionId ); |
| 51 |
return { |
| 52 |
...state, |
| 53 |
questionsRendered: [ ...questionsRendered ], |
| 54 |
}; |
| 55 |
} |
| 56 |
return { |
| 57 |
...state, |
| 58 |
questionsRendered: [ action.questionId ], |
| 59 |
}; |
| 60 |
}; |
| 61 |
|
| 62 |
const resetCurrentPage = ( state, args ) => { |
| 63 |
if ( args.currentPage ) { |
| 64 |
storageSet( `Q${ state.id }.currentPage`, args.currentPage ); |
| 65 |
} |
| 66 |
|
| 67 |
return { |
| 68 |
...state, |
| 69 |
...args, |
| 70 |
}; |
| 71 |
}; |
| 72 |
|
| 73 |
const setQuestionHint = ( state, action ) => { |
| 74 |
const questions = state.questions.map( ( question ) => { |
| 75 |
return question.id == action.questionId ? { ...question, showHint: action.showHint } : question; |
| 76 |
} ); |
| 77 |
|
| 78 |
return { |
| 79 |
...state, |
| 80 |
questions: [ ...questions ], |
| 81 |
}; |
| 82 |
}; |
| 83 |
|
| 84 |
const checkAnswer = ( state, action ) => { |
| 85 |
const questions = state.questions.map( ( question ) => { |
| 86 |
if ( question.id !== action.questionId ) { |
| 87 |
return question; |
| 88 |
} |
| 89 |
|
| 90 |
const newArgs = { |
| 91 |
explanation: action.explanation, |
| 92 |
}; |
| 93 |
|
| 94 |
if ( action.options ) { |
| 95 |
newArgs.options = action.options; |
| 96 |
} |
| 97 |
|
| 98 |
return { ...question, ...newArgs }; |
| 99 |
} ); |
| 100 |
|
| 101 |
const answered = { |
| 102 |
...state.answered, |
| 103 |
[ action.questionId ]: action.result, |
| 104 |
}; |
| 105 |
|
| 106 |
let newAnswered = localStorage.getItem( `LP_Quiz_${ state.id }_Answered` ); |
| 107 |
|
| 108 |
if ( newAnswered ) { |
| 109 |
newAnswered = { |
| 110 |
...JSON.parse( newAnswered ), |
| 111 |
...answered, |
| 112 |
} |
| 113 |
|
| 114 |
localStorage.setItem( `LP_Quiz_${ state.id }_Answered`, JSON.stringify( newAnswered ) ); |
| 115 |
} |
| 116 |
|
| 117 |
return { |
| 118 |
...state, |
| 119 |
questions: [ ...questions ], |
| 120 |
answered: answered, |
| 121 |
checkedQuestions: [ ...state.checkedQuestions, action.questionId ], |
| 122 |
}; |
| 123 |
}; |
| 124 |
|
| 125 |
const submitQuiz = ( state, action ) => { |
| 126 |
localStorage.removeItem( `LP_Quiz_${ state.id }_Answered` ); |
| 127 |
|
| 128 |
const questions = state.questions.map( ( question ) => { |
| 129 |
const newArgs = {}; |
| 130 |
if ( state.reviewQuestions ) { |
| 131 |
if ( action.results.questions[ question.id ]?.explanation ) { |
| 132 |
newArgs.explanation = action.results.questions[ question.id ].explanation; |
| 133 |
} |
| 134 |
|
| 135 |
if ( action.results.questions[ question.id ]?.options ) { |
| 136 |
newArgs.options = action.results.questions[ question.id ].options; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return { ...question, ...newArgs }; |
| 141 |
} ); |
| 142 |
|
| 143 |
return resetCurrentPage( state, { |
| 144 |
submitting: false, |
| 145 |
currentPage: 1, |
| 146 |
...action.results, |
| 147 |
questions: [ ...questions ], |
| 148 |
} ); |
| 149 |
}; |
| 150 |
|
| 151 |
const startQuizz = ( state, action ) => { |
| 152 |
const successResponse = ( action.results.success ) !== undefined ? action.results.success : false; |
| 153 |
const messageResponse = action.results.message || false; |
| 154 |
|
| 155 |
return resetCurrentPage( state, { |
| 156 |
checkedQuestions: [], |
| 157 |
hintedQuestions: [], |
| 158 |
mode: '', |
| 159 |
currentPage: 1, |
| 160 |
...action.results.results, |
| 161 |
successResponse, |
| 162 |
messageResponse, |
| 163 |
} ); |
| 164 |
}; |
| 165 |
|
| 166 |
export const userQuiz = ( state = STORE_DATA, action ) => { |
| 167 |
switch ( action.type ) { |
| 168 |
case 'SET_QUIZ_DATA': |
| 169 |
if ( 1 > action.data.questionsPerPage ) { |
| 170 |
action.data.questionsPerPage = 1; |
| 171 |
} |
| 172 |
|
| 173 |
const chunks = chunk( state.questionIds || action.data.questionIds, action.data.questionsPerPage ); |
| 174 |
|
| 175 |
action.data.numPages = chunks.length; |
| 176 |
action.data.pages = chunks; |
| 177 |
|
| 178 |
return { |
| 179 |
...state, |
| 180 |
...action.data, |
| 181 |
currentPage: storageGet( `Q${ action.data.id }.currentPage` ) || action.data.currentPage, |
| 182 |
}; |
| 183 |
case 'SUBMIT_QUIZ': |
| 184 |
return { |
| 185 |
...state, |
| 186 |
submitting: true, |
| 187 |
}; |
| 188 |
case 'START_QUIZ': |
| 189 |
case 'START_QUIZ_SUCCESS': |
| 190 |
return startQuizz( state, action ); |
| 191 |
case 'SET_CURRENT_QUESTION': |
| 192 |
storageSet( `Q${ state.id }.currentQuestion`, action.questionId ); |
| 193 |
return { |
| 194 |
...state, |
| 195 |
currentQuestion: action.questionId, |
| 196 |
}; |
| 197 |
case 'SET_CURRENT_PAGE': |
| 198 |
storageSet( `Q${ state.id }.currentPage`, action.currentPage ); |
| 199 |
|
| 200 |
return { |
| 201 |
...state, |
| 202 |
currentPage: action.currentPage, |
| 203 |
}; |
| 204 |
case 'SUBMIT_QUIZ_SUCCESS': |
| 205 |
return submitQuiz( state, action ); |
| 206 |
case 'UPDATE_USER_QUESTION_ANSWERS': |
| 207 |
return state.status === 'started' ? updateUserQuestionAnswer( state, action ) : state; |
| 208 |
case 'MARK_QUESTION_RENDERED': |
| 209 |
return markQuestionRendered( state, action ); |
| 210 |
case 'SET_QUIZ_MODE': |
| 211 |
if ( action.mode == 'reviewing' ) { |
| 212 |
return resetCurrentPage( state, { |
| 213 |
mode: action.mode, |
| 214 |
} ); |
| 215 |
} |
| 216 |
return { |
| 217 |
...state, |
| 218 |
mode: action.mode, |
| 219 |
}; |
| 220 |
case 'SET_QUESTION_HINT': |
| 221 |
return setQuestionHint( state, action ); |
| 222 |
case 'CHECK_ANSWER': |
| 223 |
return checkAnswer( state, action ); |
| 224 |
case 'SEND_KEY': |
| 225 |
return { |
| 226 |
...state, |
| 227 |
keyPressed: action.keyPressed, |
| 228 |
}; |
| 229 |
} |
| 230 |
return state; |
| 231 |
}; |
| 232 |
|
| 233 |
export const blocks = flow( |
| 234 |
combineReducers, |
| 235 |
( reducer ) => ( state, action ) => { |
| 236 |
return reducer( state, action ); |
| 237 |
}, |
| 238 |
( reducer ) => ( state, action ) => { |
| 239 |
return reducer( state, action ); |
| 240 |
}, |
| 241 |
( reducer ) => ( state, action ) => { |
| 242 |
return reducer( state, action ); |
| 243 |
} |
| 244 |
)( { |
| 245 |
a( state = { a: 1 }, action ) { |
| 246 |
return state; |
| 247 |
}, |
| 248 |
b( state = { b: 2 }, action ) { |
| 249 |
return state; |
| 250 |
}, |
| 251 |
} ); |
| 252 |
|
| 253 |
export default combineReducers( { blocks, userQuiz } ); |
| 254 |
|