Finalized the Speaking exam generation

This commit is contained in:
Tiago Ribeiro
2024-01-24 00:37:54 +00:00
parent bd0fab4c8f
commit 5f475fb7a7
2 changed files with 60 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
import Input from "@/components/Low/Input";
import {Exercise, SpeakingExam} from "@/interfaces/exam";
import {Exercise, InteractiveSpeakingExercise, SpeakingExam, SpeakingExercise} from "@/interfaces/exam";
import useExamStore from "@/stores/examStore";
import {getExamById} from "@/utils/exams";
import {playSound} from "@/utils/sound";
@@ -11,6 +11,7 @@ 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);
@@ -32,6 +33,25 @@ const PartTab = ({part, index, setPart}: {part?: SpeakingPart; index: number; se
.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 (
<Tab.Panel className="w-full bg-ielts-speaking/20 min-h-[600px] h-full rounded-xl p-6 flex flex-col gap-4">
<div className="flex gap-4 items-end">
@@ -53,6 +73,24 @@ const PartTab = ({part, index, setPart}: {part?: SpeakingPart; index: number; se
"Generate"
)}
</button>
<button
onClick={generateVideo}
disabled={isLoading || !part}
data-tip="The passage is currently being generated"
className={clsx(
"bg-ielts-speaking/70 border border-ielts-speaking text-white w-full rounded-xl h-[70px]",
"hover:bg-ielts-speaking disabled:bg-ielts-speaking/40 disabled:cursor-not-allowed",
"transition ease-in-out duration-300",
isLoading && "tooltip",
)}>
{isLoading ? (
<div className="flex items-center justify-center">
<BsArrowRepeat className="text-white animate-spin" size={25} />
</div>
) : (
"Generate Video"
)}
</button>
</div>
{isLoading && (
<div className="w-fit h-fit mt-12 self-center animate-pulse flex flex-col gap-8 items-center">
@@ -83,6 +121,7 @@ const PartTab = ({part, index, setPart}: {part?: SpeakingPart; index: number; se
))}
</div>
)}
{part.result && <span className="font-bold mt-4">Video Generated: </span>}
</div>
)}
</Tab.Panel>
@@ -94,6 +133,7 @@ interface SpeakingPart {
question?: string;
questions?: string[];
topic: string;
result?: SpeakingExercise | InteractiveSpeakingExercise;
}
const SpeakingGeneration = () => {
@@ -115,19 +155,21 @@ const SpeakingGeneration = () => {
const setSelectedModules = useExamStore((state) => state.setSelectedModules);
const submitExam = () => {
if (!part1 && !part2 && !part3) return toast.error("Please generate at least one task!");
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/generate/speaking`, {
exercises: [
{...part1, type: "1"},
{...part2, type: "2"},
{...part3, type: "3"},
].filter((x) => !!x),
minTimer,
})
.post(`/api/exam/speaking`, exam)
.then((result) => {
playSound("sent");
console.log(`Generated Exam ID: ${result.data.id}`);
@@ -137,10 +179,11 @@ const SpeakingGeneration = () => {
setPart1(undefined);
setPart2(undefined);
setPart3(undefined);
setMinTimer(14);
})
.catch((error) => {
console.log(error);
toast.error("Something went wrong!");
toast.error("Something went wrong while generating, please try again later.");
})
.finally(() => setIsLoading(false));
};
@@ -185,7 +228,7 @@ const SpeakingGeneration = () => {
selected ? "bg-white shadow" : "text-blue-100 hover:bg-white/[0.12] hover:text-ielts-speaking",
)
}>
Task 1 {part1 && <BsCheck />}
Exercise 1 {part1 && part1.result && <BsCheck />}
</Tab>
<Tab
className={({selected}) =>
@@ -196,7 +239,7 @@ const SpeakingGeneration = () => {
selected ? "bg-white shadow" : "text-blue-100 hover:bg-white/[0.12] hover:text-ielts-speaking",
)
}>
Task 2 {part2 && <BsCheck />}
Exercise 2 {part2 && part2.result && <BsCheck />}
</Tab>
<Tab
className={({selected}) =>
@@ -207,7 +250,7 @@ const SpeakingGeneration = () => {
selected ? "bg-white shadow" : "text-blue-100 hover:bg-white/[0.12] hover:text-ielts-speaking",
)
}>
Task 3 {part3 && <BsCheck />}
Interactive {part3 && part3.result && <BsCheck />}
</Tab>
</Tab.List>
<Tab.Panels>
@@ -234,7 +277,7 @@ const SpeakingGeneration = () => {
</button>
)}
<button
disabled={(!part1 && !part2 && !part3) || isLoading}
disabled={(!part1?.result && !part2?.result && !part3?.result) || isLoading}
data-tip="Please generate all three passages"
onClick={submitExam}
className={clsx(

View File

@@ -39,8 +39,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
if (!req.session.user) return res.status(401).json({ok: false});
if (req.session.user.type !== "developer") return res.status(403).json({ok: false});
const {endpoint, topic, exercises} = req.query as {module: Module; endpoint: string; topic?: string; exercises?: string[]};
const url = `${process.env.BACKEND_URL}/${endpoint}`;
const {endpoint, topic, exercises} = req.query as {module: Module; endpoint: string[]; topic?: string; exercises?: string[]};
const url = `${process.env.BACKEND_URL}/${endpoint.join("/")}`;
const result = await axios.post(
`${url}${topic && exercises ? `?topic=${topic.toLowerCase()}&exercises=${exercises.join("&exercises=")}` : ""}`,