diff --git a/frontend/src/components/CoursesWidget.jsx b/frontend/src/components/CoursesWidget.jsx index f1501da..1cda99d 100644 --- a/frontend/src/components/CoursesWidget.jsx +++ b/frontend/src/components/CoursesWidget.jsx @@ -2,54 +2,24 @@ import { useContext, useEffect, useState } from "react"; import { Card, Container } from "react-bootstrap"; import { Link } from "wouter"; import UserContext from "../contexts/UserContext"; +import { makeRequest } from "../utils.ts"; const CoursesWidget = ({ className = "" }) => { - const [courseData, setCourseData] = useState({}); + const [courseData, setCourseData] = useState([]); const { currentUser } = useContext(UserContext); - const dummyData = [ - { - course_id: 1, - course_title: "Advanced Website Design", - couse_code: "COMP 2707", - instructor: "Saja Al Mamoori", - }, - { - course_id: 2, - course_title: "Introduction to Roman Civilization", - couse_code: "GRST 1200", - instructor: "Max Nelson", - }, - { - course_id: 3, - course_title: "Software Verification and Testing", - couse_code: "COMP 4110", - instructor: "Serif Saad", - }, - { - course_id: 4, - course_title: "Selected Topics in Software Engineering", - couse_code: "COMP 4800", - instructor: "Jessica Chen", - }, - { - course_id: 5, - course_title: "Project Management: Techniques and Tools", - couse_code: "COMP 4990", - instructor: "Arunita Jaekel", - }, - ]; useEffect(() => { - fetch(`http://localhost:5000/user/${currentUser.id}/courses`) + makeRequest({ url: `http://localhost:5000/user/${currentUser.id}/courses` }) .then((resp) => resp.json()) .then((data) => { setCourseData(data.courses); }); - }, []); + }, [setCourseData, currentUser]); + return (
- {dummyData.map((course, i) => { + {courseData.map((course, i) => { return ( { className="col col-lg-2" > -

{course.couse_code}

+

{course.course_code}

- {course.course_title} + {course.name} {course.instructor}
diff --git a/frontend/src/utils.ts b/frontend/src/utils.ts index 5daef16..a8d050e 100644 --- a/frontend/src/utils.ts +++ b/frontend/src/utils.ts @@ -1,11 +1,14 @@ -const makeRequest = ({ url, method, body = {} }): Promise => { - return fetch(url, { +const makeRequest = ({ url, method, body = null }): Promise => { + const req: RequestInit = { method: method, credentials: "include", - body: JSON.stringify(body), headers: { "content-type": "application/json" }, mode: "cors", - }); + }; + if (body) { + req["body"] = JSON.stringify(body); + } + return fetch(url, req); }; const sendLoginRequest = async (