Skip to content

Commit

Permalink
chore: address review comments and improve test suite to include serv…
Browse files Browse the repository at this point in the history
…er side response loading for microlearnins
  • Loading branch information
sjschlapbach committed Jan 14, 2025
1 parent 46881a3 commit e92eaf4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 48 deletions.
12 changes: 6 additions & 6 deletions cypress/cypress/e2e/G-microlearning-workflow.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,12 +1020,12 @@ describe('Different microlearning workflows', () => {
cy.reload()
verifyPersistentCompleteInputs()

// TODO: verify that the entered answers are correctly refetched from the backend after a reload and cookie reset
// cy.clearAllLocalStorage()
// cy.clearAllSessionStorage()
// cy.reload()
// cy.wait(1000)
// verifyPersistentCompleteInputs()
// verify that the entered answers are correctly refetched from the backend after a reload and cookie reset
cy.clearAllLocalStorage()
cy.clearAllSessionStorage()
cy.reload()
cy.wait(1000)
verifyPersistentCompleteInputs()

// finish the microlearning
cy.get('[data-cy="student-stack-continue"]')
Expand Down
72 changes: 31 additions & 41 deletions packages/shared-components/src/hooks/useStudentResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,51 @@ function useStudentResponse({
}: UseStudentResponseProps) {
useEffect(() => {
const newStudentResponse =
stack.elements?.reduce((acc, element) => {
stack.elements?.reduce<StackStudentResponseType>((acc, element) => {
if (element.elementData.__typename === 'ChoicesElementData') {
return {
...acc,
[element.id]: {
type: element.elementData.type as ElementChoicesType,
response: element.elementData.options.choices.reduce(
(acc, choice) => {
return { ...acc, [choice.ix]: undefined }
},
{} as Record<number, boolean | undefined>
),
correct: undefined,
valid: false,
},
acc[element.id] = {
type: element.elementData.type as ElementChoicesType,
response: element.elementData.options.choices.reduce<
Record<number, boolean | undefined>
>((acc, choice) => {
return { ...acc, [choice.ix]: undefined }
}, {}),
valid: false,
}
} else if (element.elementData.type === ElementType.Content) {
return {
...acc,
[element.id]: {
type: element.elementData.type,
response: defaultRead ? true : undefined,
correct: undefined,
valid: true,
},

return acc
} else if (element.elementData.__typename === 'ContentElementData') {
acc[element.id] = {
type: ElementType.Content,
response: defaultRead ? true : undefined,
valid: true,
}
} else if (element.elementData.type === ElementType.Selection) {

return acc
} else if (element.elementData.__typename === 'SelectionElementData') {
const emptyResponses = getEmptySelectionResponse({
numberOfInputs: (element.elementData as SelectionElementData)
.options.numberOfInputs,
})

return {
...acc,
[element.id]: {
type: element.elementData.type,
response: emptyResponses,
correct: undefined,
valid: false,
},
acc[element.id] = {
type: ElementType.Selection,
response: emptyResponses,
valid: false,
}
return acc
}
// default case - valid for FREE_TEXT, NUMERICAL, FLASHCARD elements
else {
return {
...acc,
[element.id]: {
type: element.elementData.type,
response: undefined,
correct: undefined,
valid: false,
},
acc[element.id] = {
type: element.elementData.type,
response: undefined,
valid: false,
}

return acc
}
}, {} as StackStudentResponseType) || {}
}, {}) || {}

setStudentResponse(newStudentResponse)
}, [currentStep, stack.elements])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function completeSelectionResponse({

const completedResponse = { ...emptyResponses }
Object.keys(existingResponse).forEach((key) => {
emptyResponses[parseInt(key)] = existingResponse[parseInt(key)] ?? -1
completedResponse[parseInt(key)] = existingResponse[parseInt(key)] ?? -1
})

return completedResponse
Expand Down

0 comments on commit e92eaf4

Please sign in to comment.