import Select from "@/components/Low/Select"; import {Difficulty, LevelExam, MultipleChoiceExercise, MultipleChoiceQuestion} from "@/interfaces/exam"; import useExamStore from "@/stores/examStore"; import {getExamById} from "@/utils/exams"; import {playSound} from "@/utils/sound"; import {Tab} from "@headlessui/react"; import axios from "axios"; import clsx from "clsx"; import {capitalize, sample} from "lodash"; import {useRouter} from "next/router"; import {useState} from "react"; import {BsArrowRepeat, BsCheck, BsPencilSquare, BsX} from "react-icons/bs"; import {toast} from "react-toastify"; import {v4} from "uuid"; const DIFFICULTIES: Difficulty[] = ["easy", "medium", "hard"]; const QuestionDisplay = ({question, onUpdate}: {question: MultipleChoiceQuestion; onUpdate: (question: MultipleChoiceQuestion) => void}) => { const [isEditing, setIsEditing] = useState(false); const [options, setOptions] = useState(question.options); return (
{question.id}. {question.prompt}{" "}
{question.options.map((option, index) => ( ({option.id}) {" "} {isEditing ? ( setOptions((prev) => prev.map((x, idx) => (idx === index ? {...x, text: e.target.value} : x)))} /> ) : ( {option.text} )} ))}
{!isEditing && ( )} {isEditing && ( <> )}
); }; const TaskTab = ({exam, difficulty, setExam}: {exam?: LevelExam; difficulty: Difficulty; setExam: (exam: LevelExam) => void}) => { const [isLoading, setIsLoading] = useState(false); const generate = () => { const url = new URLSearchParams(); url.append("difficulty", difficulty); setIsLoading(true); axios .get(`/api/exam/level/generate/level?${url.toString()}`) .then((result) => { playSound(typeof result.data === "string" ? "error" : "check"); if (typeof result.data === "string") return toast.error("Something went wrong, please try to generate again."); setExam(result.data); }) .catch((error) => { console.log(error); toast.error("Something went wrong!"); }) .finally(() => setIsLoading(false)); }; const onUpdate = (question: MultipleChoiceQuestion) => { if (!exam) return; const updatedExam = { ...exam, exercises: exam.exercises.map((x) => ({ ...x, questions: (x as MultipleChoiceExercise).questions.map((q) => (q.id === question.id ? question : q)), })), }; console.log(updatedExam); setExam(updatedExam as any); }; return (
{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) => ( ))}
); })}
)}
); }; const LevelGeneration = () => { const [generatedExam, setGeneratedExam] = useState(); const [isLoading, setIsLoading] = useState(false); const [resultingExam, setResultingExam] = useState(); const [difficulty, setDifficulty] = useState(sample(DIFFICULTIES)!); 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) => { playSound("sent"); 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 ( <>