import Input from "@/components/Low/Input"; import {Exercise, InteractiveSpeakingExercise, SpeakingExam, SpeakingExercise} 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 {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 PartTab = ({part, index, setPart}: {part?: SpeakingPart; index: number; setPart: (part?: SpeakingPart) => void}) => { const [isLoading, setIsLoading] = useState(false); const generate = () => { setPart(undefined); setIsLoading(true); axios .get(`/api/exam/speaking/generate/speaking_task_${index}`) .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)); }; const generateVideo = () => { if (!part) return toast.error("Please generate the first part before generating the video!"); toast.info("This will take quite a while, please do not leave this page or close the tab/window."); setIsLoading(true); axios .post(`/api/exam/speaking/generate/speaking/generate_${index === 3 ? "interactive" : "speaking"}_video`, part) .then((result) => { playSound(typeof result.data === "string" ? "error" : "check"); if (typeof result.data === "string") return toast.error("Something went wrong, please try to generate the video again."); setPart({...part, result: result.data}); }) .catch((e) => { toast.error("Something went wrong!"); console.log(e); }) .finally(() => setIsLoading(false)); }; return (
{isLoading && (
Generating...
)} {part && (

{part.topic}

{part.question && {part.question}} {part.questions && (
{part.questions.map((question, index) => ( - {question} ))}
)} {part.prompts && (
You should talk about the following things: {part.prompts.map((prompt, index) => ( - {prompt} ))}
)} {part.result && Video Generated: ✅}
)}
); }; interface SpeakingPart { prompts?: string[]; question?: string; questions?: string[]; topic: string; result?: SpeakingExercise | InteractiveSpeakingExercise; } const SpeakingGeneration = () => { const [part1, setPart1] = useState(); const [part2, setPart2] = useState(); const [part3, setPart3] = useState(); const [minTimer, setMinTimer] = useState(14); const [isLoading, setIsLoading] = useState(false); const [resultingExam, setResultingExam] = useState(); useEffect(() => { const parts = [part1, part2, part3].filter((x) => !!x); setMinTimer(parts.length === 0 ? 5 : parts.length * 5); }, [part1, part2, part3]); const router = useRouter(); const setExams = useExamStore((state) => state.setExams); const setSelectedModules = useExamStore((state) => state.setSelectedModules); const submitExam = () => { if (!part1?.result && !part2?.result && !part3?.result) return toast.error("Please generate at least one task!"); setIsLoading(true); const exam: SpeakingExam = { id: v4(), isDiagnostic: false, exercises: [part1?.result, part2?.result, part3?.result].filter((x) => !!x) as (SpeakingExercise | InteractiveSpeakingExercise)[], minTimer, variant: minTimer >= 14 ? "full" : "partial", module: "speaking", }; axios .post(`/api/exam/speaking`, 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); setMinTimer(14); }) .catch((error) => { console.log(error); toast.error("Something went wrong while generating, please try again later."); }) .finally(() => setIsLoading(false)); }; const loadExam = async (examId: string) => { const exam = await getExamById("speaking", 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(["speaking"]); router.push("/exercises"); }; return ( <>
setMinTimer(parseInt(e) < 5 ? 5 : parseInt(e))} value={minTimer} className="max-w-[300px]" />
clsx( "w-full rounded-lg py-2.5 text-sm font-medium leading-5 text-ielts-speaking/70 flex gap-2 items-center justify-center", "ring-white ring-opacity-60 ring-offset-2 ring-offset-ielts-speaking 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-speaking", ) }> Exercise 1 {part1 && part1.result && } clsx( "w-full rounded-lg py-2.5 text-sm font-medium leading-5 text-ielts-speaking/70 flex gap-2 items-center justify-center", "ring-white ring-opacity-60 ring-offset-2 ring-offset-ielts-speaking 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-speaking", ) }> Exercise 2 {part2 && part2.result && } clsx( "w-full rounded-lg py-2.5 text-sm font-medium leading-5 text-ielts-speaking/70 flex gap-2 items-center justify-center", "ring-white ring-opacity-60 ring-offset-2 ring-offset-ielts-speaking 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-speaking", ) }> Interactive {part3 && part3.result && } {[ {part: part1, setPart: setPart1}, {part: part2, setPart: setPart2}, {part: part3, setPart: setPart3}, ].map(({part, setPart}, index) => ( ))}
{resultingExam && ( )}
); }; export default SpeakingGeneration;