Skip to content

Commit

Permalink
fix exercise validation in quizzes
Browse files Browse the repository at this point in the history
  • Loading branch information
d471061c committed Aug 4, 2023
1 parent 1bfc3d9 commit a325598
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 deletions.
7 changes: 7 additions & 0 deletions services/quizzes/src/components/AnswerExercise.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ const Exercise: React.FC<React.PropsWithChildren<ExerciseProps>> = ({
outputState: userAnswer,
port: port,
_rawSetOutputState: setUserAnswer,
validate: (previousState) => {
if (!previousState) {
return false
}
const validities = previousState.itemAnswers.map((item) => item.valid)
return validities.every(Boolean)
},
}}
>
<Widget
Expand Down
1 change: 1 addition & 0 deletions services/quizzes/src/components/ExerciseEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const EditorImpl: React.FC<React.PropsWithChildren<EditorProps>> = ({ port, priv
outputState,
port: port,
_rawSetOutputState: setOutputState,
validate: () => true,
}}
>
<QuizItemsV2 />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const ClosedEndedQuestion: React.FC<
quizItemId: quizItem.id,
type: "closed-ended-question",
textData: newValue,
valid: true,
valid: newValue.length > 0,
})
return
}

if (!quizItem.formatRegex) {
return setQuizItemAnswerState({ ...quizItemAnswerState, textData: newValue, valid: true })
return setQuizItemAnswerState({ ...quizItemAnswerState, textData: newValue, valid: false })
}

const newValueIsValid = newValue
Expand Down
7 changes: 1 addition & 6 deletions services/quizzes/src/components/widget/Matrix/Matrix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ const Matrix: React.FunctionComponent<
handleSizeChange(matrixVariable)
}, [handleSizeChange, matrixVariable])

if (!quizItemAnswerState) {
return <div></div>
}
const handleOptionSelect = (text: string, column: number, row: number) => {
const newMatrix = matrixVariable.map((rowArray, rowIndex) => {
return rowArray.map((cell, columnIndex) => {
Expand Down Expand Up @@ -148,9 +145,7 @@ const Matrix: React.FunctionComponent<
cellText={cellText}
handleOptionSelect={handleOptionSelect}
matrixSize={matrixActiveSize}
>
{" "}
</MatrixCell>
></MatrixCell>
)
}
})}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PrivateSpecQuiz } from "../../types/quizTypes/privateSpec"
import { createExerciseServiceContext } from "../shared-module/contexts/ExerciseServiceContext"

const QuizzesExerciseServiceContext = createExerciseServiceContext<PrivateSpecQuiz>()
const QuizzesExerciseServiceContext = createExerciseServiceContext<PrivateSpecQuiz>(() => false)

export default QuizzesExerciseServiceContext
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserAnswer } from "../../types/quizTypes/answer"
import { createExerciseServiceContext } from "../shared-module/contexts/ExerciseServiceContext"

const QuizzesUserItemAnswerContext = createExerciseServiceContext<UserAnswer>()
const QuizzesUserItemAnswerContext = createExerciseServiceContext<UserAnswer>(() => false)

export default QuizzesUserItemAnswerContext
6 changes: 5 additions & 1 deletion shared-module/src/contexts/ExerciseServiceContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import React from "react"
interface ExerciseServiceContextProps<T> {
outputState: T | null
port: MessagePort | null
validate: (newState: T | null) => boolean
_rawSetOutputState: (newValue: T | null) => void
}

export const createExerciseServiceContext = <OutputType,>() => {
export const createExerciseServiceContext = <OutputType,>(
validate: (newState: OutputType | null) => boolean,
) => {
return React.createContext<ExerciseServiceContextProps<OutputType>>({
outputState: null,
port: null,
_rawSetOutputState: () => {
/* NOP */
},
validate,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ const useExerciseServiceOutputState = <OutputType, SelectorReturnType>(
context: ExerciseServiceContextType<OutputType>,
selector: (arg: OutputType | null) => SelectorReturnType | null,
): UseExerciseServiceOutputStateReturn<SelectorReturnType> => {
const { outputState, port, _rawSetOutputState } = useContext(context)
const { outputState, port, _rawSetOutputState, validate } = useContext(context)

const updateState = (func: UpdateFunction<SelectorReturnType>) => {
if (!port) {
return
}

if (!validate) {
return
}

const nextState = produce(outputState, (draft) => {
const selected = selector(draft as OutputType)
// Selected is a draft too because it is a subset of the draft variable
Expand All @@ -32,7 +36,7 @@ const useExerciseServiceOutputState = <OutputType, SelectorReturnType>(
data: nextState,
// eslint-disable-next-line i18next/no-literal-string
message: "current-state",
valid: true,
valid: validate(nextState),
}
port.postMessage(message)
_rawSetOutputState(nextState)
Expand Down

0 comments on commit a325598

Please sign in to comment.