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 === y.id.toString())?.solution === x.solution.toLowerCase() || false).length; const missing = total - answers.filter((x) => questions.find((y) => x.id === 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 ( <>
For each of the questions below, select