Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
first dataLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
matt01y committed Mar 12, 2024
1 parent 4772015 commit e577a90
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 38 deletions.
35 changes: 0 additions & 35 deletions frontend/src/App.tsx

This file was deleted.

17 changes: 17 additions & 0 deletions frontend/src/dataloaders/StudentLoader.ts
Original file line number Diff line number Diff line change
@@ -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<studentLoaderObject> {
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};
}
10 changes: 7 additions & 3 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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([
{
Expand All @@ -27,14 +28,17 @@ const router = createBrowserRouter([
},
{
path: "/student",
element: <HomeStudent/>
element: <HomeStudent/>,
id: "student",
loader: studentLoader
},
{
path: "/teacher",
element: <HomeTeacher/>
}
]);
ReactDOM.createRoot(document.getElementById('root')!).render(

createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<RouterProvider router={router}/>
</React.StrictMode>,
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/pages/student/HomeStudent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
<div className={"main-header"}>
Expand Down
Empty file removed frontend/src/utils/.keep
Empty file.
12 changes: 12 additions & 0 deletions frontend/src/utils/ApiFetch.ts
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 17 additions & 0 deletions frontend/src/utils/ApiInterfaces.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e577a90

Please sign in to comment.