Skip to content

Commit

Permalink
added loader
Browse files Browse the repository at this point in the history
  • Loading branch information
warreprovoost committed May 12, 2024
1 parent 56f00bf commit 5372691
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
12 changes: 9 additions & 3 deletions frontend/src/components/Courses/AllCoursesTeacher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { SideScrollableCourses } from "./CourseUtilComponents";
import { Course, callToApiToCreateCourse } from "./CourseUtils";
import {Course, callToApiToCreateCourse, ProjectDetail} from "./CourseUtils";
import { Title } from "../Header/Title";
import { useLoaderData } from "react-router-dom";

Expand All @@ -12,7 +12,13 @@ import { useLoaderData } from "react-router-dom";
*/
export function AllCoursesTeacher(): JSX.Element {
const [open, setOpen] = useState(false);
const courses = (useLoaderData() as Course[]);
const loader = useLoaderData() as {
courses: Course[];
projects: { [courseId: string]: ProjectDetail[] }
};
const courses = loader.courses;
const projects = loader.projects;


Check failure on line 22 in frontend/src/components/Courses/AllCoursesTeacher.tsx

View workflow job for this annotation

GitHub Actions / Frontend-tests

More than 1 blank line not allowed
const [courseName, setCourseName] = useState('');
const [error, setError] = useState('');
Expand Down Expand Up @@ -49,7 +55,7 @@ export function AllCoursesTeacher(): JSX.Element {
<>
<Title title={t('title')}></Title>
<Grid container direction={'column'} style={{marginTop: '1rem', width:'100vw', height: '80vh'}}>
<SideScrollableCourses courses={courses}></SideScrollableCourses>
<SideScrollableCourses courses={courses} projects={projects}></SideScrollableCourses>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>{t('courseForm')}</DialogTitle>
<form style={{ margin: "2rem" }} onSubmit={handleSubmit}>
Expand Down
16 changes: 6 additions & 10 deletions frontend/src/components/Courses/CourseUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export const dataLoaderCourses = async () => {
/**
* Fetch the projects for the Course component
* @param courses - All the courses
* @returns the projects
*/
export async function fetchProjectsCourse (courses:Course[]) {
const projectPromises = courses.map((course) =>
Expand All @@ -152,9 +153,8 @@ export async function fetchProjectsCourse (courses:Course[]) {

const projectResults = await Promise.all(projectPromises);
const projectsMap: { [courseId: string]: ProjectDetail[] } = {};

projectResults.forEach((result, index) => {
const detailProjectPromises = result.data.map(async (item: Project) => {
for await (const [index, result] of projectResults.entries()) {
projectsMap[getIdFromLink(courses[index].course_id)] = await Promise.all(result.data.map(async (item: Project) => {
const projectRes = await authenticatedFetch(item.project_id);
if (projectRes.status !== 200) {
throw new Response("Failed to fetch project data", {
Expand All @@ -177,13 +177,9 @@ export async function fetchProjectsCourse (courses:Course[]) {
deadlines: projectDeadlines,
};
return project;
});
Promise.all(detailProjectPromises).then((projects) => {
projectsMap[getIdFromLink(courses[index].course_id)] = projects;
setProjects({ ...projectsMap });
});
});
setProjects(projectsMap);
}));
}
return { ...projectsMap };
}

const dataLoaderCourse = async (courseId: string) => {
Expand Down

0 comments on commit 5372691

Please sign in to comment.