Skip to content

Commit

Permalink
feat: added FE implementation for multiple quiz type
Browse files Browse the repository at this point in the history
  • Loading branch information
ahurniak committed Sep 5, 2024
1 parent 014a512 commit ddb953d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
10 changes: 10 additions & 0 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ export const getQuestion = async (questionId: number) =>

export const isAnswerCorrect = async (questionId: number, answerIdx: number) =>
await fetchJson<boolean>(`/api/quiz-question/${questionId}/answer/${answerIdx}`)

export const isMultipleAnswersCorrect = async (questionId: number, answersList: number[]) => {
return await fetchJson<boolean>(`/api/quiz-question/${questionId}/answer`, {
method: 'POST',
body: JSON.stringify(answersList),
headers: {
'Content-Type': 'application/json',
},
})
}
34 changes: 29 additions & 5 deletions frontend/src/quiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import { useParams } from '@solidjs/router'
import type { QuizQuestion } from 'model/quiz-question.ts'
import * as api from 'api.ts'
import { preventDefault } from 'helpers.ts'
import { transformObjectToArray } from './utils/transformObjectToArray.ts'

const Feedback = (correct: boolean) => <p class="feedback">{correct ? 'Correct!' : 'Incorrect!'}</p>

const Question = ({ id, question, answers, quizType }: QuizQuestion) => {
const [selectedAnswer, setSelectedAnswer] = createSignal<number | null>(null)
const [selectedAnswers, setSelectedAnswers] = createSignal<{ [key: string]: boolean } | Record<string, boolean>>({})
const [isAnswerCorrect, setIsAnswerCorrect] = createSignal(false)

const [submitted, setSubmitted] = createSignal(false)

const isMultiple = quizType === 'MULTIPLE'

const submit = preventDefault(async () => {
const selectedAnswerIdx = selectedAnswer()
if (selectedAnswerIdx === null) return
Expand All @@ -24,20 +28,40 @@ const Question = ({ id, question, answers, quizType }: QuizQuestion) => {
})
})

const submitMultiple = preventDefault(async () => {
if (Object.keys(selectedAnswers()).length === 0) return

const payload = transformObjectToArray(selectedAnswers())

api.isMultipleAnswersCorrect(id, payload).then(isCorrect => {
setSubmitted(true)
setIsAnswerCorrect(isCorrect)
})
})

const selectAnswer = (answerIdx: number) => () => setSelectedAnswer(answerIdx)

const handleCheckboxChange = (event: InputEvent) => {
const { name, checked } = event.target as HTMLInputElement
setSelectedAnswers(prevState => ({
...prevState,
[name]: checked,
}))
}

const Answer = (answer: string, idx: Accessor<number>) => {
const answerId = `answer-${idx()}`
const answerId: string = `answer-${idx()}`

if (quizType === 'MULTIPLE') {
if (isMultiple) {
return (
<li>
<input
type={'checkbox'}
name={'answer'}
name={`${idx()}`}
id={answerId}
value={answer}
onClick={selectAnswer(idx())}
checked={selectedAnswers()?.[idx()]}
onInput={handleCheckboxChange}
/>
<label for={answerId}>{answer}</label>
</li>
Expand All @@ -53,7 +77,7 @@ const Question = ({ id, question, answers, quizType }: QuizQuestion) => {
}

return (
<form onSubmit={submit}>
<form onSubmit={isMultiple ? submitMultiple : submit}>
<h1>{question}</h1>
<ul>
<For each={answers} children={Answer} />
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/utils/transformObjectToArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function transformObjectToArray(obj: Record<number, boolean>): number[] {
return Object.keys(obj)
.filter(key => obj[+key]) // Filter keys where the value is true
.map(Number) // Convert keys from string to number
}

0 comments on commit ddb953d

Please sign in to comment.