import {TrueFalseExercise} from "@/interfaces/exam"; import useExamStore from "@/stores/examStore"; import {Fragment, useEffect, useState} from "react"; import {CommonProps} from "."; import Button from "../Low/Button"; export default function TrueFalse({id, type, prompt, questions, userSolutions, onNext, onBack}: TrueFalseExercise & CommonProps) { const [answers, setAnswers] = useState<{id: string; solution: "true" | "false" | "not_given"}[]>(userSolutions); const hasExamEnded = useExamStore((state) => state.hasExamEnded); useEffect(() => { if (hasExamEnded) onNext({exercise: id, solutions: answers, score: calculateScore(), type}); // 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}; }; 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}]); }; return ( <>
{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}
); })}
); }