diff --git a/src/pages/(generation)/LevelGeneration.tsx b/src/pages/(generation)/LevelGeneration.tsx new file mode 100644 index 00000000..52e81c5a --- /dev/null +++ b/src/pages/(generation)/LevelGeneration.tsx @@ -0,0 +1,207 @@ +import {LevelExam, MultipleChoiceExercise} from "@/interfaces/exam"; +import useExamStore from "@/stores/examStore"; +import {getExamById} from "@/utils/exams"; +import {Tab} from "@headlessui/react"; +import axios from "axios"; +import clsx from "clsx"; +import {useRouter} from "next/router"; +import {useState} from "react"; +import {BsArrowRepeat} from "react-icons/bs"; +import {toast} from "react-toastify"; +import {v4} from "uuid"; + +const TaskTab = ({exam, setExam}: {exam?: LevelExam; setExam: (exam: LevelExam) => void}) => { + const [isLoading, setIsLoading] = useState(false); + + const generate = () => { + setIsLoading(true); + axios + .get(`/api/exam/level/generate/level`) + .then((result) => { + console.log(result.data); + setExam(result.data); + }) + .catch((error) => { + console.log(error); + toast.error("Something went wrong!"); + }) + .finally(() => setIsLoading(false)); + }; + + return ( + + + + {isLoading ? ( + + + + ) : ( + "Generate" + )} + + + {isLoading && ( + + + Generating... + + )} + {exam && ( + + {exam.exercises + .filter((x) => x.type === "multipleChoice") + .map((ex) => { + const exercise = ex as MultipleChoiceExercise; + + return ( + + + Multiple Choice + + {exercise.questions.length} questions + + + + {exercise.questions.map((question) => ( + + + {question.id}. {question.prompt} + + + ({question.solution}){" "} + {question.options.find((o) => o.id === question.solution)?.text} + + + ))} + + + ); + })} + + )} + + ); +}; + +const LevelGeneration = () => { + const [generatedExam, setGeneratedExam] = useState(); + const [isLoading, setIsLoading] = useState(false); + const [resultingExam, setResultingExam] = useState(); + + const router = useRouter(); + + const setExams = useExamStore((state) => state.setExams); + const setSelectedModules = useExamStore((state) => state.setSelectedModules); + + const loadExam = async (examId: string) => { + const exam = await getExamById("level", examId.trim()); + if (!exam) { + toast.error("Unknown Exam ID! Please make sure you selected the right module and entered the right exam ID", { + toastId: "invalid-exam-id", + }); + + return; + } + + setExams([exam]); + setSelectedModules(["level"]); + + router.push("/exercises"); + }; + + const submitExam = () => { + if (!generatedExam) { + toast.error("Please generate all tasks before submitting"); + return; + } + + setIsLoading(true); + const exam: LevelExam = { + ...generatedExam, + isDiagnostic: false, + minTimer: 25, + module: "level", + id: v4(), + }; + + axios + .post(`/api/exam/level`, exam) + .then((result) => { + console.log(`Generated Exam ID: ${result.data.id}`); + toast.success("This new exam has been generated successfully! Check the ID in our browser's console."); + setResultingExam(result.data); + + setGeneratedExam(undefined); + }) + .catch((error) => { + console.log(error); + toast.error("Something went wrong while generating, please try again later."); + }) + .finally(() => setIsLoading(false)); + }; + + return ( + <> + + + + clsx( + "w-full rounded-lg py-2.5 text-sm font-medium leading-5 text-ielts-level/70", + "ring-white ring-opacity-60 ring-offset-2 ring-offset-ielts-level focus:outline-none focus:ring-2", + "transition duration-300 ease-in-out", + selected ? "bg-white shadow" : "text-blue-100 hover:bg-white/[0.12] hover:text-ielts-level", + ) + }> + Exam + + + + + + + + {resultingExam && ( + loadExam(resultingExam.id)} + className={clsx( + "bg-white border border-ielts-level text-ielts-level w-full max-w-[200px] rounded-xl h-[70px] self-end", + "hover:bg-ielts-level hover:text-white disabled:bg-ielts-level/40 disabled:cursor-not-allowed", + "transition ease-in-out duration-300", + )}> + Perform Exam + + )} + + {isLoading ? ( + + + + ) : ( + "Submit" + )} + + + > + ); +}; + +export default LevelGeneration; diff --git a/src/pages/generation.tsx b/src/pages/generation.tsx index 82894b4f..62828726 100644 --- a/src/pages/generation.tsx +++ b/src/pages/generation.tsx @@ -19,6 +19,7 @@ import axios from "axios"; import ReadingGeneration from "./(generation)/ReadingGeneration"; import ListeningGeneration from "./(generation)/ListeningGeneration"; import WritingGeneration from "./(generation)/WritingGeneration"; +import LevelGeneration from "./(generation)/LevelGeneration"; export const getServerSideProps = withIronSessionSsr(({req, res}) => { const user = req.session.user; @@ -114,6 +115,7 @@ export default function Generation() { {module === "reading" && } {module === "listening" && } {module === "writing" && } + {module === "level" && } )} >