import { TrueFalseExercise } from "@/interfaces/exam"; import clsx from "clsx"; import { Fragment, useCallback, useEffect, useState } from "react"; import { CommonProps } from "./types"; import Button from "../Low/Button"; import PracticeBadge from "../Low/PracticeBadge"; const TrueFalse: React.FC = ({ id, type, prompt, questions, userSolutions, isPractice = false, registerSolution, headerButtons, footerButtons, }) => { const [answers, setAnswers] = useState<{ id: string; solution: "true" | "false" | "not_given" }[]>(userSolutions); const calculateScore = useCallback(() => { 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 }; }, [answers, questions]); 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(() => { registerSolution(() => ({ exercise: id, solutions: answers, score: calculateScore(), type, isPractice })); // eslint-disable-next-line react-hooks/exhaustive-deps }, [id, answers, type, isPractice, calculateScore]); return (
{headerButtons}
{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. {isPractice && }
{questions.map((question, index) => { const id = question.id.toString(); return (
{index + 1}. {question.prompt}
); })}
{footerButtons}
); } export default TrueFalse;