import {renderExercise} from "@/components/Exercises"; import ModuleTitle from "@/components/Medium/ModuleTitle"; import {renderSolution} from "@/components/Solutions"; import {infoButtonStyle} from "@/constants/buttonStyles"; import {UserSolution, SpeakingExam} from "@/interfaces/exam"; import useExamStore from "@/stores/examStore"; import {defaultUserSolutions} from "@/utils/exams"; import {countExercises} from "@/utils/moduleUtils"; import {convertCamelCaseToReadable} from "@/utils/string"; import {mdiArrowRight} from "@mdi/js"; import Icon from "@mdi/react"; import clsx from "clsx"; import {Fragment, useEffect, useState} from "react"; import {toast} from "react-toastify"; interface Props { exam: SpeakingExam; showSolutions?: boolean; onFinish: (userSolutions: UserSolution[]) => void; } export default function Speaking({exam, showSolutions = false, onFinish}: Props) { const [questionIndex, setQuestionIndex] = useState(0); const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0); const {userSolutions, setUserSolutions} = useExamStore((state) => state); const {hasExamEnded, setHasExamEnded} = useExamStore((state) => state); const {exerciseIndex, setExerciseIndex} = useExamStore((state) => state); const scrollToTop = () => Array.from(document.getElementsByTagName("body")).forEach((body) => body.scrollTo(0, 0)); useEffect(() => { setCurrentQuestionIndex(0); }, [questionIndex]); const nextExercise = (solution?: UserSolution) => { scrollToTop(); if (solution) { setUserSolutions([...userSolutions.filter((x) => x.exercise !== solution.exercise), solution]); } setQuestionIndex((prev) => prev + currentQuestionIndex); if (exerciseIndex + 1 < exam.exercises.length) { setExerciseIndex(exerciseIndex + 1); return; } if (exerciseIndex >= exam.exercises.length) return; setHasExamEnded(false); if (solution) { onFinish( [...userSolutions.filter((x) => x.exercise !== solution.exercise), solution].map((x) => ({...x, module: "speaking", exam: exam.id})), ); } else { onFinish(userSolutions.map((x) => ({...x, module: "speaking", exam: exam.id}))); } }; const previousExercise = (solution?: UserSolution) => { scrollToTop(); if (solution) { setUserSolutions([...userSolutions.filter((x) => x.exercise !== solution.exercise), solution]); } if (exerciseIndex > 0) { setExerciseIndex(exerciseIndex - 1); } }; const getExercise = () => { const exercise = exam.exercises[exerciseIndex]; return { ...exercise, userSolutions: userSolutions.find((x) => x.exercise === exercise.id)?.solutions || [], }; }; return ( <>