This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from SELab-2/routeLoaderData
Route loader data Phase 2
- Loading branch information
Showing
10 changed files
with
122 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {Project, Subject} from "../utils/ApiInterfaces.ts"; | ||
import apiFetch from "../utils/ApiFetch.ts"; | ||
|
||
export enum teacherStudentRole { | ||
STUDENT = "student", | ||
TEACHER = "teacher" | ||
} | ||
|
||
export async function projectsLoader(role: teacherStudentRole): Promise<Project[]> { | ||
const getter = await getAllProjectsAndSubjects(role); | ||
const subjects = getter.subjects; | ||
const projects = getter.projects; | ||
// TODO: add submission data | ||
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; | ||
} | ||
|
||
export interface projectsAndSubjects { | ||
projects: Project[], | ||
subjects: Subject[] | ||
} | ||
|
||
export async function getAllProjectsAndSubjects(role: teacherStudentRole): Promise<projectsAndSubjects> { | ||
const projects: Project[] = await (await apiFetch(`/${role}/projects`)).json() as Project[]; | ||
const subjects: Subject[] = await (await apiFetch(`/${role}/subjects`)).json() as Subject[]; | ||
return {projects, subjects} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
import apiFetch from "../utils/ApiFetch.ts"; | ||
import {Project, Subject} from "../utils/ApiInterfaces.ts"; | ||
import {Project} from "../utils/ApiInterfaces.ts"; | ||
import {projectsLoader, teacherStudentRole} from "./SharedFunctions.ts"; | ||
|
||
export interface studentLoaderObject { | ||
projects: Project[] | ||
} | ||
|
||
export const STUDENT_ROUTER_ID = "student"; | ||
|
||
|
||
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; | ||
} | ||
} | ||
const projects: Project[] = await projectsLoader(teacherStudentRole.STUDENT); | ||
// TODO: add submission data | ||
return {"projects": projects}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {Project, properSubject, Subject} from "../utils/ApiInterfaces.ts"; | ||
import {getAllProjectsAndSubjects, projectsAndSubjects, teacherStudentRole} from "./SharedFunctions.ts"; | ||
|
||
export interface subjectsTeacherLoaderObject { | ||
subjects: properSubject[] | ||
} | ||
|
||
export const SUBJECT_TEACHER_ROUTER_ID = "subjectTeacher"; | ||
|
||
export default async function subjectsTeacherLoader(): Promise<subjectsTeacherLoaderObject> { | ||
const temp: projectsAndSubjects = await getAllProjectsAndSubjects(teacherStudentRole.TEACHER); | ||
const subjects: Subject[] = temp.subjects; | ||
const projects: Project[] = temp.projects; | ||
|
||
const p_subjects: properSubject[] = subjects.map(subject => { | ||
const active_projects = projects.filter(project => | ||
project.archived && project.subject_id === subject.id | ||
); | ||
return { | ||
id: subject.id, | ||
name: subject.name, | ||
active_projects: active_projects.length, | ||
first_deadline: null // TODO: add deadlines when needed api endpoints are added. | ||
}; | ||
}); | ||
return {"subjects": p_subjects} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Project} from "../utils/ApiInterfaces.ts"; | ||
import {projectsLoader, teacherStudentRole} from "./SharedFunctions.ts"; | ||
|
||
export interface teacherLoaderObject { | ||
projects: Project[] | ||
} | ||
|
||
export const TEACHER_ROUTER_ID = "teacher"; | ||
|
||
export default async function teacherLoader(): Promise<teacherLoaderObject> { | ||
const projects: Project[] = await projectsLoader(teacherStudentRole.TEACHER); | ||
return {"projects": projects} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {JSX} from "react"; | ||
import {SUBJECT_TEACHER_ROUTER_ID, subjectsTeacherLoaderObject} from "../../dataloaders/SubjectsTeacherLoader.ts"; | ||
import {useRouteLoaderData} from "react-router-dom"; | ||
|
||
export default function SubjectsTeacher(): JSX.Element { | ||
|
||
const data= useRouteLoaderData(SUBJECT_TEACHER_ROUTER_ID) as subjectsTeacherLoaderObject; | ||
console.log(data); | ||
|
||
return( | ||
<p>hier ziet de lkr al zijn vakken.</p> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters