From 31fc25827639fa482ec44c58110d5da9ee0a7706 Mon Sep 17 00:00:00 2001 From: lavanyagarg112 Date: Mon, 23 Jun 2025 13:37:29 +0200 Subject: [PATCH 01/23] Clean code --- app/courses/page.tsx | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/courses/page.tsx b/app/courses/page.tsx index 7ab03f0..aee74bb 100644 --- a/app/courses/page.tsx +++ b/app/courses/page.tsx @@ -16,9 +16,7 @@ export default function CoursesPage() { const isAdmin = user?.organisation?.role === "admin"; useEffect(() => { - // Fetch courses from API or database async function fetchCourses() { - // Replace with actual API call const fetchedCourses = await fetch("/api/courses", { credentials: "include", }) @@ -33,6 +31,5 @@ export default function CoursesPage() { fetchCourses(); }, []); - // 3) Render the client component that will show/hide admin buttons return ; } From 6d5f4637677d21839ec597ec6b3b2d7efd28a347 Mon Sep 17 00:00:00 2001 From: lavanyagarg112 Date: Tue, 24 Jun 2025 00:27:35 +0200 Subject: [PATCH 02/23] Get enrolled and unenrolled courses --- app/courses/page.tsx | 60 +++++++++++++++++-- .../organisation/courses/CourseCard.tsx | 17 ++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/app/courses/page.tsx b/app/courses/page.tsx index aee74bb..33d5f4a 100644 --- a/app/courses/page.tsx +++ b/app/courses/page.tsx @@ -8,6 +8,8 @@ import { useState, useEffect } from "react"; export default function CoursesPage() { const { user } = useAuth(); const [courses, setCourses] = useState([]); + const [enrolled, setEnrolled] = useState([]); + const [other, setOther] = useState([]); if (!user || !user.hasCompletedOnboarding) { return null; @@ -15,8 +17,11 @@ export default function CoursesPage() { const isAdmin = user?.organisation?.role === "admin"; + // for user: courses and enrolments + // for admin: all courses + useEffect(() => { - async function fetchCourses() { + async function fetchAdminCourses() { const fetchedCourses = await fetch("/api/courses", { credentials: "include", }) @@ -28,8 +33,55 @@ export default function CoursesPage() { }); setCourses(fetchedCourses); } - fetchCourses(); - }, []); + async function fetchUserCourses() { + const userCourses = await fetch("/api/courses/all-user-courses", { + credentials: "include", + }) + .then((res) => res.json()) + .then((data) => { + setEnrolled(data.enrolled || []); + setOther(data.other || []); + }) + .catch((err) => { + console.error("Failed to fetch user courses:", err); + setEnrolled([]); + setOther([]); + }); + } + if (isAdmin) { + fetchAdminCourses(); + } else { + fetchUserCourses(); + } + }, [isAdmin]); + + if (isAdmin) { + return ; + } + + return ( +
+
+

My Courses

+ {enrolled.length > 0 ? ( + + ) : ( +

+ You’re not enrolled in any courses yet. +

+ )} +
- return ; +
+

+ Available Courses +

+ {other.length > 0 ? ( + + ) : ( +

No other courses available.

+ )} +
+
+ ); } diff --git a/components/organisation/courses/CourseCard.tsx b/components/organisation/courses/CourseCard.tsx index 6ab3e5f..d0fd070 100644 --- a/components/organisation/courses/CourseCard.tsx +++ b/components/organisation/courses/CourseCard.tsx @@ -13,6 +13,15 @@ interface Props { isAdmin: boolean; } +// todo +// get user enrollements from backed +// so from backend: user enrollments + other courses +// endpoints to: +// 1. get all courses (admin) +// 2. get user enrollments (user) +// 3. get other courses (user) +// admin is not enrolled in any course + export default function CourseCard({ course, isAdmin }: Props) { return (
@@ -25,6 +34,14 @@ export default function CourseCard({ course, isAdmin }: Props) { View Modules + {!isAdmin && ( + + )} {isAdmin && ( - {!isAdmin && ( + {!isAdmin && !isEnrolled && ( From dce05e651100316ef799f582c15153c230a50047 Mon Sep 17 00:00:00 2001 From: lavanyagarg112 Date: Tue, 24 Jun 2025 00:35:54 +0200 Subject: [PATCH 05/23] Add unenrolled endpoint api call --- .../organisation/courses/CourseCard.tsx | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/components/organisation/courses/CourseCard.tsx b/components/organisation/courses/CourseCard.tsx index 421c97d..7132e6c 100644 --- a/components/organisation/courses/CourseCard.tsx +++ b/components/organisation/courses/CourseCard.tsx @@ -45,6 +45,27 @@ export default function CourseCard({ course, isAdmin, isEnrolled }: Props) { } }; + const handleUnEnroll = async () => { + try { + const response = await fetch("/api/courses/unenroll-course", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ courseId: course.id }), + credentials: "include", + }); + + if (!response.ok) { + throw new Error("Failed to unenroll from course"); + } + + window.location.reload(); + } catch (error) { + console.error("Error unenrolling from course:", error); + } + }; + return (

{course.name}

@@ -64,6 +85,14 @@ export default function CourseCard({ course, isAdmin, isEnrolled }: Props) { Enroll )} + {!isAdmin && isEnrolled && ( + + )} {isAdmin && (
- + {(isEnrolled || isEditMode) && ( + + + + )} + {!isEnrolled && !isEditMode && ( - + )} {isEditMode && (