From a4d57b852b23eb27aadf80487c0dfa0126a0666c Mon Sep 17 00:00:00 2001 From: Mohammed <88824957+majhcc@users.noreply.github.com> Date: Tue, 16 Jan 2024 22:22:14 +0400 Subject: [PATCH] Refactor CoursePage component to include completed courses --- routes/group/[slug].tsx | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/routes/group/[slug].tsx b/routes/group/[slug].tsx index 170b95b..c12d2c5 100644 --- a/routes/group/[slug].tsx +++ b/routes/group/[slug].tsx @@ -9,23 +9,38 @@ import CourseCard from "../../components/CourseCard.tsx"; import Footer from "../../components/Footer.tsx"; import IconChevronDown from "https://deno.land/x/tabler_icons_tsx@0.0.5/tsx/chevron-down.tsx"; - -export const handler: Handlers = { - GET(_req, ctx) { +import { getCookies } from "$std/http/mod.ts"; +import { getStudent } from "../../utils/KV.ts"; +interface Props { + courses: CourseGroup; + completed: string[]; +} +export const handler: Handlers = { + async GET(_req, ctx) { let foundCourseGroup: CourseGroup | Course | undefined = undefined; const toFind = decodeURIComponent(ctx.params.slug); foundCourseGroup = cache.courses.find((c) => { return "courses" in c && c.courses.length > 0 && c.lableSlug === toFind; }); - + const session = getCookies(_req.headers)["sessionId"]; + if (!session) { + return ctx.render({ + completed: [], + courses: foundCourseGroup as CourseGroup, + }); + } + let completed = (await getStudent(session)).completedCourses; if (!foundCourseGroup) return ctx.renderNotFound(); - return ctx.render(foundCourseGroup as CourseGroup); + return ctx.render({ + completed, + courses: foundCourseGroup as CourseGroup, + }); }, }; -export default function CoursePage(props: PageProps) { - const foundCourseGroup = props.data; +export default function CoursePage(props: PageProps) { + const {courses:foundCourseGroup, completed} = props.data; return ( <> @@ -43,7 +58,7 @@ export default function CoursePage(props: PageProps) {
{foundCourseGroup.courses.map((innerCourse) => ( - + ))}