Skip to content

Commit 6287fba

Browse files
committed
Clean up
1 parent 536e1ed commit 6287fba

File tree

1 file changed

+37
-71
lines changed

1 file changed

+37
-71
lines changed

client/util/redux/testReducer.js

Lines changed: 37 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,16 @@ export default (state = initialState, action) => {
234234
}
235235
case 'GET_READING_TEST_QUESTIONS_SUCCESS': {
236236
const {
237-
question_list,
238-
session_id,
239-
question_set_dict,
237+
question_list: questionList,
238+
session_id: readingTestSessionId,
239+
question_set_dict: readingTestSetDict,
240240
all_cycles: allCycles,
241241
current_cycle: currentCycle,
242242
previous_status: previousStatus,
243243
} = response
244244

245-
console.log('previousStatus: ', previousStatus)
246-
247245
// Split questions by set
248-
const questionsBySet = question_list.reduce((acc, question) => {
246+
const questionsBySet = questionList.reduce((acc, question) => {
249247
const { set } = question
250248
if (!acc[set]) {
251249
acc[set] = { seen: [], unseen: [] }
@@ -258,95 +256,63 @@ export default (state = initialState, action) => {
258256
return acc
259257
}, {})
260258

261-
// Sort sets by set number
262-
// const sortedSets = Object.keys(questionsBySet).sort((a, b) => parseInt(a) - parseInt(b));
263-
264-
// Find the current question
265-
let tmpcurrentReadingTestQuestion = null
266-
let currentSet = null
267-
let tmpcurrentQuestionIdxinSet = -1
268-
let tmpcurrentReadingQuestionIndex = 0
269-
let tmpreadingSetLength = 0
270-
// let tempreadingTestQuestions = []
271-
272-
/* let tmp_reading_question_idx = 0
273-
let current_question_is_set = false
274-
for (const set of sortedSets) {
275-
const { seen, unseen } = questionsBySet[set];
276-
tempreadingTestQuestions = tempreadingTestQuestions.concat(seen)
277-
tempreadingTestQuestions = tempreadingTestQuestions.concat(unseen)
278-
if (unseen.length > 0 && current_question_is_set != true) {
279-
tmpcurrentReadingTestQuestion = unseen[0];
280-
currentSet = set;
281-
tmpcurrentQuestionIdxinSet = seen.length;
282-
tmpcurrentReadingQuestionIndex = tmp_reading_question_idx + tmpcurrentQuestionIdxinSet;
283-
tmpreadingSetLength = unseen.length + seen.length;
284-
current_question_is_set = true
285-
} else {
286-
tmp_reading_question_idx = tempreadingTestQuestions.length;
287-
}
288-
} */
289-
290-
question_list.forEach((q, i) => {
291-
if (q.seen === true) {
292-
tmpcurrentReadingQuestionIndex = i
293-
}
294-
})
295-
296-
/* if (tmpcurrentReadingQuestionIndex === -1) {
297-
tmpcurrentReadingTestQuestion = question_list[0]
298-
console.log('tmpcurrentReadingTestQuestion 1: ', tmpcurrentReadingTestQuestion)
299-
} else {
300-
tmpcurrentReadingTestQuestion = question_list[tmpcurrentReadingQuestionIndex]
301-
} */
302-
303-
tmpcurrentReadingTestQuestion = question_list[tmpcurrentReadingQuestionIndex]
259+
// Find the index of the last seen question
260+
const currentReadingQuestionIndex = questionList.reduce((acc, q, i) => {
261+
return q.seen ? i : acc
262+
}, 0)
304263

305-
currentSet = Number(tmpcurrentReadingTestQuestion.set)
306-
tmpcurrentQuestionIdxinSet = questionsBySet[currentSet].seen.length
307-
tmpreadingSetLength =
264+
const currentReadingTestQuestion = questionList[currentReadingQuestionIndex]
265+
const currentSet = Number(currentReadingTestQuestion.set)
266+
const currentQuestionIdxinSet = questionsBySet[currentSet].seen.length
267+
const readingSetLength =
308268
questionsBySet[currentSet].seen.length + questionsBySet[currentSet].unseen.length
309269

310270
// Calculate previous reading set
311271
const prevReadingSet =
312-
currentSet && parseInt(currentSet) > 1 ? parseInt(currentSet) - 1 : null
272+
currentSet && parseInt(currentSet, 10) > 1 ? parseInt(currentSet, 10) - 1 : null
313273

314274
// If the current question has only one construct, set the eliciated_construct field
315-
if (tmpcurrentReadingTestQuestion?.constructs?.length === 1) {
316-
tmpcurrentReadingTestQuestion.eliciated_construct = tmpcurrentReadingTestQuestion.constructs[0];
275+
if (currentReadingTestQuestion?.constructs?.length === 1) {
276+
const [construct] = currentReadingTestQuestion.constructs
277+
currentReadingTestQuestion.eliciated_construct = construct
317278
}
318279

319-
if (previousStatus && !previousStatus.is_correct && previousStatus.responses?.length) {
320-
tmpcurrentReadingTestQuestion.choices = tmpcurrentReadingTestQuestion.choices.map(choice =>
280+
// If resuming, mark previously selected incorrect choices
281+
if (previousStatus && previousStatus.responses?.length) {
282+
currentReadingTestQuestion.choices = currentReadingTestQuestion.choices.map(choice =>
321283
previousStatus?.responses.includes(choice.option)
322284
? { ...choice, isSelected: true }
323285
: { ...choice }
324286
)
325287
}
326288

327-
console.log('reducer question_list: ', question_list)
328-
329-
console.log(
330-
'reducer testDone: ',
331-
question_list.filter(question => !question.seen).length === 0
332-
)
289+
// If resuming, mark previously selected correct choice
290+
if (
291+
previousStatus &&
292+
previousStatus.is_correct &&
293+
currentReadingTestQuestion.question_id === previousStatus.last_question_id
294+
) {
295+
currentReadingTestQuestion.choices = currentReadingTestQuestion.choices.map(choice =>
296+
choice.is_correct ? { ...choice, isSelected: true } : { ...choice }
297+
)
298+
}
333299

334300
return {
335301
...state,
336-
readingTestSetDict: question_set_dict,
337-
readingTestQuestions: question_list, // tempreadingTestQuestions,
338-
currentReadingTestQuestion: tmpcurrentReadingTestQuestion,
302+
readingTestSetDict,
303+
readingTestQuestions: questionList,
304+
currentReadingTestQuestion,
339305
currentReadingSet: currentSet,
340306
prevReadingSet,
341-
readingTestSessionId: session_id,
342-
currentReadingQuestionIndex: tmpcurrentReadingQuestionIndex,
343-
currentQuestionIdxinSet: tmpcurrentQuestionIdxinSet,
307+
readingTestSessionId,
308+
currentReadingQuestionIndex,
309+
currentQuestionIdxinSet,
344310
feedbacks: [],
345311
attempt_and_feedbacks: [],
346-
readingSetLength: tmpreadingSetLength,
312+
readingSetLength,
347313
pending: false,
348314
resumedTest: Object.values(questionsBySet).some(x => x.seen.length > 0),
349-
testDone: question_list.filter(question => !question.seen).length === 0,
315+
testDone: questionList.filter(question => !question.seen).length === 0,
350316
allCycles,
351317
currentCycle,
352318
previousStatus,

0 commit comments

Comments
 (0)