import Input from "@/components/Low/Input"; import Select from "@/components/Low/Select"; import {Difficulty, ReadingExam, ReadingPart} from "@/interfaces/exam"; import useExamStore from "@/stores/examStore"; import {getExamById} from "@/utils/exams"; import {playSound} from "@/utils/sound"; import {convertCamelCaseToReadable} from "@/utils/string"; import {Tab} from "@headlessui/react"; import axios from "axios"; import clsx from "clsx"; import {capitalize, sample} from "lodash"; import {useRouter} from "next/router"; import {useEffect, useState} from "react"; import {BsArrowRepeat, BsCheck} from "react-icons/bs"; import {toast} from "react-toastify"; import {v4} from "uuid"; const DIFFICULTIES: Difficulty[] = ["easy", "medium", "hard"]; const PartTab = ({ part, types, difficulty, index, setPart, }: { part?: ReadingPart; types: string[]; index: number; difficulty: Difficulty; setPart: (part?: ReadingPart) => void; }) => { const [topic, setTopic] = useState(""); const [isLoading, setIsLoading] = useState(false); const generate = () => { const url = new URLSearchParams(); url.append("difficulty", difficulty); if (topic) url.append("topic", topic); if (types) types.forEach((t) => url.append("exercises", t)); setPart(undefined); setIsLoading(true); axios .get(`/api/exam/reading/generate/reading_passage_${index}${topic || types ? `?${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."); setPart(result.data); }) .catch((error) => { console.log(error); toast.error("Something went wrong!"); }) .finally(() => setIsLoading(false)); }; return (
{isLoading && (
Generating...
)} {part && (
{part.exercises.map((x) => ( {x.type && convertCamelCaseToReadable(x.type)} ))}

{part.text.title}

{part.text.content}
)}
); }; const ReadingGeneration = () => { const [part1, setPart1] = useState(); const [part2, setPart2] = useState(); const [part3, setPart3] = useState(); const [minTimer, setMinTimer] = useState(60); const [types, setTypes] = useState([]); const [isLoading, setIsLoading] = useState(false); const [resultingExam, setResultingExam] = useState(); const [difficulty, setDifficulty] = useState(sample(DIFFICULTIES)!); useEffect(() => { const parts = [part1, part2, part3].filter((x) => !!x); setMinTimer(parts.length === 0 ? 60 : parts.length * 20); }, [part1, part2, part3]); const router = useRouter(); const setExams = useExamStore((state) => state.setExams); const setSelectedModules = useExamStore((state) => state.setSelectedModules); const availableTypes = [ {type: "fillBlanks", label: "Fill the Blanks"}, {type: "multipleChoice", label: "Multiple Choice"}, {type: "trueFalse", label: "True or False"}, {type: "writeBlanks", label: "Write the Blanks"}, {type: "matchSentences", label: "Match Sentences"}, ]; const toggleType = (type: string) => setTypes((prev) => (prev.includes(type) ? [...prev.filter((x) => x !== type)] : [...prev, type])); const loadExam = async (examId: string) => { const exam = await getExamById("reading", 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(["reading"]); router.push("/exercises"); }; const submitExam = () => { const parts = [part1, part2, part3].filter((x) => !!x) as ReadingPart[]; if (parts.length === 0) { toast.error("Please generate at least one passage before submitting"); return; } setIsLoading(true); const exam: ReadingExam = { parts, isDiagnostic: false, minTimer, module: "reading", id: v4(), type: "academic", variant: parts.length === 3 ? "full" : "partial", difficulty, }; axios .post(`/api/exam/reading`, 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); setPart1(undefined); setPart2(undefined); setPart3(undefined); setDifficulty(sample(DIFFICULTIES)!); setMinTimer(60); setTypes([]); }) .catch((error) => { console.log(error); toast.error("Something went wrong while generating, please try again later."); }) .finally(() => setIsLoading(false)); }; return ( <>
setMinTimer(parseInt(e) < 15 ? 15 : parseInt(e))} value={minTimer} className="max-w-[300px]" />