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"; function Blank({ id, maxWords, userSolution, showSolutions = false, setUserSolution, }: { id: string; solutions?: string[]; userSolution?: string; maxWords: number; showSolutions?: boolean; setUserSolution: (solution: string) => void; }) { const [userInput, setUserInput] = useState(userSolution || ""); useEffect(() => { const words = userInput.split(" ").filter((x) => x !== ""); if (words.length >= maxWords) { toast.warning(`You have reached your word limit of ${maxWords} words!`, {toastId: "word-limit"}); setUserInput(words.join(" ").trim()); } }, [maxWords, userInput]); return ( setUserInput(e.target.value)} onBlur={() => setUserSolution(userInput)} value={userInput} contentEditable={showSolutions} /> ); } export default function WriteBlanks({id, prompt, type, maxWords, solutions, userSolutions, text, onNext, onBack}: WriteBlanksExercise & CommonProps) { const [answers, setAnswers] = useState<{id: string; solution: string}[]>(userSolutions); const calculateScore = () => { const total = text.match(/({{\d+}})/g)?.length || 0; const correct = answers.filter( (x) => solutions .find((y) => x.id === y.id) ?.solution.map((y) => y.toLowerCase()) .includes(x.solution.toLowerCase()) || false, ).length; return {total, correct}; }; const renderLines = (line: string) => { return ( {reactStringReplace(line, /({{\d+}})/g, (match) => { const id = match.replaceAll(/[\{\}]/g, ""); const userSolution = answers.find((x) => x.id === id); const setUserSolution = (solution: string) => { setAnswers((prev) => [...prev.filter((x) => x.id !== id), {id, solution}]); }; return ; })} ); }; return ( <>
{prompt.split("\\n").map((line, index) => ( {line}
))}
{text.split("\\n").map((line, index) => (

{renderLines(line)}

))}
); }