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

dataLoaders phase 1 #79

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions frontend/src/App.tsx

This file was deleted.

18 changes: 18 additions & 0 deletions frontend/src/dataloaders/StudentLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
}
}
// TODO: add submission data
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 = false; // 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
}