import {FillBlanksExercise} from "@/interfaces/exam"; 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); useEffect(() => setSelectedWord(previouslySelectedWord), [previouslySelectedWord]); return ( <>
{blankId}
Choose the correct word:
{words.map(({word, isDisabled}) => ( ))}
); } export default function FillBlanks({id, allowRepetition, type, prompt, solutions, text, words, onNext, onBack}: FillBlanksExercise & CommonProps) { const [userSolutions, setUserSolutions] = useState<{id: string; solution: string}[]>([]); const [currentBlankId, setCurrentBlankId] = useState(); const [blankSelectedWord, setBlankSelectedWord] = useState(); useEffect(() => { setBlankSelectedWord(currentBlankId ? userSolutions.find((x) => x.id === currentBlankId)?.solution : undefined); }, [userSolutions, currentBlankId]); const calculateScore = () => { const total = text.match(/({{\d+}})/g)?.length || 0; const correct = userSolutions.filter((x) => solutions.find((y) => x.id === y.id)?.solution === 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 = userSolutions.find((x) => x.id === id); return ( ); })} ); }; return ( <>
({word, isDisabled: allowRepetition ? false : userSolutions.map((x) => x.solution).includes(word)}))} previouslySelectedWord={blankSelectedWord} isOpen={!!currentBlankId} onCancel={() => setCurrentBlankId(undefined)} onAnswer={(solution: string) => { setUserSolutions((prev) => [...prev.filter((x) => x.id !== currentBlankId), {id: currentBlankId!, solution}]); setCurrentBlankId(undefined); }} /> {prompt.split("\\n").map((line, index) => ( {line}
))}
{text.split("\\n").map((line, index) => (

{renderLines(line)}

))}
); }