Skip to content

Commit 5eb8f98

Browse files
committed
divide all courses with user course
1 parent dd6cc22 commit 5eb8f98

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

frontend/src/components/pages/CourseDashboard.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ export function CourseDashboard() {
1111
title: "",
1212
description: "",
1313
});
14-
const [searchQuery, setSearchQuery] = useState("");
15-
const [error, setError] = useState(null);
14+
const [searchQuery, setSearchQuery] = useState("");
15+
const [error, setError] = useState(null);
16+
const [courses, setCourses] = useState([]);
17+
const [loading, setLoading] = useState(true);
1618

1719
const handleChange = (e) => {
1820
const { name, value } = e.target;
@@ -87,6 +89,21 @@ export function CourseDashboard() {
8789
.catch((error) => setError(error.message));
8890
}
8991

92+
async function fetchAllCourses() {
93+
try {
94+
const response = await fetch('http://localhost:4000/course/');
95+
if (!response.ok) {
96+
throw new Error('Network response was not ok');
97+
}
98+
const data = await response.json();
99+
setCourses(data.courses || []);
100+
setLoading(false);
101+
} catch (error) {
102+
setLoading(false);
103+
}
104+
}
105+
fetchAllCourses();
106+
90107
fetchCourses();
91108
fetchUser();
92109
}, []);
@@ -117,11 +134,11 @@ export function CourseDashboard() {
117134
);
118135
}
119136

120-
const filteredCourses = courseInfo.filter((course) =>
137+
const filteredCourses = courses.filter((course) =>
121138
course.title.toLowerCase().includes(searchQuery.toLowerCase())
122139
);
123140

124-
const courseList = filteredCourses.map((course) => (
141+
const myCourses = courseInfo.map((course) => (
125142
<a href={`/courses/${course.ID}`} key={course.ID}>
126143
<div className="bg-gray-100 p-4 rounded shadow hover:bg-gray-300">
127144
<h3 className="text-xl font-semibold truncate overflow-hidden">{course.title}</h3>
@@ -130,6 +147,15 @@ export function CourseDashboard() {
130147
</a>
131148
));
132149

150+
const allCourses = filteredCourses.map((course) => (
151+
<div key={course.ID} className="course-box">
152+
<a href={`/courses/${course.ID}`} key={course.ID}>
153+
<h3><b>{course.title}</b></h3>
154+
<p>{course.description}</p>
155+
</a>
156+
</div>
157+
))
158+
133159
return (
134160
<div className="p-6">
135161
<div className="mb-5">
@@ -141,8 +167,31 @@ export function CourseDashboard() {
141167
<SearchBar onChange={handleSearchChange} />
142168
</div>
143169
</div>
144-
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
145-
{courseList}
170+
171+
{/* My Courses Section */}
172+
<div className="mt-10">
173+
<h2 className="text-xl font-semibold mb-4">My Courses</h2>
174+
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
175+
{myCourses}
176+
</div>
177+
</div>
178+
179+
{/* All Courses Section */}
180+
<div className="mt-10">
181+
<h2 className="text-xl font-semibold mb-4">All Courses</h2>
182+
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
183+
<div className="course-list">
184+
{loading ? (
185+
<p>Loading courses...</p>
186+
) : filteredCourses.length > 0 ? (
187+
allCourses
188+
) : (
189+
<div className="course-box">
190+
<p>Nothing to show, yet</p>
191+
</div>
192+
)}
193+
</div>
194+
</div>
146195
</div>
147196
</div>
148197
);

0 commit comments

Comments
 (0)