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,
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) {
toast.warning(`You have reached your word limit of ${maxWords} words!`, {toastId: "word-limit"});
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-blue-ultralight text-mti-blue-light";
}
return "bg-mti-green-ultralight text-mti-green-light";
};
return (
{userSolution && !isUserSolutionCorrect() && (
setUserInput(e.target.value)}
value={userSolution}
contentEditable={disabled}
/>
)}
setUserInput(e.target.value)}
value={!solutions ? userInput : solutions.join(" / ")}
contentEditable={disabled}
/>
);
}
export default function WriteBlanksSolutions({
id,
prompt,
maxWords,
solutions,
userSolutions,
text,
onNext,
onBack,
}: WriteBlanksExercise & CommonProps) {
const renderLines = (line: string) => {
return (
{reactStringReplace(line, /({{\d+}})/g, (match) => {
const id = match.replaceAll(/[\{\}]/g, "");
const userSolution = userSolutions.find((x) => x.id === id);
const solution = solutions.find((x) => x.id === id)!;
return
{renderLines(line)}