From e577a90d40c809f93792f50c6b9bf6f7eae661ce Mon Sep 17 00:00:00 2001 From: Matthias Seghers Date: Tue, 12 Mar 2024 21:47:15 +0100 Subject: [PATCH] first dataLoader --- frontend/src/App.tsx | 35 ---------------------- frontend/src/dataloaders/StudentLoader.ts | 17 +++++++++++ frontend/src/main.tsx | 10 +++++-- frontend/src/pages/student/HomeStudent.tsx | 7 +++++ frontend/src/utils/.keep | 0 frontend/src/utils/ApiFetch.ts | 12 ++++++++ frontend/src/utils/ApiInterfaces.ts | 17 +++++++++++ 7 files changed, 60 insertions(+), 38 deletions(-) delete mode 100644 frontend/src/App.tsx create mode 100644 frontend/src/dataloaders/StudentLoader.ts delete mode 100644 frontend/src/utils/.keep create mode 100644 frontend/src/utils/ApiFetch.ts create mode 100644 frontend/src/utils/ApiInterfaces.ts diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx deleted file mode 100644 index a860f91c..00000000 --- a/frontend/src/App.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import {JSX, useState} from 'react' -import reactLogo from './assets/react.svg' -import viteLogo from '/vite.svg' -import './App.css' - -function App(): JSX.Element { - const [count, setCount] = useState(0) - - return ( - <> -
- - Vite logo - - - React logo - -
-

Vite + React

-
- -

- Edit src/App.tsx and save to test HMR -

-
-

- Click on the Vite and React logos to learn more -

- - ) -} - -export default App diff --git a/frontend/src/dataloaders/StudentLoader.ts b/frontend/src/dataloaders/StudentLoader.ts new file mode 100644 index 00000000..f5e1c270 --- /dev/null +++ b/frontend/src/dataloaders/StudentLoader.ts @@ -0,0 +1,17 @@ +import apiFetch from "../utils/ApiFetch.ts"; +import {Project, Subject} from "../utils/ApiInterfaces.ts"; + +export interface studentLoaderObject { + projects: Project[] +} +export default async function studentLoader(): Promise { + const projects: Project[] = await (await apiFetch("/api/student/projects")).json() as Project[]; + const subjects: Subject[] = await (await apiFetch("/api/student/subjects")).json() as Subject[]; + for (let i = 0; i < projects.length; i++) { + const subject: Subject | undefined = subjects.find(subject => subject.id === projects[i].subject_id); + if (subject !== undefined) { + projects[i].subject_name = subject.name; + } + } + return {"projects": projects}; +} \ No newline at end of file diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index 3467c88e..15b215b1 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -1,5 +1,5 @@ import React from 'react' -import ReactDOM from 'react-dom/client' +import {createRoot} from 'react-dom/client' import {createBrowserRouter, RouterProvider,} from "react-router-dom"; import './index.css' import Root from './pages/root.tsx' @@ -10,6 +10,7 @@ import HomeStudent from "./pages/student/HomeStudent.tsx"; import HomeTeacher from "./pages/teacher/HomeTeacher.tsx"; import 'bulma/css/bulma.min.css'; import './assets/styles/mainpage.css' +import studentLoader from "./dataloaders/StudentLoader.ts"; const router = createBrowserRouter([ { @@ -27,14 +28,17 @@ const router = createBrowserRouter([ }, { path: "/student", - element: + element: , + id: "student", + loader: studentLoader }, { path: "/teacher", element: } ]); -ReactDOM.createRoot(document.getElementById('root')!).render( + +createRoot(document.getElementById('root')!).render( , diff --git a/frontend/src/pages/student/HomeStudent.tsx b/frontend/src/pages/student/HomeStudent.tsx index e4ab35e5..d53fa306 100644 --- a/frontend/src/pages/student/HomeStudent.tsx +++ b/frontend/src/pages/student/HomeStudent.tsx @@ -3,8 +3,15 @@ import {Header} from "../../components/Header.tsx"; import {Sidebar} from "../../components/Sidebar.tsx"; import ProjectCardStudent from "./ProjectCardStudent.tsx"; import '../../assets/styles/students_components.css' +import {useRouteLoaderData} from "react-router-dom"; +import {studentLoaderObject} from "../../dataloaders/StudentLoader.ts"; export default function HomeStudent(): JSX.Element { + + // data contains a list of Project in data.projects (i think) + const data: studentLoaderObject = useRouteLoaderData("student") as studentLoaderObject + console.log(data) // TODO: remove + return ( <>
diff --git a/frontend/src/utils/.keep b/frontend/src/utils/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/frontend/src/utils/ApiFetch.ts b/frontend/src/utils/ApiFetch.ts new file mode 100644 index 00000000..6ad7f44d --- /dev/null +++ b/frontend/src/utils/ApiFetch.ts @@ -0,0 +1,12 @@ +const DEBUG: boolean = true; // should always be false on the repo. + +export default function apiFetch(url: string, options?: RequestInit) { + if (typeof options === 'undefined') { + options = {} + } + if (DEBUG) { + url = "http://127.0.0.1:8000" + url; + } + // TODO: add auth things in options + return fetch(url, options) +} \ No newline at end of file diff --git a/frontend/src/utils/ApiInterfaces.ts b/frontend/src/utils/ApiInterfaces.ts new file mode 100644 index 00000000..09b38abf --- /dev/null +++ b/frontend/src/utils/ApiInterfaces.ts @@ -0,0 +1,17 @@ +export interface Subject { + id: number, + name: string +} + +export interface Project { + id: number, + name: string, + deadline: string | Date, + archived: boolean, + description: string, + requirements: string, + visible: string, + max_students: number, + subject_id: number + subject_name: string | undefined | null +}