/* eslint-disable @next/next/no-img-element */ import {WritingExercise} from "@/interfaces/exam"; import {CommonProps} from "."; import React, {Fragment, useEffect, useRef, useState} from "react"; import {toast} from "react-toastify"; import Button from "../Low/Button"; import {Dialog, Transition} from "@headlessui/react"; import useExamStore from "@/stores/examStore"; export default function Writing({ id, prompt, prefix, suffix, type, wordCounter, attachment, userSolutions, onNext, onBack, }: WritingExercise & CommonProps) { const [isModalOpen, setIsModalOpen] = useState(false); const [inputText, setInputText] = useState(userSolutions.length === 1 ? userSolutions[0].solution : ""); const [isSubmitEnabled, setIsSubmitEnabled] = useState(false); const hasExamEnded = useExamStore((state) => state.hasExamEnded); useEffect(() => { if (localStorage.getItem("enable_paste")) return; const listener = (e: KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && e.key === "v") { e.preventDefault(); } }; document.addEventListener("keydown", listener); return () => { document.removeEventListener("keydown", listener); }; }, []); useEffect(() => { if (hasExamEnded) onNext({exercise: id, solutions: [{id, solution: inputText}], score: {correct: 1, total: 1, missing: 0}, type}); // eslint-disable-next-line react-hooks/exhaustive-deps }, [hasExamEnded]); useEffect(() => { const words = inputText.split(" ").filter((x) => x !== ""); if (wordCounter.type === "min") { setIsSubmitEnabled(wordCounter.limit <= words.length); } else { setIsSubmitEnabled(true); if (wordCounter.limit < words.length) { toast.warning(`You have reached your word limit of ${wordCounter.limit} words!`, {toastId: "word-limit"}); setInputText(words.slice(0, words.length - 1).join(" ")); } } }, [inputText, wordCounter]); return ( <> {attachment && ( setIsModalOpen(false)} className="relative z-50">
{attachment.description}
)}
{prefix.replaceAll("\\n", "\n")} {prompt.replaceAll("\\n", "\n")} {attachment && ( setIsModalOpen(true)} src={attachment.url} alt={attachment.description} className="max-w-md self-center rounded-xl cursor-pointer" /> )}
{suffix}