Skip to content

Commit

Permalink
Merge pull request #23 from TeaByte/majhcc/issue21
Browse files Browse the repository at this point in the history
Fix test code
  • Loading branch information
m7medVision authored Jan 16, 2024
2 parents 7ef13c0 + f40b52e commit 07b5e1a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
12 changes: 10 additions & 2 deletions islands/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 === "") {
Expand All @@ -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",
Expand All @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions routes/api/test/finsh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ export const handler: Handlers<FinshTest> = {
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");
},
Expand Down
4 changes: 2 additions & 2 deletions utils/KV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const updateStudent = async (sessionId: string, student: student): Promis
export const addCompletedCourse = async (sessionId: string, course: string) : Promise<Result> => {
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)
Expand All @@ -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 }
}
}

0 comments on commit 07b5e1a

Please sign in to comment.