88 lines
2.8 KiB
TypeScript
88 lines
2.8 KiB
TypeScript
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 {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 [exerciseIndex, setExerciseIndex] = useState(0);
|
|
const [userSolutions, setUserSolutions] = useState<UserSolution[]>(exam.exercises.map((x) => defaultUserSolutions(x, exam)));
|
|
const [hasExamEnded, setHasExamEnded] = useExamStore((state) => [state.hasExamEnded, state.setHasExamEnded]);
|
|
|
|
const nextExercise = (solution?: UserSolution) => {
|
|
if (solution) {
|
|
setUserSolutions((prev) => [...prev.filter((x) => x.exercise !== solution.exercise), solution]);
|
|
}
|
|
|
|
if (exerciseIndex + 1 < exam.exercises.length) {
|
|
setExerciseIndex((prev) => prev + 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) => {
|
|
if (solution) {
|
|
setUserSolutions((prev) => [...prev.filter((x) => x.exercise !== solution.exercise), solution]);
|
|
}
|
|
|
|
if (exerciseIndex > 0) {
|
|
setExerciseIndex((prev) => prev - 1);
|
|
}
|
|
};
|
|
|
|
const getExercise = () => {
|
|
const exercise = exam.exercises[exerciseIndex];
|
|
return {
|
|
...exercise,
|
|
userSolutions: userSolutions.find((x) => x.exercise === exercise.id)?.solutions || [],
|
|
};
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className="flex flex-col h-full w-full gap-8 items-center">
|
|
<ModuleTitle
|
|
minTimer={exam.minTimer}
|
|
exerciseIndex={exerciseIndex + 1}
|
|
module="speaking"
|
|
totalExercises={exam.exercises.length}
|
|
disableTimer={showSolutions}
|
|
/>
|
|
{exerciseIndex > -1 &&
|
|
exerciseIndex < exam.exercises.length &&
|
|
!showSolutions &&
|
|
renderExercise(getExercise(), nextExercise, previousExercise)}
|
|
{exerciseIndex > -1 &&
|
|
exerciseIndex < exam.exercises.length &&
|
|
showSolutions &&
|
|
renderSolution(exam.exercises[exerciseIndex], nextExercise, previousExercise)}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|