import { TrueFalseExercise } from "@/interfaces/exam"; import useExamStore from "@/stores/examStore"; import clsx from "clsx"; import { Fragment, useEffect, useState } from "react"; import { CommonProps } from "."; import Button from "../Low/Button"; export default function TrueFalse({ id, type, prompt, questions, userSolutions, isPractice = false, onNext, onBack, disableProgressButtons = false }: TrueFalseExercise & CommonProps) { const [answers, setAnswers] = useState<{ id: string; solution: "true" | "false" | "not_given" }[]>(userSolutions); const hasExamEnded = useExamStore((state) => state.hasExamEnded); const setCurrentSolution = useExamStore((state) => state.setCurrentSolution); useEffect(() => { if (hasExamEnded) onNext({ exercise: id, solutions: answers, score: calculateScore(), type, isPractice }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [hasExamEnded]); const calculateScore = () => { const total = questions.length || 0; const correct = answers.filter( (x) => questions .find((y) => x.id.toString() === y.id.toString()) ?.solution?.toString() .toLowerCase() === x.solution.toLowerCase() || false, ).length; const missing = total - answers.filter((x) => questions.find((y) => x.id.toString() === y.id.toString())).length; return { total, correct, missing }; }; useEffect(() => { setCurrentSolution({ exercise: id, solutions: answers, score: calculateScore(), type, isPractice }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [answers, setAnswers]); const toggleAnswer = (solution: "true" | "false" | "not_given", questionId: string) => { const answer = answers.find((x) => x.id === questionId); if (answer && answer.solution === solution) { setAnswers((prev) => prev.filter((x) => x.id !== questionId)); return; } setAnswers((prev) => [...prev.filter((x) => x.id !== questionId), { id: questionId, solution }]); }; useEffect(() => { if (disableProgressButtons) onNext({ exercise: id, solutions: answers, score: calculateScore(), type, isPractice }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [answers, disableProgressButtons]) const progressButtons = () => (
) return (
{!disableProgressButtons && progressButtons()}
{prompt.split("\\n").map((line, index) => ( {line}
))}

For each of the questions below, select

TRUE FALSE NOT GIVEN if the statement agrees with the information if the statement contradicts with the information if there is no information on this
You can click a selected option again to deselect it.
{questions.map((question, index) => { const id = question.id.toString(); return (
{index + 1}. {question.prompt}
); })}
{!disableProgressButtons && progressButtons()}
); }