import {FillBlanksExercise} from "@/interfaces/exam"; import useExamStore from "@/stores/examStore"; import clsx from "clsx"; import {Fragment, useEffect, useState} from "react"; import reactStringReplace from "react-string-replace"; import {CommonProps} from "."; import Button from "../Low/Button"; interface WordsDrawerProps { words: {word: string; isDisabled: boolean}[]; isOpen: boolean; blankId?: string; previouslySelectedWord?: string; onCancel: () => void; onAnswer: (answer: string) => void; } function WordsDrawer({words, isOpen, blankId, previouslySelectedWord, onCancel, onAnswer}: WordsDrawerProps) { const [selectedWord, setSelectedWord] = useState(previouslySelectedWord); return ( <>
{blankId}
Choose the correct word:
{words.map(({word, isDisabled}) => ( ))}
); } export default function FillBlanks({ id, allowRepetition, type, prompt, solutions, text, words, userSolutions, onNext, onBack, }: FillBlanksExercise & CommonProps) { const [answers, setAnswers] = useState<{id: string; solution: string}[]>(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 = text.match(/({{\d+}})/g)?.length || 0; const correct = answers.filter((x) => { const solution = solutions.find((y) => x.id.toString() === y.id.toString())?.solution.toLowerCase(); if (!solution) return false; const option = words.find((w) => typeof w === "string" ? w.toLowerCase() === x.solution.toLowerCase() : w.letter.toLowerCase() === x.solution.toLowerCase(), ); if (!option) return false; return solution === (typeof option === "string" ? option.toLowerCase() : option.word.toLowerCase()); }).length; const missing = total - answers.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, ""); const userSolution = answers.find((x) => x.id === id); return ( setAnswers((prev) => [...prev.filter((x) => x.id !== id), {id, solution: e.target.value}])} value={userSolution?.solution}> ); })}
); }; return ( <>
{prompt.split("\\n").map((line, index) => ( {line}
))}
{text.split("\\n").map((line, index) => (

{renderLines(line)}

))}
Options
{words.map((v) => { const text = typeof v === "string" ? v : `${v.letter} - ${v.word}`; return ( x.solution.toLowerCase() === (typeof v === "string" ? v : v.letter).toLowerCase()) && "bg-mti-purple-dark text-white", )} key={text}> {text} ); })}
); }