diff --git a/islands/Editor.tsx b/islands/Editor.tsx index 035fb56..2d573e8 100644 --- a/islands/Editor.tsx +++ b/islands/Editor.tsx @@ -36,7 +36,7 @@ export default function Editor(props: EditorProps) { window.editor.setValue(""); setOutput(""); } - function handleCodeTest() { + async function handleCodeTest() { setTesting(true); const code: string = window.editor.getValue() || ""; if (props.testingCode === "") { @@ -61,7 +61,7 @@ export default function Editor(props: EditorProps) { msg: "تم تجاوز الاختبارات بنجاح", type: "success", }); - fetch("/api/test/finsh", { + const res = await fetch("/api/test/finsh", { method: "POST", headers: { "Content-Type": "application/json", @@ -70,6 +70,14 @@ export default function Editor(props: EditorProps) { courseslug: props.slug, }), }); + if (!res.ok) { + showToast({ + msg: "لم يتم تسجيل الاختبارات", + type: "error", + }); + setTesting(false); + return; + } setTesting(false); return; } else { diff --git a/routes/api/test/finsh.ts b/routes/api/test/finsh.ts index 34bae45..7b2ebd7 100644 --- a/routes/api/test/finsh.ts +++ b/routes/api/test/finsh.ts @@ -10,9 +10,12 @@ export const handler: Handlers = { const FinshTest = (await req.json() as FinshTest) const courseslug = FinshTest.courseslug const sessionId = getCookies(req.headers)?.sessionId; - addCompletedCourse(sessionId, courseslug) + const res = await addCompletedCourse(sessionId, courseslug) + if (!res.ok) { + return new Response("error", { status: 400 }) + } } catch (error) { - return new Response(error.message, { status: 500 }); + return new Response("error", { status: 400 }) } return new Response("ok"); }, diff --git a/utils/KV.ts b/utils/KV.ts index 740b48b..4151df4 100644 --- a/utils/KV.ts +++ b/utils/KV.ts @@ -32,7 +32,7 @@ export const updateStudent = async (sessionId: string, student: student): Promis export const addCompletedCourse = async (sessionId: string, course: string) : Promise => { const student = await getStudent(sessionId) if (student.completedCourses.includes(course)) { - throw new Error("Course already completed") + return { ok: true } } const completedCourses = student.completedCourses completedCourses.push(course) @@ -41,6 +41,6 @@ export const addCompletedCourse = async (sessionId: string, course: string) : Pr const res = await updateStudent(sessionId, student) return res } catch { - throw new Error("Failed to add completed course") + return { ok: false } } } \ No newline at end of file