import {ReadingExam, UserSolution} from "@/interfaces/exam"; import {Fragment, useEffect, useState} from "react"; import Icon from "@mdi/react"; import {mdiArrowRight, mdiNotebook} from "@mdi/js"; import clsx from "clsx"; import {infoButtonStyle} from "@/constants/buttonStyles"; import {convertCamelCaseToReadable} from "@/utils/string"; import {Dialog, Transition} from "@headlessui/react"; import {renderExercise} from "@/components/Exercises"; import {renderSolution} from "@/components/Solutions"; import {Panel} from "primereact/panel"; import {Steps} from "primereact/steps"; import {BsAlarm, BsBook, BsClock, BsStopwatch} from "react-icons/bs"; import ProgressBar from "@/components/Low/ProgressBar"; import ModuleTitle from "@/components/Medium/ModuleTitle"; import {Divider} from "primereact/divider"; import Button from "@/components/Low/Button"; import BlankQuestionsModal from "@/components/BlankQuestionsModal"; import useExamStore from "@/stores/examStore"; import {defaultUserSolutions} from "@/utils/exams"; interface Props { exam: ReadingExam; showSolutions?: boolean; onFinish: (userSolutions: UserSolution[]) => void; } function TextModal({isOpen, title, content, onClose}: {isOpen: boolean; title: string; content: string; onClose: () => void}) { return (
{title}

{content.split("\\n").map((line, index) => ( {line}
))}

); } export default function Reading({exam, showSolutions = false, onFinish}: Props) { const [exerciseIndex, setExerciseIndex] = useState(-1); const [showTextModal, setShowTextModal] = useState(false); const [userSolutions, setUserSolutions] = useState(exam.exercises.map((x) => defaultUserSolutions(x, exam))); const [showBlankModal, setShowBlankModal] = useState(false); const [hasExamEnded, setHasExamEnded] = useExamStore((state) => [state.hasExamEnded, state.setHasExamEnded]); useEffect(() => { if (hasExamEnded && exerciseIndex === -1) { setExerciseIndex((prev) => prev + 1); } }, [hasExamEnded, exerciseIndex]); const confirmFinishModule = (keepGoing?: boolean) => { if (!keepGoing) { setShowBlankModal(false); return; } onFinish(userSolutions.map((x) => ({...x, module: "reading", exam: exam.id}))); }; const nextExercise = (solution?: UserSolution) => { if (solution) { setUserSolutions((prev) => [...prev.filter((x) => x.exercise !== solution.exercise), solution]); } if (exerciseIndex + 1 < exam.exercises.length && !hasExamEnded) { setExerciseIndex((prev) => prev + 1); return; } if ( solution && ![...userSolutions.filter((x) => x.exercise !== solution?.exercise).map((x) => x.score.missing), solution?.score.missing].every( (x) => x === 0, ) && !showSolutions && !hasExamEnded ) { setShowBlankModal(true); return; } setHasExamEnded(false); if (solution) { onFinish( [...userSolutions.filter((x) => x.exercise !== solution.exercise), solution].map((x) => ({...x, module: "reading", exam: exam.id})), ); } else { onFinish(userSolutions.map((x) => ({...x, module: "reading", exam: exam.id}))); } }; const previousExercise = (solution?: UserSolution) => { if (solution) { setUserSolutions((prev) => [...prev.filter((x) => x.exercise !== solution.exercise), solution]); } setExerciseIndex((prev) => prev - 1); }; const getExercise = () => { const exercise = exam.exercises[exerciseIndex]; return { ...exercise, userSolutions: userSolutions.find((x) => x.exercise === exercise.id)?.solutions || [], }; }; const renderText = () => (

Please read the following excerpt attentively, you will then be asked questions about the text you've read.

You will be allowed to read the text while doing the exercises

{exam.text.title}

{exam.text.content.split("\\n").map((line, index) => (

{line}

))}
); return ( <>
setShowTextModal(false)} /> {exerciseIndex === -1 && renderText()} {exerciseIndex > -1 && exerciseIndex < exam.exercises.length && !showSolutions && renderExercise(getExercise(), nextExercise, previousExercise)} {exerciseIndex > -1 && exerciseIndex < exam.exercises.length && showSolutions && renderSolution(exam.exercises[exerciseIndex], nextExercise, previousExercise)} {exerciseIndex > -1 && exerciseIndex < exam.exercises.length && ( )}
{exerciseIndex === -1 && ( )} ); }