import {errorButtonStyle, infoButtonStyle} from "@/constants/buttonStyles"; import {FillBlanksExercise} from "@/interfaces/exam"; import {Dialog, Transition} from "@headlessui/react"; import clsx from "clsx"; import {Fragment, useState} from "react"; import reactStringReplace from "react-string-replace"; interface WordsPopoutProps { words: {word: string; isDisabled: boolean}[]; isOpen: boolean; onCancel: () => void; onAnswer: (answer: string) => void; } function WordsPopout({words, isOpen, onCancel, onAnswer}: WordsPopoutProps) { return (
List of words
{words.map((word) => ( ))}
); } export default function FillBlanks({allowRepetition, prompt, solutions, text, words}: FillBlanksExercise) { const [userSolutions, setUserSolutions] = useState<{id: string; solution: string}[]>([]); const [currentBlankId, setCurrentBlankId] = useState(); 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)}))} isOpen={!!currentBlankId} onCancel={() => setCurrentBlankId(undefined)} onAnswer={(solution: string) => { setUserSolutions((prev) => [...prev.filter((x) => x.id !== currentBlankId), {id: currentBlankId!, solution}]); setCurrentBlankId(undefined); }} /> {prompt} {text.split("\n").map((line) => ( <> {renderLines(line)}
))}
); }