It is now possible to generate and save both Reading and Writing exams
This commit is contained in:
@@ -1,12 +1,16 @@
|
|||||||
import Input from "@/components/Low/Input";
|
import Input from "@/components/Low/Input";
|
||||||
import {ReadingPart} from "@/interfaces/exam";
|
import {ReadingExam, ReadingPart} from "@/interfaces/exam";
|
||||||
|
import useExamStore from "@/stores/examStore";
|
||||||
|
import {getExamById} from "@/utils/exams";
|
||||||
import {convertCamelCaseToReadable} from "@/utils/string";
|
import {convertCamelCaseToReadable} from "@/utils/string";
|
||||||
import {Tab} from "@headlessui/react";
|
import {Tab} from "@headlessui/react";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
|
import {useRouter} from "next/router";
|
||||||
import {useState} from "react";
|
import {useState} from "react";
|
||||||
import {BsArrowRepeat} from "react-icons/bs";
|
import {BsArrowRepeat} from "react-icons/bs";
|
||||||
import {toast} from "react-toastify";
|
import {toast} from "react-toastify";
|
||||||
|
import {v4} from "uuid";
|
||||||
|
|
||||||
const PartTab = ({part, types, index, setPart}: {part?: ReadingPart; types: string[]; index: number; setPart: (part?: ReadingPart) => void}) => {
|
const PartTab = ({part, types, index, setPart}: {part?: ReadingPart; types: string[]; index: number; setPart: (part?: ReadingPart) => void}) => {
|
||||||
const [topic, setTopic] = useState("");
|
const [topic, setTopic] = useState("");
|
||||||
@@ -80,6 +84,13 @@ const ReadingGeneration = () => {
|
|||||||
const [part2, setPart2] = useState<ReadingPart>();
|
const [part2, setPart2] = useState<ReadingPart>();
|
||||||
const [part3, setPart3] = useState<ReadingPart>();
|
const [part3, setPart3] = useState<ReadingPart>();
|
||||||
const [types, setTypes] = useState<string[]>([]);
|
const [types, setTypes] = useState<string[]>([]);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [resultingExam, setResultingExam] = useState<ReadingExam>();
|
||||||
|
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const setExams = useExamStore((state) => state.setExams);
|
||||||
|
const setSelectedModules = useExamStore((state) => state.setSelectedModules);
|
||||||
|
|
||||||
const availableTypes = [
|
const availableTypes = [
|
||||||
{type: "fillBlanks", label: "Fill the Blanks"},
|
{type: "fillBlanks", label: "Fill the Blanks"},
|
||||||
@@ -90,6 +101,57 @@ const ReadingGeneration = () => {
|
|||||||
|
|
||||||
const toggleType = (type: string) => setTypes((prev) => (prev.includes(type) ? [...prev.filter((x) => x !== type)] : [...prev, type]));
|
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 = () => {
|
||||||
|
if (!part1 || !part2 || !part3) {
|
||||||
|
toast.error("Please generate all three passages before submitting");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
const exam: ReadingExam = {
|
||||||
|
parts: [part1, part2, part3],
|
||||||
|
isDiagnostic: false,
|
||||||
|
minTimer: 60,
|
||||||
|
module: "reading",
|
||||||
|
id: v4(),
|
||||||
|
type: "academic",
|
||||||
|
};
|
||||||
|
|
||||||
|
axios
|
||||||
|
.post(`/api/exam/reading`, exam)
|
||||||
|
.then((result) => {
|
||||||
|
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);
|
||||||
|
setTypes([]);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.log(error);
|
||||||
|
toast.error("Something went wrong while generating, please try again later.");
|
||||||
|
})
|
||||||
|
.finally(() => setIsLoading(false));
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="flex flex-col gap-3">
|
<div className="flex flex-col gap-3">
|
||||||
@@ -156,17 +218,38 @@ const ReadingGeneration = () => {
|
|||||||
))}
|
))}
|
||||||
</Tab.Panels>
|
</Tab.Panels>
|
||||||
</Tab.Group>
|
</Tab.Group>
|
||||||
|
<div className="w-full flex justify-end gap-4">
|
||||||
|
{resultingExam && (
|
||||||
<button
|
<button
|
||||||
disabled={!part1 || !part2 || !part3}
|
disabled={isLoading}
|
||||||
data-tip="Please generate all three passages"
|
onClick={() => loadExam(resultingExam.id)}
|
||||||
className={clsx(
|
className={clsx(
|
||||||
"bg-ielts-reading/70 border border-ielts-reading text-white w-full max-w-[200px] px-6 py-6 rounded-xl h-[70px] self-end",
|
"bg-white border border-ielts-reading text-ielts-reading w-full max-w-[200px] rounded-xl h-[70px] self-end",
|
||||||
|
"hover:bg-ielts-reading hover:text-white disabled:bg-ielts-reading/40 disabled:cursor-not-allowed",
|
||||||
|
"transition ease-in-out duration-300",
|
||||||
|
)}>
|
||||||
|
Perform Exam
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
disabled={!part1 || !part2 || !part3 || isLoading}
|
||||||
|
data-tip="Please generate all three passages"
|
||||||
|
onClick={submitExam}
|
||||||
|
className={clsx(
|
||||||
|
"bg-ielts-reading/70 border border-ielts-reading text-white w-full max-w-[200px] rounded-xl h-[70px] self-end",
|
||||||
"hover:bg-ielts-reading disabled:bg-ielts-reading/40 disabled:cursor-not-allowed",
|
"hover:bg-ielts-reading disabled:bg-ielts-reading/40 disabled:cursor-not-allowed",
|
||||||
"transition ease-in-out duration-300",
|
"transition ease-in-out duration-300",
|
||||||
(!part1 || !part2 || !part3) && "tooltip",
|
(!part1 || !part2 || !part3) && "tooltip",
|
||||||
)}>
|
)}>
|
||||||
Submit
|
{isLoading ? (
|
||||||
|
<div className="flex items-center justify-center">
|
||||||
|
<BsArrowRepeat className="text-white animate-spin" size={25} />
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
"Submit"
|
||||||
|
)}
|
||||||
</button>
|
</button>
|
||||||
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
import Input from "@/components/Low/Input";
|
import Input from "@/components/Low/Input";
|
||||||
|
import {WritingExam} from "@/interfaces/exam";
|
||||||
|
import useExamStore from "@/stores/examStore";
|
||||||
|
import {getExamById} from "@/utils/exams";
|
||||||
import {Tab} from "@headlessui/react";
|
import {Tab} from "@headlessui/react";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
|
import {useRouter} from "next/router";
|
||||||
import {useState} from "react";
|
import {useState} from "react";
|
||||||
import {BsArrowRepeat} from "react-icons/bs";
|
import {BsArrowRepeat} from "react-icons/bs";
|
||||||
import {toast} from "react-toastify";
|
import {toast} from "react-toastify";
|
||||||
|
import {v4} from "uuid";
|
||||||
|
|
||||||
const WritingGeneration = () => {
|
const TaskTab = ({task, index, setTask}: {task?: string; index: number; setTask: (task: string) => void}) => {
|
||||||
const [task1, setTask1] = useState<string>();
|
|
||||||
const [task2, setTask2] = useState<string>();
|
|
||||||
|
|
||||||
const TaskTab = ({task, index, setTask}: {task?: string; index: number; setTask: (task: string) => void}) => {
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
const generate = () => {
|
const generate = () => {
|
||||||
@@ -58,6 +59,90 @@ const WritingGeneration = () => {
|
|||||||
)}
|
)}
|
||||||
</Tab.Panel>
|
</Tab.Panel>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const WritingGeneration = () => {
|
||||||
|
const [task1, setTask1] = useState<string>();
|
||||||
|
const [task2, setTask2] = useState<string>();
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [resultingExam, setResultingExam] = useState<WritingExam>();
|
||||||
|
|
||||||
|
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 all tasks before submitting");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
const exam: WritingExam = {
|
||||||
|
isDiagnostic: false,
|
||||||
|
minTimer: 60,
|
||||||
|
module: "writing",
|
||||||
|
exercises: [
|
||||||
|
{
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
id: v4(),
|
||||||
|
};
|
||||||
|
|
||||||
|
axios
|
||||||
|
.post(`/api/exam/writing`, exam)
|
||||||
|
.then((result) => {
|
||||||
|
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);
|
||||||
|
|
||||||
|
setTask1(undefined);
|
||||||
|
setTask2(undefined);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.log(error);
|
||||||
|
toast.error("Something went wrong while generating, please try again later.");
|
||||||
|
})
|
||||||
|
.finally(() => setIsLoading(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -96,17 +181,38 @@ const WritingGeneration = () => {
|
|||||||
))}
|
))}
|
||||||
</Tab.Panels>
|
</Tab.Panels>
|
||||||
</Tab.Group>
|
</Tab.Group>
|
||||||
|
<div className="w-full flex justify-end gap-4">
|
||||||
|
{resultingExam && (
|
||||||
<button
|
<button
|
||||||
disabled={!task1 || !task2}
|
disabled={isLoading}
|
||||||
data-tip="Please generate all three passages"
|
onClick={() => loadExam(resultingExam.id)}
|
||||||
className={clsx(
|
className={clsx(
|
||||||
"bg-ielts-writing/70 border border-ielts-writing text-white w-full max-w-[200px] px-6 py-6 rounded-xl h-[70px] self-end",
|
"bg-white border border-ielts-writing text-ielts-writing w-full max-w-[200px] rounded-xl h-[70px] self-end",
|
||||||
|
"hover:bg-ielts-writing hover:text-white disabled:bg-ielts-writing/40 disabled:cursor-not-allowed",
|
||||||
|
"transition ease-in-out duration-300",
|
||||||
|
)}>
|
||||||
|
Perform Exam
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
disabled={!task1 || !task2 || isLoading}
|
||||||
|
data-tip="Please generate all three passages"
|
||||||
|
onClick={submitExam}
|
||||||
|
className={clsx(
|
||||||
|
"bg-ielts-writing/70 border border-ielts-writing text-white w-full max-w-[200px] rounded-xl h-[70px] self-end",
|
||||||
"hover:bg-ielts-writing disabled:bg-ielts-writing/40 disabled:cursor-not-allowed",
|
"hover:bg-ielts-writing disabled:bg-ielts-writing/40 disabled:cursor-not-allowed",
|
||||||
"transition ease-in-out duration-300",
|
"transition ease-in-out duration-300",
|
||||||
(!task1 || !task2) && "tooltip",
|
(!task1 || !task2) && "tooltip",
|
||||||
)}>
|
)}>
|
||||||
Submit
|
{isLoading ? (
|
||||||
|
<div className="flex items-center justify-center">
|
||||||
|
<BsArrowRepeat className="text-white animate-spin" size={25} />
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
"Submit"
|
||||||
|
)}
|
||||||
</button>
|
</button>
|
||||||
|
</div>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,18 +1,26 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
import type {NextApiRequest, NextApiResponse} from "next";
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
import {app} from "@/firebase";
|
import {app} from "@/firebase";
|
||||||
import {getFirestore, collection, getDocs, query, where} from "firebase/firestore";
|
import {getFirestore, collection, getDocs, query, where, setDoc, doc} from "firebase/firestore";
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {shuffle} from "lodash";
|
import {shuffle} from "lodash";
|
||||||
import {Exam} from "@/interfaces/exam";
|
import {Exam} from "@/interfaces/exam";
|
||||||
import {Stat} from "@/interfaces/user";
|
import {Stat} from "@/interfaces/user";
|
||||||
|
import {v4} from "uuid";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
if (req.method === "GET") return await GET(req, res);
|
||||||
|
if (req.method === "POST") return await POST(req, res);
|
||||||
|
|
||||||
|
res.status(404).json({ok: false});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function GET(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
if (!req.session.user) {
|
||||||
res.status(401).json({ok: false});
|
res.status(401).json({ok: false});
|
||||||
return;
|
return;
|
||||||
@@ -45,3 +53,21 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
res.status(200).json(exams);
|
res.status(200).json(exams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
if (!req.session.user) {
|
||||||
|
res.status(401).json({ok: false});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.session.user.type !== "developer") {
|
||||||
|
res.status(403).json({ok: false});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const {module} = req.query as {module: string};
|
||||||
|
|
||||||
|
const exam = {...req.body, module: module};
|
||||||
|
await setDoc(doc(db, module, req.body.id), exam);
|
||||||
|
|
||||||
|
res.status(200).json(exam);
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ const db = getFirestore(app);
|
|||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
if (req.method === "GET") return await GET(req, res);
|
||||||
|
|
||||||
|
res.status(404).json({ok: false});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function GET(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
if (!req.session.user) {
|
||||||
res.status(401).json({ok: false});
|
res.status(401).json({ok: false});
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user