Added a button to review the exam from the selected module forward
This commit is contained in:
@@ -1,251 +1,333 @@
|
||||
import Button from "@/components/Low/Button";
|
||||
import ModuleTitle from "@/components/Medium/ModuleTitle";
|
||||
import {moduleResultText} from "@/constants/ielts";
|
||||
import {Module} from "@/interfaces";
|
||||
import {User} from "@/interfaces/user";
|
||||
import { moduleResultText } from "@/constants/ielts";
|
||||
import { Module } from "@/interfaces";
|
||||
import { User } from "@/interfaces/user";
|
||||
import useExamStore from "@/stores/examStore";
|
||||
import {calculateBandScore} from "@/utils/score";
|
||||
import { calculateBandScore } from "@/utils/score";
|
||||
import clsx from "clsx";
|
||||
import Link from "next/link";
|
||||
import {useRouter} from "next/router";
|
||||
import {Fragment, useEffect, useState} from "react";
|
||||
import {BsArrowCounterclockwise, BsBook, BsClipboard, BsEyeFill, BsHeadphones, BsMegaphone, BsPen, BsShareFill} from "react-icons/bs";
|
||||
import {LevelScore} from "@/constants/ielts";
|
||||
import {getLevelScore} from "@/utils/score";
|
||||
import { useRouter } from "next/router";
|
||||
import { Fragment, useEffect, useState } from "react";
|
||||
import {
|
||||
BsArrowCounterclockwise,
|
||||
BsBook,
|
||||
BsClipboard,
|
||||
BsEyeFill,
|
||||
BsHeadphones,
|
||||
BsMegaphone,
|
||||
BsPen,
|
||||
BsShareFill,
|
||||
} from "react-icons/bs";
|
||||
import { LevelScore } from "@/constants/ielts";
|
||||
import { getLevelScore } from "@/utils/score";
|
||||
import { capitalize } from "lodash";
|
||||
|
||||
interface Score {
|
||||
module: Module;
|
||||
correct: number;
|
||||
total: number;
|
||||
missing: number;
|
||||
module: Module;
|
||||
correct: number;
|
||||
total: number;
|
||||
missing: number;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
user: User;
|
||||
modules: Module[];
|
||||
scores: Score[];
|
||||
isLoading: boolean;
|
||||
onViewResults: () => void;
|
||||
user: User;
|
||||
modules: Module[];
|
||||
scores: Score[];
|
||||
isLoading: boolean;
|
||||
onViewResults: (moduleIndex?: number) => void;
|
||||
}
|
||||
|
||||
export default function Finish({user, scores, modules, isLoading, onViewResults}: Props) {
|
||||
const [selectedModule, setSelectedModule] = useState(modules[0]);
|
||||
const [selectedScore, setSelectedScore] = useState<Score>(scores.find((x) => x.module === modules[0])!);
|
||||
export default function Finish({
|
||||
user,
|
||||
scores,
|
||||
modules,
|
||||
isLoading,
|
||||
onViewResults,
|
||||
}: Props) {
|
||||
const [selectedModule, setSelectedModule] = useState(modules[0]);
|
||||
const [selectedScore, setSelectedScore] = useState<Score>(
|
||||
scores.find((x) => x.module === modules[0])!,
|
||||
);
|
||||
|
||||
const exams = useExamStore((state) => state.exams);
|
||||
const exams = useExamStore((state) => state.exams);
|
||||
|
||||
useEffect(() => setSelectedScore(scores.find((x) => x.module === selectedModule)!), [scores, selectedModule]);
|
||||
useEffect(
|
||||
() => setSelectedScore(scores.find((x) => x.module === selectedModule)!),
|
||||
[scores, selectedModule],
|
||||
);
|
||||
|
||||
const moduleColors: {[key in Module]: {progress: string; inner: string}} = {
|
||||
reading: {
|
||||
progress: "text-ielts-reading",
|
||||
inner: "bg-ielts-reading-light",
|
||||
},
|
||||
listening: {
|
||||
progress: "text-ielts-listening",
|
||||
inner: "bg-ielts-listening-light",
|
||||
},
|
||||
writing: {
|
||||
progress: "text-ielts-writing",
|
||||
inner: "bg-ielts-writing-light",
|
||||
},
|
||||
speaking: {
|
||||
progress: "text-ielts-speaking",
|
||||
inner: "bg-ielts-speaking-light",
|
||||
},
|
||||
level: {
|
||||
progress: "text-ielts-level",
|
||||
inner: "bg-ielts-level-light",
|
||||
},
|
||||
};
|
||||
const moduleColors: { [key in Module]: { progress: string; inner: string } } =
|
||||
{
|
||||
reading: {
|
||||
progress: "text-ielts-reading",
|
||||
inner: "bg-ielts-reading-light",
|
||||
},
|
||||
listening: {
|
||||
progress: "text-ielts-listening",
|
||||
inner: "bg-ielts-listening-light",
|
||||
},
|
||||
writing: {
|
||||
progress: "text-ielts-writing",
|
||||
inner: "bg-ielts-writing-light",
|
||||
},
|
||||
speaking: {
|
||||
progress: "text-ielts-speaking",
|
||||
inner: "bg-ielts-speaking-light",
|
||||
},
|
||||
level: {
|
||||
progress: "text-ielts-level",
|
||||
inner: "bg-ielts-level-light",
|
||||
},
|
||||
};
|
||||
|
||||
const getTotalExercises = () => {
|
||||
const exam = exams.find((x) => x.module === selectedModule)!;
|
||||
if (exam.module === "reading" || exam.module === "listening") {
|
||||
return exam.parts.flatMap((x) => x.exercises).length;
|
||||
}
|
||||
const getTotalExercises = () => {
|
||||
const exam = exams.find((x) => x.module === selectedModule)!;
|
||||
if (exam.module === "reading" || exam.module === "listening") {
|
||||
return exam.parts.flatMap((x) => x.exercises).length;
|
||||
}
|
||||
|
||||
return exam.exercises.length;
|
||||
};
|
||||
return exam.exercises.length;
|
||||
};
|
||||
|
||||
const bandScore: number = calculateBandScore(selectedScore.correct, selectedScore.total, selectedModule, user.focus);
|
||||
const bandScore: number = calculateBandScore(
|
||||
selectedScore.correct,
|
||||
selectedScore.total,
|
||||
selectedModule,
|
||||
user.focus,
|
||||
);
|
||||
|
||||
const showLevel = (level: number) => {
|
||||
if (selectedModule === "level") {
|
||||
const [levelStr, grade] = getLevelScore(level);
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center gap-1">
|
||||
<span className="text-xl font-bold">{levelStr}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
const showLevel = (level: number) => {
|
||||
if (selectedModule === "level") {
|
||||
const [levelStr, grade] = getLevelScore(level);
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center gap-1">
|
||||
<span className="text-xl font-bold">{levelStr}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return <span className="text-3xl font-bold">{level}</span>;
|
||||
};
|
||||
return <span className="text-3xl font-bold">{level}</span>;
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="flex h-fit min-h-full w-full flex-col items-center justify-between gap-8">
|
||||
<ModuleTitle
|
||||
module={selectedModule}
|
||||
totalExercises={getTotalExercises()}
|
||||
exerciseIndex={getTotalExercises()}
|
||||
minTimer={exams.find((x) => x.module === selectedModule)!.minTimer}
|
||||
disableTimer
|
||||
/>
|
||||
<div className="flex gap-4 self-start">
|
||||
{modules.includes("reading") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("reading")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-reading flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "reading" ? "bg-ielts-reading text-white" : "bg-mti-gray-smoke text-ielts-reading",
|
||||
)}>
|
||||
<BsBook className="h-6 w-6" />
|
||||
<span className="font-semibold">Reading</span>
|
||||
</div>
|
||||
)}
|
||||
{modules.includes("listening") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("listening")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-listening flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "listening" ? "bg-ielts-listening text-white" : "bg-mti-gray-smoke text-ielts-listening",
|
||||
)}>
|
||||
<BsHeadphones className="h-6 w-6" />
|
||||
<span className="font-semibold">Listening</span>
|
||||
</div>
|
||||
)}
|
||||
{modules.includes("writing") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("writing")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-writing flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "writing" ? "bg-ielts-writing text-white" : "bg-mti-gray-smoke text-ielts-writing",
|
||||
)}>
|
||||
<BsPen className="h-6 w-6" />
|
||||
<span className="font-semibold">Writing</span>
|
||||
</div>
|
||||
)}
|
||||
{modules.includes("speaking") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("speaking")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-speaking flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "speaking" ? "bg-ielts-speaking text-white" : "bg-mti-gray-smoke text-ielts-speaking",
|
||||
)}>
|
||||
<BsMegaphone className="h-6 w-6" />
|
||||
<span className="font-semibold">Speaking</span>
|
||||
</div>
|
||||
)}
|
||||
{modules.includes("level") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("level")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-level flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "level" ? "bg-ielts-level text-white" : "bg-mti-gray-smoke text-ielts-level",
|
||||
)}>
|
||||
<BsClipboard className="h-6 w-6" />
|
||||
<span className="font-semibold">Level</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{isLoading && (
|
||||
<div className="absolute left-1/2 top-1/2 flex h-fit w-fit -translate-x-1/2 -translate-y-1/2 animate-pulse flex-col items-center gap-12">
|
||||
<span className={clsx("loading loading-infinity w-32", moduleColors[selectedModule].progress)} />
|
||||
<span className={clsx("text-center text-2xl font-bold", moduleColors[selectedModule].progress)}>
|
||||
Evaluating your answers, please be patient...
|
||||
<br />
|
||||
You can also check it later on your records page!
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{!isLoading && (
|
||||
<div className="mb-20 mt-32 flex w-full items-center justify-between gap-9">
|
||||
<span className="max-w-3xl">{moduleResultText(selectedModule, bandScore)}</span>
|
||||
<div className="flex gap-9 px-16">
|
||||
<div
|
||||
className={clsx("radial-progress overflow-hidden", moduleColors[selectedModule].progress)}
|
||||
style={
|
||||
{
|
||||
"--value": (selectedScore.correct / selectedScore.total) * 100,
|
||||
"--thickness": "12px",
|
||||
"--size": "13rem",
|
||||
} as any
|
||||
}>
|
||||
<div
|
||||
className={clsx(
|
||||
"flex h-48 w-48 flex-col items-center justify-center rounded-full",
|
||||
moduleColors[selectedModule].inner,
|
||||
)}>
|
||||
<span className="text-xl">Level</span>
|
||||
{showLevel(bandScore)}
|
||||
</div>
|
||||
</div>
|
||||
{!["writing", "speaking"].includes(selectedModule) ? (
|
||||
<div className="flex flex-col gap-5 w-28">
|
||||
<div className="flex gap-2">
|
||||
<div className="bg-mti-red-light mt-1 h-3 w-3 rounded-full" />
|
||||
<div className="flex flex-col">
|
||||
<span className="text-mti-red-light">
|
||||
{(((selectedScore.total - selectedScore.missing) / selectedScore.total) * 100).toFixed(0)}%
|
||||
</span>
|
||||
<span className="text-lg">Completion</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<div className="bg-mti-purple-light mt-1 h-3 w-3 rounded-full" />
|
||||
<div className="flex flex-col">
|
||||
<span className="text-mti-purple-light">{selectedScore.correct.toString().padStart(2, "0")}</span>
|
||||
<span className="text-lg">Correct</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<div className="bg-mti-rose-light mt-1 h-3 w-3 rounded-full" />
|
||||
<div className="flex flex-col">
|
||||
<span className="text-mti-rose-light">
|
||||
{(selectedScore.total - selectedScore.correct).toString().padStart(2, "0")}
|
||||
</span>
|
||||
<span className="text-lg">Wrong</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="w-28 h-full" />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
return (
|
||||
<>
|
||||
<div className="flex h-fit min-h-full w-full flex-col items-center justify-between gap-8">
|
||||
<ModuleTitle
|
||||
module={selectedModule}
|
||||
totalExercises={getTotalExercises()}
|
||||
exerciseIndex={getTotalExercises()}
|
||||
minTimer={exams.find((x) => x.module === selectedModule)!.minTimer}
|
||||
disableTimer
|
||||
/>
|
||||
<div className="flex gap-4 self-start">
|
||||
{modules.includes("reading") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("reading")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-reading flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "reading"
|
||||
? "bg-ielts-reading text-white"
|
||||
: "bg-mti-gray-smoke text-ielts-reading",
|
||||
)}
|
||||
>
|
||||
<BsBook className="h-6 w-6" />
|
||||
<span className="font-semibold">Reading</span>
|
||||
</div>
|
||||
)}
|
||||
{modules.includes("listening") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("listening")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-listening flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "listening"
|
||||
? "bg-ielts-listening text-white"
|
||||
: "bg-mti-gray-smoke text-ielts-listening",
|
||||
)}
|
||||
>
|
||||
<BsHeadphones className="h-6 w-6" />
|
||||
<span className="font-semibold">Listening</span>
|
||||
</div>
|
||||
)}
|
||||
{modules.includes("writing") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("writing")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-writing flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "writing"
|
||||
? "bg-ielts-writing text-white"
|
||||
: "bg-mti-gray-smoke text-ielts-writing",
|
||||
)}
|
||||
>
|
||||
<BsPen className="h-6 w-6" />
|
||||
<span className="font-semibold">Writing</span>
|
||||
</div>
|
||||
)}
|
||||
{modules.includes("speaking") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("speaking")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-speaking flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "speaking"
|
||||
? "bg-ielts-speaking text-white"
|
||||
: "bg-mti-gray-smoke text-ielts-speaking",
|
||||
)}
|
||||
>
|
||||
<BsMegaphone className="h-6 w-6" />
|
||||
<span className="font-semibold">Speaking</span>
|
||||
</div>
|
||||
)}
|
||||
{modules.includes("level") && (
|
||||
<div
|
||||
onClick={() => setSelectedModule("level")}
|
||||
className={clsx(
|
||||
"hover:bg-ielts-level flex cursor-pointer items-center gap-2 rounded-xl p-4 transition duration-300 ease-in-out hover:text-white hover:shadow-lg",
|
||||
selectedModule === "level"
|
||||
? "bg-ielts-level text-white"
|
||||
: "bg-mti-gray-smoke text-ielts-level",
|
||||
)}
|
||||
>
|
||||
<BsClipboard className="h-6 w-6" />
|
||||
<span className="font-semibold">Level</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{isLoading && (
|
||||
<div className="absolute left-1/2 top-1/2 flex h-fit w-fit -translate-x-1/2 -translate-y-1/2 animate-pulse flex-col items-center gap-12">
|
||||
<span
|
||||
className={clsx(
|
||||
"loading loading-infinity w-32",
|
||||
moduleColors[selectedModule].progress,
|
||||
)}
|
||||
/>
|
||||
<span
|
||||
className={clsx(
|
||||
"text-center text-2xl font-bold",
|
||||
moduleColors[selectedModule].progress,
|
||||
)}
|
||||
>
|
||||
Evaluating your answers, please be patient...
|
||||
<br />
|
||||
You can also check it later on your records page!
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{!isLoading && (
|
||||
<div className="mb-20 mt-32 flex w-full items-center justify-between gap-9">
|
||||
<span className="max-w-3xl">
|
||||
{moduleResultText(selectedModule, bandScore)}
|
||||
</span>
|
||||
<div className="flex gap-9 px-16">
|
||||
<div
|
||||
className={clsx(
|
||||
"radial-progress overflow-hidden",
|
||||
moduleColors[selectedModule].progress,
|
||||
)}
|
||||
style={
|
||||
{
|
||||
"--value":
|
||||
(selectedScore.correct / selectedScore.total) * 100,
|
||||
"--thickness": "12px",
|
||||
"--size": "13rem",
|
||||
} as any
|
||||
}
|
||||
>
|
||||
<div
|
||||
className={clsx(
|
||||
"flex h-48 w-48 flex-col items-center justify-center rounded-full",
|
||||
moduleColors[selectedModule].inner,
|
||||
)}
|
||||
>
|
||||
<span className="text-xl">Level</span>
|
||||
{showLevel(bandScore)}
|
||||
</div>
|
||||
</div>
|
||||
{!["writing", "speaking"].includes(selectedModule) ? (
|
||||
<div className="flex flex-col gap-5 w-28">
|
||||
<div className="flex gap-2">
|
||||
<div className="bg-mti-red-light mt-1 h-3 w-3 rounded-full" />
|
||||
<div className="flex flex-col">
|
||||
<span className="text-mti-red-light">
|
||||
{(
|
||||
((selectedScore.total - selectedScore.missing) /
|
||||
selectedScore.total) *
|
||||
100
|
||||
).toFixed(0)}
|
||||
%
|
||||
</span>
|
||||
<span className="text-lg">Completion</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<div className="bg-mti-purple-light mt-1 h-3 w-3 rounded-full" />
|
||||
<div className="flex flex-col">
|
||||
<span className="text-mti-purple-light">
|
||||
{selectedScore.correct.toString().padStart(2, "0")}
|
||||
</span>
|
||||
<span className="text-lg">Correct</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<div className="bg-mti-rose-light mt-1 h-3 w-3 rounded-full" />
|
||||
<div className="flex flex-col">
|
||||
<span className="text-mti-rose-light">
|
||||
{(selectedScore.total - selectedScore.correct)
|
||||
.toString()
|
||||
.padStart(2, "0")}
|
||||
</span>
|
||||
<span className="text-lg">Wrong</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="w-28 h-full" />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!isLoading && (
|
||||
<div className="absolute bottom-8 left-0 flex w-full justify-between gap-8 self-end px-8">
|
||||
<div className="flex gap-8">
|
||||
<div className="flex w-fit cursor-pointer flex-col items-center gap-1">
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
className="bg-mti-purple-light hover:bg-mti-purple flex h-11 w-11 items-center justify-center rounded-full transition duration-300 ease-in-out">
|
||||
<BsArrowCounterclockwise className="h-7 w-7 text-white" />
|
||||
</button>
|
||||
<span>Play Again</span>
|
||||
</div>
|
||||
<div className="flex w-fit cursor-pointer flex-col items-center gap-1">
|
||||
<button
|
||||
onClick={onViewResults}
|
||||
className="bg-mti-purple-light hover:bg-mti-purple flex h-11 w-11 items-center justify-center rounded-full transition duration-300 ease-in-out">
|
||||
<BsEyeFill className="h-7 w-7 text-white" />
|
||||
</button>
|
||||
<span>Review Answers</span>
|
||||
</div>
|
||||
</div>
|
||||
{!isLoading && (
|
||||
<div className="absolute bottom-8 left-0 flex w-full justify-between gap-8 self-end px-8">
|
||||
<div className="flex gap-8">
|
||||
<div className="flex w-fit cursor-pointer flex-col items-center gap-1">
|
||||
<button
|
||||
onClick={() => window.location.reload()}
|
||||
className="bg-mti-purple-light hover:bg-mti-purple flex h-11 w-11 items-center justify-center rounded-full transition duration-300 ease-in-out"
|
||||
>
|
||||
<BsArrowCounterclockwise className="h-7 w-7 text-white" />
|
||||
</button>
|
||||
<span>Play Again</span>
|
||||
</div>
|
||||
<div className="flex w-fit cursor-pointer flex-col items-center gap-1">
|
||||
<button
|
||||
onClick={() => onViewResults()}
|
||||
className="bg-mti-purple-light hover:bg-mti-purple flex h-11 w-11 items-center justify-center rounded-full transition duration-300 ease-in-out"
|
||||
>
|
||||
<BsEyeFill className="h-7 w-7 text-white" />
|
||||
</button>
|
||||
<span>Review All</span>
|
||||
</div>
|
||||
<div className="flex w-fit cursor-pointer flex-col items-center gap-1">
|
||||
<button
|
||||
onClick={() =>
|
||||
onViewResults(modules.findIndex((x) => x === selectedModule))
|
||||
}
|
||||
className="bg-mti-purple-light hover:bg-mti-purple flex h-11 w-11 items-center justify-center rounded-full transition duration-300 ease-in-out"
|
||||
>
|
||||
<BsEyeFill className="h-7 w-7 text-white" />
|
||||
</button>
|
||||
<span>Review {capitalize(selectedModule)}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Link href="/" className="w-full max-w-[200px] self-end">
|
||||
<Button color="purple" className="w-full max-w-[200px] self-end">
|
||||
Dashboard
|
||||
</Button>
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
<Link href="/" className="w-full max-w-[200px] self-end">
|
||||
<Button color="purple" className="w-full max-w-[200px] self-end">
|
||||
Dashboard
|
||||
</Button>
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* eslint-disable @next/next/no-img-element */
|
||||
import {Module} from "@/interfaces";
|
||||
import {useEffect, useState} from "react";
|
||||
import { Module } from "@/interfaces";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import AbandonPopup from "@/components/AbandonPopup";
|
||||
import Layout from "@/components/High/Layout";
|
||||
@@ -12,437 +12,567 @@ import Selection from "@/exams/Selection";
|
||||
import Speaking from "@/exams/Speaking";
|
||||
import Writing from "@/exams/Writing";
|
||||
import useUser from "@/hooks/useUser";
|
||||
import {Exam, UserSolution, Variant} from "@/interfaces/exam";
|
||||
import {Stat} from "@/interfaces/user";
|
||||
import { Exam, UserSolution, Variant } from "@/interfaces/exam";
|
||||
import { Stat } from "@/interfaces/user";
|
||||
import useExamStore from "@/stores/examStore";
|
||||
import {evaluateSpeakingAnswer, evaluateWritingAnswer} from "@/utils/evaluation";
|
||||
import {defaultExamUserSolutions, getExam} from "@/utils/exams";
|
||||
import {
|
||||
evaluateSpeakingAnswer,
|
||||
evaluateWritingAnswer,
|
||||
} from "@/utils/evaluation";
|
||||
import { defaultExamUserSolutions, getExam } from "@/utils/exams";
|
||||
import axios from "axios";
|
||||
import {useRouter} from "next/router";
|
||||
import {toast, ToastContainer} from "react-toastify";
|
||||
import {v4 as uuidv4} from "uuid";
|
||||
import { useRouter } from "next/router";
|
||||
import { toast, ToastContainer } from "react-toastify";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import useSessions from "@/hooks/useSessions";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
|
||||
interface Props {
|
||||
page: "exams" | "exercises";
|
||||
page: "exams" | "exercises";
|
||||
}
|
||||
|
||||
export default function ExamPage({page}: Props) {
|
||||
const [variant, setVariant] = useState<Variant>("full");
|
||||
const [avoidRepeated, setAvoidRepeated] = useState(false);
|
||||
const [hasBeenUploaded, setHasBeenUploaded] = useState(false);
|
||||
const [showAbandonPopup, setShowAbandonPopup] = useState(false);
|
||||
const [isEvaluationLoading, setIsEvaluationLoading] = useState(false);
|
||||
const [statsAwaitingEvaluation, setStatsAwaitingEvaluation] = useState<string[]>([]);
|
||||
const [timeSpent, setTimeSpent] = useState(0);
|
||||
export default function ExamPage({ page }: Props) {
|
||||
const [variant, setVariant] = useState<Variant>("full");
|
||||
const [avoidRepeated, setAvoidRepeated] = useState(false);
|
||||
const [hasBeenUploaded, setHasBeenUploaded] = useState(false);
|
||||
const [showAbandonPopup, setShowAbandonPopup] = useState(false);
|
||||
const [isEvaluationLoading, setIsEvaluationLoading] = useState(false);
|
||||
const [statsAwaitingEvaluation, setStatsAwaitingEvaluation] = useState<
|
||||
string[]
|
||||
>([]);
|
||||
const [timeSpent, setTimeSpent] = useState(0);
|
||||
|
||||
const resetStore = useExamStore((state) => state.reset);
|
||||
const assignment = useExamStore((state) => state.assignment);
|
||||
const initialTimeSpent = useExamStore((state) => state.timeSpent);
|
||||
const resetStore = useExamStore((state) => state.reset);
|
||||
const assignment = useExamStore((state) => state.assignment);
|
||||
const initialTimeSpent = useExamStore((state) => state.timeSpent);
|
||||
|
||||
const examStore = useExamStore;
|
||||
const examStore = useExamStore;
|
||||
|
||||
const {exam, setExam} = useExamStore((state) => state);
|
||||
const {exams, setExams} = useExamStore((state) => state);
|
||||
const {sessionId, setSessionId} = useExamStore((state) => state);
|
||||
const {partIndex, setPartIndex} = useExamStore((state) => state);
|
||||
const {moduleIndex, setModuleIndex} = useExamStore((state) => state);
|
||||
const {questionIndex, setQuestionIndex} = useExamStore((state) => state);
|
||||
const {exerciseIndex, setExerciseIndex} = useExamStore((state) => state);
|
||||
const {userSolutions, setUserSolutions} = useExamStore((state) => state);
|
||||
const {showSolutions, setShowSolutions} = useExamStore((state) => state);
|
||||
const {selectedModules, setSelectedModules} = useExamStore((state) => state);
|
||||
const { exam, setExam } = useExamStore((state) => state);
|
||||
const { exams, setExams } = useExamStore((state) => state);
|
||||
const { sessionId, setSessionId } = useExamStore((state) => state);
|
||||
const { partIndex, setPartIndex } = useExamStore((state) => state);
|
||||
const { moduleIndex, setModuleIndex } = useExamStore((state) => state);
|
||||
const { questionIndex, setQuestionIndex } = useExamStore((state) => state);
|
||||
const { exerciseIndex, setExerciseIndex } = useExamStore((state) => state);
|
||||
const { userSolutions, setUserSolutions } = useExamStore((state) => state);
|
||||
const { showSolutions, setShowSolutions } = useExamStore((state) => state);
|
||||
const { selectedModules, setSelectedModules } = useExamStore(
|
||||
(state) => state,
|
||||
);
|
||||
|
||||
const {user} = useUser({redirectTo: "/login"});
|
||||
const router = useRouter();
|
||||
const { user } = useUser({ redirectTo: "/login" });
|
||||
const router = useRouter();
|
||||
|
||||
const reset = () => {
|
||||
resetStore();
|
||||
setVariant("full");
|
||||
setAvoidRepeated(false);
|
||||
setHasBeenUploaded(false);
|
||||
setShowAbandonPopup(false);
|
||||
setIsEvaluationLoading(false);
|
||||
setStatsAwaitingEvaluation([]);
|
||||
setTimeSpent(0);
|
||||
};
|
||||
const reset = () => {
|
||||
resetStore();
|
||||
setVariant("full");
|
||||
setAvoidRepeated(false);
|
||||
setHasBeenUploaded(false);
|
||||
setShowAbandonPopup(false);
|
||||
setIsEvaluationLoading(false);
|
||||
setStatsAwaitingEvaluation([]);
|
||||
setTimeSpent(0);
|
||||
};
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
const saveSession = async () => {
|
||||
console.log("Saving your session...");
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
const saveSession = async () => {
|
||||
console.log("Saving your session...");
|
||||
|
||||
await axios.post("/api/sessions", {
|
||||
id: sessionId,
|
||||
sessionId,
|
||||
date: new Date().toISOString(),
|
||||
userSolutions,
|
||||
moduleIndex,
|
||||
selectedModules,
|
||||
assignment,
|
||||
timeSpent,
|
||||
exams,
|
||||
exam,
|
||||
partIndex,
|
||||
exerciseIndex,
|
||||
questionIndex,
|
||||
user: user?.id,
|
||||
});
|
||||
};
|
||||
await axios.post("/api/sessions", {
|
||||
id: sessionId,
|
||||
sessionId,
|
||||
date: new Date().toISOString(),
|
||||
userSolutions,
|
||||
moduleIndex,
|
||||
selectedModules,
|
||||
assignment,
|
||||
timeSpent,
|
||||
exams,
|
||||
exam,
|
||||
partIndex,
|
||||
exerciseIndex,
|
||||
questionIndex,
|
||||
user: user?.id,
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => setTimeSpent((prev) => prev + initialTimeSpent), [initialTimeSpent]);
|
||||
useEffect(
|
||||
() => setTimeSpent((prev) => prev + initialTimeSpent),
|
||||
[initialTimeSpent],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (userSolutions.length === 0 && exams.length > 0) {
|
||||
const defaultSolutions = exams.map(defaultExamUserSolutions).flat();
|
||||
setUserSolutions(defaultSolutions);
|
||||
}
|
||||
}, [exams, setUserSolutions, userSolutions]);
|
||||
useEffect(() => {
|
||||
if (userSolutions.length === 0 && exams.length > 0) {
|
||||
const defaultSolutions = exams.map(defaultExamUserSolutions).flat();
|
||||
setUserSolutions(defaultSolutions);
|
||||
}
|
||||
}, [exams, setUserSolutions, userSolutions]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
sessionId.length > 0 &&
|
||||
userSolutions.length > 0 &&
|
||||
selectedModules.length > 0 &&
|
||||
exams.length > 0 &&
|
||||
!!exam &&
|
||||
timeSpent > 0 &&
|
||||
!showSolutions &&
|
||||
moduleIndex < selectedModules.length
|
||||
)
|
||||
saveSession();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [assignment, exam, exams, moduleIndex, selectedModules, sessionId, userSolutions, user, exerciseIndex, partIndex, questionIndex]);
|
||||
useEffect(() => {
|
||||
if (
|
||||
sessionId.length > 0 &&
|
||||
userSolutions.length > 0 &&
|
||||
selectedModules.length > 0 &&
|
||||
exams.length > 0 &&
|
||||
!!exam &&
|
||||
timeSpent > 0 &&
|
||||
!showSolutions &&
|
||||
moduleIndex < selectedModules.length
|
||||
)
|
||||
saveSession();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [
|
||||
assignment,
|
||||
exam,
|
||||
exams,
|
||||
moduleIndex,
|
||||
selectedModules,
|
||||
sessionId,
|
||||
userSolutions,
|
||||
user,
|
||||
exerciseIndex,
|
||||
partIndex,
|
||||
questionIndex,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (timeSpent % 20 === 0 && timeSpent > 0 && moduleIndex < selectedModules.length && !showSolutions) saveSession();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [timeSpent]);
|
||||
useEffect(() => {
|
||||
if (
|
||||
timeSpent % 20 === 0 &&
|
||||
timeSpent > 0 &&
|
||||
moduleIndex < selectedModules.length &&
|
||||
!showSolutions
|
||||
)
|
||||
saveSession();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [timeSpent]);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedModules.length > 0 && sessionId.length === 0) {
|
||||
const shortUID = new ShortUniqueId();
|
||||
setSessionId(shortUID.randomUUID(8));
|
||||
}
|
||||
}, [setSessionId, selectedModules, sessionId]);
|
||||
useEffect(() => {
|
||||
if (selectedModules.length > 0 && sessionId.length === 0) {
|
||||
const shortUID = new ShortUniqueId();
|
||||
setSessionId(shortUID.randomUUID(8));
|
||||
}
|
||||
}, [setSessionId, selectedModules, sessionId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (user?.type === "developer") console.log(exam);
|
||||
}, [exam, user]);
|
||||
useEffect(() => {
|
||||
if (user?.type === "developer") console.log(exam);
|
||||
}, [exam, user]);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedModules.length > 0 && timeSpent === 0 && !showSolutions) {
|
||||
const timerInterval = setInterval(() => {
|
||||
setTimeSpent((prev) => prev + 1);
|
||||
}, 1000);
|
||||
useEffect(() => {
|
||||
if (selectedModules.length > 0 && timeSpent === 0 && !showSolutions) {
|
||||
const timerInterval = setInterval(() => {
|
||||
setTimeSpent((prev) => prev + 1);
|
||||
}, 1000);
|
||||
|
||||
return () => {
|
||||
clearInterval(timerInterval);
|
||||
};
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedModules.length]);
|
||||
return () => {
|
||||
clearInterval(timerInterval);
|
||||
};
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedModules.length]);
|
||||
|
||||
useEffect(() => {
|
||||
if (showSolutions) setModuleIndex(-1);
|
||||
}, [setModuleIndex, showSolutions]);
|
||||
useEffect(() => {
|
||||
if (showSolutions) setModuleIndex(-1);
|
||||
}, [setModuleIndex, showSolutions]);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
if (selectedModules.length > 0 && exams.length > 0 && moduleIndex < selectedModules.length) {
|
||||
const nextExam = exams[moduleIndex];
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
if (
|
||||
selectedModules.length > 0 &&
|
||||
exams.length > 0 &&
|
||||
moduleIndex < selectedModules.length
|
||||
) {
|
||||
const nextExam = exams[moduleIndex];
|
||||
|
||||
if (partIndex === -1 && nextExam.module !== "listening") setPartIndex(0);
|
||||
if (exerciseIndex === -1 && !["reading", "listening"].includes(nextExam.module)) setExerciseIndex(0);
|
||||
setExam(nextExam ? updateExamWithUserSolutions(nextExam) : undefined);
|
||||
}
|
||||
})();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedModules, moduleIndex, exams]);
|
||||
if (partIndex === -1 && nextExam.module !== "listening")
|
||||
setPartIndex(0);
|
||||
if (
|
||||
exerciseIndex === -1 &&
|
||||
!["reading", "listening"].includes(nextExam?.module)
|
||||
)
|
||||
setExerciseIndex(0);
|
||||
setExam(nextExam ? updateExamWithUserSolutions(nextExam) : undefined);
|
||||
}
|
||||
})();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedModules, moduleIndex, exams]);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
if (selectedModules.length > 0 && exams.length === 0) {
|
||||
const examPromises = selectedModules.map((module) =>
|
||||
getExam(
|
||||
module,
|
||||
avoidRepeated,
|
||||
variant,
|
||||
user?.type === "student" || user?.type === "developer" ? user.preferredGender : undefined,
|
||||
),
|
||||
);
|
||||
Promise.all(examPromises).then((values) => {
|
||||
if (values.every((x) => !!x)) {
|
||||
setExams(values.map((x) => x!));
|
||||
} else {
|
||||
toast.error("Something went wrong, please try again");
|
||||
setTimeout(router.reload, 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
})();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedModules, setExams, exams]);
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
if (selectedModules.length > 0 && exams.length === 0) {
|
||||
const examPromises = selectedModules.map((module) =>
|
||||
getExam(
|
||||
module,
|
||||
avoidRepeated,
|
||||
variant,
|
||||
user?.type === "student" || user?.type === "developer"
|
||||
? user.preferredGender
|
||||
: undefined,
|
||||
),
|
||||
);
|
||||
Promise.all(examPromises).then((values) => {
|
||||
if (values.every((x) => !!x)) {
|
||||
setExams(values.map((x) => x!));
|
||||
} else {
|
||||
toast.error("Something went wrong, please try again");
|
||||
setTimeout(router.reload, 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
})();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedModules, setExams, exams]);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedModules.length > 0 && exams.length !== 0 && moduleIndex >= selectedModules.length && !hasBeenUploaded && !showSolutions) {
|
||||
const newStats: Stat[] = userSolutions.map((solution) => ({
|
||||
...solution,
|
||||
id: solution.id || uuidv4(),
|
||||
timeSpent,
|
||||
session: sessionId,
|
||||
exam: solution.exam!,
|
||||
module: solution.module!,
|
||||
user: user?.id || "",
|
||||
date: new Date().getTime(),
|
||||
isDisabled: solution.isDisabled,
|
||||
...(assignment ? {assignment: assignment.id} : {}),
|
||||
}));
|
||||
useEffect(() => {
|
||||
if (
|
||||
selectedModules.length > 0 &&
|
||||
exams.length !== 0 &&
|
||||
moduleIndex >= selectedModules.length &&
|
||||
!hasBeenUploaded &&
|
||||
!showSolutions
|
||||
) {
|
||||
const newStats: Stat[] = userSolutions.map((solution) => ({
|
||||
...solution,
|
||||
id: solution.id || uuidv4(),
|
||||
timeSpent,
|
||||
session: sessionId,
|
||||
exam: solution.exam!,
|
||||
module: solution.module!,
|
||||
user: user?.id || "",
|
||||
date: new Date().getTime(),
|
||||
isDisabled: solution.isDisabled,
|
||||
...(assignment ? { assignment: assignment.id } : {}),
|
||||
}));
|
||||
|
||||
axios
|
||||
.post<{ok: boolean}>("/api/stats", newStats)
|
||||
.then((response) => setHasBeenUploaded(response.data.ok))
|
||||
.catch(() => setHasBeenUploaded(false));
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedModules, moduleIndex, hasBeenUploaded]);
|
||||
axios
|
||||
.post<{ ok: boolean }>("/api/stats", newStats)
|
||||
.then((response) => setHasBeenUploaded(response.data.ok))
|
||||
.catch(() => setHasBeenUploaded(false));
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedModules, moduleIndex, hasBeenUploaded]);
|
||||
|
||||
useEffect(() => {
|
||||
setIsEvaluationLoading(statsAwaitingEvaluation.length !== 0);
|
||||
}, [statsAwaitingEvaluation]);
|
||||
useEffect(() => {
|
||||
setIsEvaluationLoading(statsAwaitingEvaluation.length !== 0);
|
||||
}, [statsAwaitingEvaluation]);
|
||||
|
||||
useEffect(() => {
|
||||
if (statsAwaitingEvaluation.length > 0) {
|
||||
checkIfStatsHaveBeenEvaluated(statsAwaitingEvaluation);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [statsAwaitingEvaluation]);
|
||||
useEffect(() => {
|
||||
if (statsAwaitingEvaluation.length > 0) {
|
||||
checkIfStatsHaveBeenEvaluated(statsAwaitingEvaluation);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [statsAwaitingEvaluation]);
|
||||
|
||||
const checkIfStatsHaveBeenEvaluated = (ids: string[]) => {
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
const awaitedStats = await Promise.all(ids.map(async (id) => (await axios.get<Stat>(`/api/stats/${id}`)).data));
|
||||
const solutionsEvaluated = awaitedStats.every((stat) => stat.solutions.every((x) => x.evaluation !== null));
|
||||
if (solutionsEvaluated) {
|
||||
const statsUserSolutions: UserSolution[] = awaitedStats.map((stat) => ({
|
||||
id: stat.id,
|
||||
exercise: stat.exercise,
|
||||
score: stat.score,
|
||||
solutions: stat.solutions,
|
||||
type: stat.type,
|
||||
exam: stat.exam,
|
||||
module: stat.module,
|
||||
}));
|
||||
const checkIfStatsHaveBeenEvaluated = (ids: string[]) => {
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
const awaitedStats = await Promise.all(
|
||||
ids.map(
|
||||
async (id) => (await axios.get<Stat>(`/api/stats/${id}`)).data,
|
||||
),
|
||||
);
|
||||
const solutionsEvaluated = awaitedStats.every((stat) =>
|
||||
stat.solutions.every((x) => x.evaluation !== null),
|
||||
);
|
||||
if (solutionsEvaluated) {
|
||||
const statsUserSolutions: UserSolution[] = awaitedStats.map(
|
||||
(stat) => ({
|
||||
id: stat.id,
|
||||
exercise: stat.exercise,
|
||||
score: stat.score,
|
||||
solutions: stat.solutions,
|
||||
type: stat.type,
|
||||
exam: stat.exam,
|
||||
module: stat.module,
|
||||
}),
|
||||
);
|
||||
|
||||
const updatedUserSolutions = userSolutions.map((x) => {
|
||||
const respectiveSolution = statsUserSolutions.find((y) => y.exercise === x.exercise);
|
||||
return respectiveSolution ? respectiveSolution : x;
|
||||
});
|
||||
const updatedUserSolutions = userSolutions.map((x) => {
|
||||
const respectiveSolution = statsUserSolutions.find(
|
||||
(y) => y.exercise === x.exercise,
|
||||
);
|
||||
return respectiveSolution ? respectiveSolution : x;
|
||||
});
|
||||
|
||||
setUserSolutions(updatedUserSolutions);
|
||||
return setStatsAwaitingEvaluation((prev) => prev.filter((x) => !ids.includes(x)));
|
||||
}
|
||||
setUserSolutions(updatedUserSolutions);
|
||||
return setStatsAwaitingEvaluation((prev) =>
|
||||
prev.filter((x) => !ids.includes(x)),
|
||||
);
|
||||
}
|
||||
|
||||
return checkIfStatsHaveBeenEvaluated(ids);
|
||||
} catch {
|
||||
return checkIfStatsHaveBeenEvaluated(ids);
|
||||
}
|
||||
}, 5 * 1000);
|
||||
};
|
||||
return checkIfStatsHaveBeenEvaluated(ids);
|
||||
} catch {
|
||||
return checkIfStatsHaveBeenEvaluated(ids);
|
||||
}
|
||||
}, 5 * 1000);
|
||||
};
|
||||
|
||||
const updateExamWithUserSolutions = (exam: Exam): Exam => {
|
||||
if (exam.module === "reading" || exam.module === "listening") {
|
||||
const parts = exam.parts.map((p) =>
|
||||
Object.assign(p, {
|
||||
exercises: p.exercises.map((x) =>
|
||||
Object.assign(x, {
|
||||
userSolutions: userSolutions.find((y) => x.id === y.exercise)?.solutions,
|
||||
}),
|
||||
),
|
||||
}),
|
||||
);
|
||||
return Object.assign(exam, {parts});
|
||||
}
|
||||
const updateExamWithUserSolutions = (exam: Exam): Exam => {
|
||||
if (exam.module === "reading" || exam.module === "listening") {
|
||||
const parts = exam.parts.map((p) =>
|
||||
Object.assign(p, {
|
||||
exercises: p.exercises.map((x) =>
|
||||
Object.assign(x, {
|
||||
userSolutions: userSolutions.find((y) => x.id === y.exercise)
|
||||
?.solutions,
|
||||
}),
|
||||
),
|
||||
}),
|
||||
);
|
||||
return Object.assign(exam, { parts });
|
||||
}
|
||||
|
||||
const exercises = exam.exercises.map((x) =>
|
||||
Object.assign(x, {
|
||||
userSolutions: userSolutions.find((y) => x.id === y.exercise)?.solutions,
|
||||
}),
|
||||
);
|
||||
return Object.assign(exam, {exercises});
|
||||
};
|
||||
const exercises = exam.exercises.map((x) =>
|
||||
Object.assign(x, {
|
||||
userSolutions: userSolutions.find((y) => x.id === y.exercise)
|
||||
?.solutions,
|
||||
}),
|
||||
);
|
||||
return Object.assign(exam, { exercises });
|
||||
};
|
||||
|
||||
const onFinish = async (solutions: UserSolution[]) => {
|
||||
const solutionIds = solutions.map((x) => x.exercise);
|
||||
const solutionExams = solutions.map((x) => x.exam);
|
||||
const onFinish = async (solutions: UserSolution[]) => {
|
||||
const solutionIds = solutions.map((x) => x.exercise);
|
||||
const solutionExams = solutions.map((x) => x.exam);
|
||||
|
||||
let newSolutions = [...solutions];
|
||||
let newSolutions = [...solutions];
|
||||
|
||||
if (exam && !solutionExams.includes(exam.id)) return;
|
||||
if (exam && !solutionExams.includes(exam.id)) return;
|
||||
|
||||
if (exam && (exam.module === "writing" || exam.module === "speaking") && solutions.length > 0 && !showSolutions) {
|
||||
setHasBeenUploaded(true);
|
||||
setIsEvaluationLoading(true);
|
||||
if (
|
||||
exam &&
|
||||
(exam.module === "writing" || exam.module === "speaking") &&
|
||||
solutions.length > 0 &&
|
||||
!showSolutions
|
||||
) {
|
||||
setHasBeenUploaded(true);
|
||||
setIsEvaluationLoading(true);
|
||||
|
||||
const responses: UserSolution[] = (
|
||||
await Promise.all(
|
||||
exam.exercises.map(async (exercise, index) => {
|
||||
const evaluationID = uuidv4();
|
||||
if (exercise.type === "writing")
|
||||
return await evaluateWritingAnswer(exercise, index + 1, solutions.find((x) => x.exercise === exercise.id)!, evaluationID);
|
||||
const responses: UserSolution[] = (
|
||||
await Promise.all(
|
||||
exam.exercises.map(async (exercise, index) => {
|
||||
const evaluationID = uuidv4();
|
||||
if (exercise.type === "writing")
|
||||
return await evaluateWritingAnswer(
|
||||
exercise,
|
||||
index + 1,
|
||||
solutions.find((x) => x.exercise === exercise.id)!,
|
||||
evaluationID,
|
||||
);
|
||||
|
||||
if (exercise.type === "interactiveSpeaking" || exercise.type === "speaking")
|
||||
return await evaluateSpeakingAnswer(exercise, solutions.find((x) => x.exercise === exercise.id)!, evaluationID);
|
||||
}),
|
||||
)
|
||||
).filter((x) => !!x) as UserSolution[];
|
||||
if (
|
||||
exercise.type === "interactiveSpeaking" ||
|
||||
exercise.type === "speaking"
|
||||
)
|
||||
return await evaluateSpeakingAnswer(
|
||||
exercise,
|
||||
solutions.find((x) => x.exercise === exercise.id)!,
|
||||
evaluationID,
|
||||
);
|
||||
}),
|
||||
)
|
||||
).filter((x) => !!x) as UserSolution[];
|
||||
|
||||
newSolutions = [...newSolutions.filter((x) => !responses.map((y) => y.exercise).includes(x.exercise)), ...responses];
|
||||
setStatsAwaitingEvaluation((prev) => [...prev, ...responses.filter((x) => !!x).map((r) => (r as any).id)]);
|
||||
setHasBeenUploaded(false);
|
||||
}
|
||||
newSolutions = [
|
||||
...newSolutions.filter(
|
||||
(x) => !responses.map((y) => y.exercise).includes(x.exercise),
|
||||
),
|
||||
...responses,
|
||||
];
|
||||
setStatsAwaitingEvaluation((prev) => [
|
||||
...prev,
|
||||
...responses.filter((x) => !!x).map((r) => (r as any).id),
|
||||
]);
|
||||
setHasBeenUploaded(false);
|
||||
}
|
||||
|
||||
axios.get("/api/stats/update");
|
||||
axios.get("/api/stats/update");
|
||||
|
||||
setUserSolutions([...userSolutions.filter((x) => !solutionIds.includes(x.exercise)), ...newSolutions]);
|
||||
setModuleIndex(moduleIndex + 1);
|
||||
setUserSolutions([
|
||||
...userSolutions.filter((x) => !solutionIds.includes(x.exercise)),
|
||||
...newSolutions,
|
||||
]);
|
||||
setModuleIndex(moduleIndex + 1);
|
||||
|
||||
setPartIndex(-1);
|
||||
setExerciseIndex(-1);
|
||||
setQuestionIndex(0);
|
||||
};
|
||||
setPartIndex(-1);
|
||||
setExerciseIndex(-1);
|
||||
setQuestionIndex(0);
|
||||
};
|
||||
|
||||
const aggregateScoresByModule = (): {module: Module; total: number; missing: number; correct: number}[] => {
|
||||
const scores: {
|
||||
[key in Module]: {total: number; missing: number; correct: number};
|
||||
} = {
|
||||
reading: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
listening: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
writing: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
speaking: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
level: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
};
|
||||
const aggregateScoresByModule = (): {
|
||||
module: Module;
|
||||
total: number;
|
||||
missing: number;
|
||||
correct: number;
|
||||
}[] => {
|
||||
const scores: {
|
||||
[key in Module]: { total: number; missing: number; correct: number };
|
||||
} = {
|
||||
reading: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
listening: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
writing: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
speaking: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
level: {
|
||||
total: 0,
|
||||
correct: 0,
|
||||
missing: 0,
|
||||
},
|
||||
};
|
||||
|
||||
userSolutions.forEach((x) => {
|
||||
const examModule =
|
||||
x.module || (x.type === "writing" ? "writing" : x.type === "speaking" || x.type === "interactiveSpeaking" ? "speaking" : undefined);
|
||||
userSolutions.forEach((x) => {
|
||||
const examModule =
|
||||
x.module ||
|
||||
(x.type === "writing"
|
||||
? "writing"
|
||||
: x.type === "speaking" || x.type === "interactiveSpeaking"
|
||||
? "speaking"
|
||||
: undefined);
|
||||
|
||||
scores[examModule!] = {
|
||||
total: scores[examModule!].total + x.score.total,
|
||||
correct: scores[examModule!].correct + x.score.correct,
|
||||
missing: scores[examModule!].missing + x.score.missing,
|
||||
};
|
||||
});
|
||||
scores[examModule!] = {
|
||||
total: scores[examModule!].total + x.score.total,
|
||||
correct: scores[examModule!].correct + x.score.correct,
|
||||
missing: scores[examModule!].missing + x.score.missing,
|
||||
};
|
||||
});
|
||||
|
||||
return Object.keys(scores)
|
||||
.filter((x) => scores[x as Module].total > 0)
|
||||
.map((x) => ({module: x as Module, ...scores[x as Module]}));
|
||||
};
|
||||
return Object.keys(scores)
|
||||
.filter((x) => scores[x as Module].total > 0)
|
||||
.map((x) => ({ module: x as Module, ...scores[x as Module] }));
|
||||
};
|
||||
|
||||
const renderScreen = () => {
|
||||
if (selectedModules.length === 0) {
|
||||
return (
|
||||
<Selection
|
||||
page={page}
|
||||
user={user!}
|
||||
disableSelection={page === "exams"}
|
||||
onStart={(modules: Module[], avoid: boolean, variant: Variant) => {
|
||||
setModuleIndex(0);
|
||||
setAvoidRepeated(avoid);
|
||||
setSelectedModules(modules);
|
||||
setVariant(variant);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
const renderScreen = () => {
|
||||
if (selectedModules.length === 0) {
|
||||
return (
|
||||
<Selection
|
||||
page={page}
|
||||
user={user!}
|
||||
disableSelection={page === "exams"}
|
||||
onStart={(modules: Module[], avoid: boolean, variant: Variant) => {
|
||||
setModuleIndex(0);
|
||||
setAvoidRepeated(avoid);
|
||||
setSelectedModules(modules);
|
||||
setVariant(variant);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (moduleIndex >= selectedModules.length || moduleIndex === -1) {
|
||||
return (
|
||||
<Finish
|
||||
isLoading={isEvaluationLoading}
|
||||
user={user!}
|
||||
modules={selectedModules}
|
||||
onViewResults={() => {
|
||||
setShowSolutions(true);
|
||||
setModuleIndex(0);
|
||||
setExerciseIndex(["reading", "listening"].includes(exams[0].module) ? -1 : 0);
|
||||
setPartIndex(exams[0].module === "listening" ? -1 : 0);
|
||||
setExam(exams[0]);
|
||||
}}
|
||||
scores={aggregateScoresByModule()}
|
||||
/>
|
||||
);
|
||||
}
|
||||
if (moduleIndex >= selectedModules.length || moduleIndex === -1) {
|
||||
return (
|
||||
<Finish
|
||||
isLoading={isEvaluationLoading}
|
||||
user={user!}
|
||||
modules={selectedModules}
|
||||
onViewResults={(index?: number) => {
|
||||
setShowSolutions(true);
|
||||
setModuleIndex(index || 0);
|
||||
setExerciseIndex(
|
||||
["reading", "listening"].includes(exams[0].module) ? -1 : 0,
|
||||
);
|
||||
setPartIndex(exams[0].module === "listening" ? -1 : 0);
|
||||
setExam(exams[0]);
|
||||
}}
|
||||
scores={aggregateScoresByModule()}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (exam && exam.module === "reading") {
|
||||
return <Reading exam={exam} onFinish={onFinish} showSolutions={showSolutions} />;
|
||||
}
|
||||
if (exam && exam.module === "reading") {
|
||||
return (
|
||||
<Reading
|
||||
exam={exam}
|
||||
onFinish={onFinish}
|
||||
showSolutions={showSolutions}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (exam && exam.module === "listening") {
|
||||
return <Listening exam={exam} onFinish={onFinish} showSolutions={showSolutions} />;
|
||||
}
|
||||
if (exam && exam.module === "listening") {
|
||||
return (
|
||||
<Listening
|
||||
exam={exam}
|
||||
onFinish={onFinish}
|
||||
showSolutions={showSolutions}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (exam && exam.module === "writing") {
|
||||
return <Writing exam={exam} onFinish={onFinish} showSolutions={showSolutions} />;
|
||||
}
|
||||
if (exam && exam.module === "writing") {
|
||||
return (
|
||||
<Writing
|
||||
exam={exam}
|
||||
onFinish={onFinish}
|
||||
showSolutions={showSolutions}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (exam && exam.module === "speaking") {
|
||||
return <Speaking exam={exam} onFinish={onFinish} showSolutions={showSolutions} />;
|
||||
}
|
||||
if (exam && exam.module === "speaking") {
|
||||
return (
|
||||
<Speaking
|
||||
exam={exam}
|
||||
onFinish={onFinish}
|
||||
showSolutions={showSolutions}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (exam && exam.module === "level") {
|
||||
return <Level exam={exam} onFinish={onFinish} showSolutions={showSolutions} />;
|
||||
}
|
||||
if (exam && exam.module === "level") {
|
||||
return (
|
||||
<Level exam={exam} onFinish={onFinish} showSolutions={showSolutions} />
|
||||
);
|
||||
}
|
||||
|
||||
return <>Loading...</>;
|
||||
};
|
||||
return <>Loading...</>;
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<ToastContainer />
|
||||
{user && (
|
||||
<Layout
|
||||
user={user}
|
||||
className="justify-between"
|
||||
focusMode={selectedModules.length !== 0 && !showSolutions && moduleIndex < selectedModules.length}
|
||||
onFocusLayerMouseEnter={() => setShowAbandonPopup(true)}>
|
||||
<>
|
||||
{renderScreen()}
|
||||
{!showSolutions && moduleIndex < selectedModules.length && (
|
||||
<AbandonPopup
|
||||
isOpen={showAbandonPopup}
|
||||
abandonPopupTitle="Leave Exercise"
|
||||
abandonPopupDescription="Are you sure you want to leave the exercise? Your progress will be saved and this exam can be resumed on the Dashboard."
|
||||
abandonConfirmButtonText="Confirm"
|
||||
onAbandon={() => {
|
||||
reset();
|
||||
}}
|
||||
onCancel={() => setShowAbandonPopup(false)}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
</Layout>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
return (
|
||||
<>
|
||||
<ToastContainer />
|
||||
{user && (
|
||||
<Layout
|
||||
user={user}
|
||||
className="justify-between"
|
||||
focusMode={
|
||||
selectedModules.length !== 0 &&
|
||||
!showSolutions &&
|
||||
moduleIndex < selectedModules.length
|
||||
}
|
||||
onFocusLayerMouseEnter={() => setShowAbandonPopup(true)}
|
||||
>
|
||||
<>
|
||||
{renderScreen()}
|
||||
{!showSolutions && moduleIndex < selectedModules.length && (
|
||||
<AbandonPopup
|
||||
isOpen={showAbandonPopup}
|
||||
abandonPopupTitle="Leave Exercise"
|
||||
abandonPopupDescription="Are you sure you want to leave the exercise? Your progress will be saved and this exam can be resumed on the Dashboard."
|
||||
abandonConfirmButtonText="Confirm"
|
||||
onAbandon={() => {
|
||||
reset();
|
||||
}}
|
||||
onCancel={() => setShowAbandonPopup(false)}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
</Layout>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user