();
useEffect(() => {
const task1Timer = task1 ? 20 : 0;
const task2Timer = task2 ? 40 : 0;
setMinTimer(task1Timer > 0 || task2Timer > 0 ? task1Timer + task2Timer : 20);
}, [task1, task2]);
const router = useRouter();
const setExams = useExamStore((state) => state.setExams);
const setSelectedModules = useExamStore((state) => state.setSelectedModules);
const loadExam = async (examId: string) => {
const exam = await getExamById("writing", 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(["writing"]);
router.push("/exercises");
};
const submitExam = () => {
if (!task1 && !task2) {
toast.error("Please generate a task before submitting");
return;
}
const exercise1 = task1
? ({
id: v4(),
type: "writing",
prefix: `You should spend about 20 minutes on this task.`,
prompt: task1,
userSolutions: [],
suffix: "You should write at least 150 words.",
wordCounter: {
limit: 150,
type: "min",
},
} as WritingExercise)
: undefined;
const exercise2 = task2
? ({
id: v4(),
type: "writing",
prefix: `You should spend about 40 minutes on this task.`,
prompt: task2,
userSolutions: [],
suffix: "You should write at least 250 words.",
wordCounter: {
limit: 250,
type: "min",
},
} as WritingExercise)
: undefined;
setIsLoading(true);
const exam: WritingExam = {
isDiagnostic: false,
minTimer,
module: "writing",
exercises: [...(exercise1 ? [exercise1] : []), ...(exercise2 ? [exercise2] : [])],
id: v4(),
variant: exercise1 && exercise2 ? "full" : "partial",
};
axios
.post(`/api/exam/writing`, exam)
.then((result) => {
console.log(`Generated Exam ID: ${result.data.id}`);
playSound("sent");
toast.success("This new exam has been generated successfully! Check the ID in our browser's console.");
setResultingExam(result.data);
setTask1(undefined);
setTask2(undefined);
})
.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]"
/>
clsx(
"w-full rounded-lg py-2.5 text-sm font-medium leading-5 text-ielts-writing/70 flex gap-2 items-center justify-center",
"ring-white ring-opacity-60 ring-offset-2 ring-offset-ielts-writing 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-writing",
)
}>
Task 1 {task1 && }
clsx(
"w-full rounded-lg py-2.5 text-sm font-medium leading-5 text-ielts-writing/70 flex gap-2 items-center justify-center",
"ring-white ring-opacity-60 ring-offset-2 ring-offset-ielts-writing 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-writing",
)
}>
Task 2 {task2 && }
{[
{task: task1, setTask: setTask1},
{task: task2, setTask: setTask2},
].map(({task, setTask}, index) => (
))}
{resultingExam && (
)}
>
);
};
export default WritingGeneration;