import {errorButtonStyle, infoButtonStyle} from "@/constants/buttonStyles"; import {WriteBlanksExercise} from "@/interfaces/exam"; import {mdiArrowLeft, mdiArrowRight} from "@mdi/js"; import Icon from "@mdi/react"; import clsx from "clsx"; import {Fragment, useEffect, useState} from "react"; import reactStringReplace from "react-string-replace"; import {CommonProps} from "."; import {toast} from "react-toastify"; import Button from "../Low/Button"; import useExamStore from "@/stores/examStore"; function Blank({ id, maxWords, solutions, userSolution, disabled = false, setUserSolution, }: { id: string; solutions: string[]; userSolution?: string; maxWords: number; disabled?: boolean; setUserSolution?: (solution: string) => void; }) { const [userInput, setUserInput] = useState(userSolution || ""); useEffect(() => { const words = userInput.split(" ").filter((x) => x !== ""); if (words.length >= maxWords) { setUserInput(words.join(" ").trim()); if (setUserSolution) setUserSolution(words.join(" ").trim()); } }, [maxWords, userInput, setUserSolution]); const isUserSolutionCorrect = () => userSolution && solutions.map((x) => x.trim().toLowerCase()).includes(userSolution.trim().toLowerCase()); const getSolutionStyling = () => { if (!userSolution) { return "bg-mti-gray-davy text-white"; } return "bg-mti-purple-ultralight text-mti-purple-light"; }; return ( {userSolution && !isUserSolutionCorrect() && (
{userSolution}
)}
{!solutions ? userInput : solutions.join(" / ")}
); } export default function WriteBlanksSolutions({ id, type, prompt, maxWords, solutions, userSolutions, text, onNext, onBack, }: WriteBlanksExercise & CommonProps) { const {questionIndex, setQuestionIndex, partIndex, exam} = useExamStore((state) => state); const calculateScore = () => { const total = text.match(/({{\d+}})/g)?.length || 0; const correct = userSolutions.filter( (x) => solutions .find((y) => x.id.toString() === y.id.toString()) ?.solution.map((y) => y.toLowerCase().trim()) .includes(x.solution.toLowerCase().trim()) || false, ).length; const missing = total - userSolutions.filter((x) => solutions.find((y) => x.id.toString() === y.id.toString())).length; return {total, correct, missing}; }; const renderLines = (line: string) => { return ( {reactStringReplace(line, /({{\d+}})/g, (match) => { const id = match.replaceAll(/[\{\}]/g, "").toString(); const userSolution = userSolutions.find((x) => x.id.toString() === id.toString()); const solution = solutions.find((x) => x.id.toString() === id.toString())!; return ( ); })} ); }; return ( <>
{prompt.split("\\n").map((line, index) => ( {line}
))}
{userSolutions && text.split("\\n").map((line, index) => (

{renderLines(line)}

))}
Correct
Unanswered
Wrong
); }