Skip to content

Commit e327bb8

Browse files
committed
Fix MyCourses page in case there are no courses for the user or there was a fetching error
1 parent 92bb94c commit e327bb8

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

ui/src/pages/MyCourses.tsx

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,53 @@ function MyCourses({user}: { user: User | null }) {
1010
return <Navigate to="/login" replace/>;
1111
}
1212
const [courses, setCourses] = useState<Course[]>([]);
13+
const [error, setError] = useState<string | null>(null);
1314
const {backendUrl} = useConfig();
1415

1516
useEffect(() => {
16-
fetch(`${backendUrl}/students/${user.userId}/courses`, {
17-
credentials: "include",
18-
}).then(res => res.json())
19-
.then(data => setCourses(data.courses))
20-
.catch(err => console.error("Error fetching My Courses:", err))
21-
}, [])
17+
if (!user) {
18+
setError("No user authenticated to find courses for");
19+
return;
20+
}
21+
const fetchCourses = async () => {
22+
try {
23+
const res = await fetch(`${backendUrl}/students/${user.userId}/courses`, {
24+
credentials: "include",
25+
});
26+
if (!res.ok) {
27+
setError("Error while loading courses");
28+
setCourses([]);
29+
} else {
30+
const data = await res.json();
31+
setCourses(Array.isArray(data.courses) ? data.courses : []);
32+
setError(null);
33+
}
34+
} catch (err) {
35+
console.error("Error fetching My Courses:", err);
36+
setError("Error while loading courses");
37+
setCourses([]);
38+
}
39+
};
40+
fetchCourses();
41+
}, [backendUrl, user]);
42+
43+
if (error) {
44+
return (
45+
<div className="max-w-5xl mx-auto mt-10 pb-5">
46+
<h1 className="text-4xl font-extrabold mb-8 text-gray-800">My Courses</h1>
47+
<div className="text-lg text-gray-600">{error}</div>
48+
</div>
49+
);
50+
}
51+
52+
if (courses.length === 0) {
53+
return (
54+
<div className="max-w-5xl mx-auto mt-10 pb-5">
55+
<h1 className="text-4xl font-extrabold mb-8 text-gray-800">My Courses</h1>
56+
<div className="text-lg text-gray-600"> You are not enrolled in any courses yet.</div>
57+
</div>
58+
);
59+
}
2260

2361
return (
2462
<div className="max-w-5xl mx-auto mt-10 pb-5">

0 commit comments

Comments
 (0)