From 8b7aae1f0df59977a7236a69fbe08f6e04ce124e Mon Sep 17 00:00:00 2001
From: Aron Buzogany <108480125+AronBuzogany@users.noreply.github.com>
Date: Thu, 23 May 2024 20:46:05 +0200
Subject: [PATCH 1/2] redirecting to homepage when user not logged in (#421)
* redirecting to homepage when user not logged in
* linting
---
frontend/src/App.tsx | 1 +
frontend/src/components/Header/Layout.tsx | 41 ++++++++++++++++++++++-
2 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 6d2cad70..a3c39927 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -35,6 +35,7 @@ const router = createBrowserRouter(
>
} loader={fetchProjectPage} />
}>
+ } loader={fetchProjectPage} />
} loader={fetchProjectPage} />
{
+ if (
+ !meData.loggedIn &&
+ !(
+ location.pathname === "/" ||
+ /\/([a-z]{2})?\/home/.test(location.pathname)
+ )
+ ) {
+ navigate("/");
+ }
+ }, [meData.loggedIn, location.pathname, navigate]);
return (
<>
@@ -16,3 +38,20 @@ export default function Layout(): JSX.Element {
>
);
}
+
+const useEnsureLangCodeInPath = () => {
+ const location = useLocation();
+ const navigate = useNavigate();
+
+ useEffect(() => {
+ const pathParts = location.pathname.split("/").filter(Boolean);
+ const langCode = i18next.resolvedLanguage;
+
+ // Check if the URL starts with the lang code
+ if (pathParts[0] !== langCode) {
+ // Prepend the lang code to the path
+ const newPath = `/${langCode}/${pathParts.join("/")}`;
+ navigate(newPath);
+ }
+ }, [location, navigate]);
+};
\ No newline at end of file
From d3bcf1fffa706042a4aae683f24f4b1eda4f64c4 Mon Sep 17 00:00:00 2001
From: Aron Buzogany <108480125+AronBuzogany@users.noreply.github.com>
Date: Thu, 23 May 2024 20:46:24 +0200
Subject: [PATCH 2/2] only fetching admins when needed (#420)
---
.../src/components/Courses/CourseUtils.tsx | 104 ++++++++++--------
1 file changed, 61 insertions(+), 43 deletions(-)
diff --git a/frontend/src/components/Courses/CourseUtils.tsx b/frontend/src/components/Courses/CourseUtils.tsx
index 8525f930..d06202ff 100644
--- a/frontend/src/components/Courses/CourseUtils.tsx
+++ b/frontend/src/components/Courses/CourseUtils.tsx
@@ -41,13 +41,15 @@ export function loggedInToken() {
* @returns The username.
*/
export async function getUser(uid: string): Promise {
- return authenticatedFetch(`${apiHost}/users/${getIdFromLink(uid)}`).then((response) => {
- if (response.ok) {
- return response.json().then((data) => {
- return data.data;
- });
+ return authenticatedFetch(`${apiHost}/users/${getIdFromLink(uid)}`).then(
+ (response) => {
+ if (response.ok) {
+ return response.json().then((data) => {
+ return data.data;
+ });
+ }
}
- })
+ );
}
/**
@@ -124,14 +126,14 @@ const fetchData = async (url: string, params?: URLSearchParams) => {
export const dataLoaderCourses = async () => {
//const params = new URLSearchParams({ 'teacher': loggedInUid() });
- const courses = await fetchData(`courses`);
+ const courses = await fetchData(`courses`);
const projects = await fetchProjectsCourse(courses);
const me = await fetchMe();
- for( const c of courses){
- const teacher = await fetchData(`users/${c.teacher}`)
- c.teacher = teacher.display_name
+ for (const c of courses) {
+ const teacher = await fetchData(`users/${c.teacher}`);
+ c.teacher = teacher.display_name;
}
- return {courses, projects, me}
+ return { courses, projects, me };
};
/**
@@ -139,7 +141,7 @@ export const dataLoaderCourses = async () => {
* @param courses - All the courses
* @returns the projects
*/
-export async function fetchProjectsCourse (courses:Course[]) {
+export async function fetchProjectsCourse(courses: Course[]) {
const projectPromises = courses.map((course) =>
authenticatedFetch(
`${apiHost}/projects?course_id=${getIdFromLink(course.course_id)}`
@@ -149,30 +151,32 @@ export async function fetchProjectsCourse (courses:Course[]) {
const projectResults = await Promise.all(projectPromises);
const projectsMap: { [courseId: string]: ProjectDetail[] } = {};
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", {
- status: projectRes.status,
- });
- }
- const projectJson = await projectRes.json();
- const projectData = projectJson.data;
- let projectDeadlines = [];
- if (projectData.deadlines) {
- projectDeadlines = projectData.deadlines.map(
- ([description, dateString]: [string, string]) => ({
- description,
- date: new Date(dateString),
- })
- );
- }
- const project: ProjectDetail = {
- ...item,
- deadlines: projectDeadlines,
- };
- return project;
- }));
+ 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", {
+ status: projectRes.status,
+ });
+ }
+ const projectJson = await projectRes.json();
+ const projectData = projectJson.data;
+ let projectDeadlines = [];
+ if (projectData.deadlines) {
+ projectDeadlines = projectData.deadlines.map(
+ ([description, dateString]: [string, string]) => ({
+ description,
+ date: new Date(dateString),
+ })
+ );
+ }
+ const project: ProjectDetail = {
+ ...item,
+ deadlines: projectDeadlines,
+ };
+ return project;
+ })
+ );
}
return { ...projectsMap };
}
@@ -228,7 +232,7 @@ const dataLoaderStudents = async (courseId: string) => {
const fetchMes = async (uids: string[]) => {
return Promise.all(uids.map((uid) => getUser(uid)));
-}
+};
export const dataLoaderCourseDetail = async ({
params,
@@ -239,14 +243,28 @@ export const dataLoaderCourseDetail = async ({
if (!courseId) {
throw new Error("Course ID is undefined.");
}
+ const me = await fetchMe();
+
const course = await dataLoaderCourse(courseId);
+
+ const courseAdminuids = course["admins"].map((admin: string) => {
+ const urlSplit = admin.split("/");
+ return urlSplit[urlSplit.length - 1];
+ });
+
const projects = await dataLoaderProjects(courseId);
- const admins = await dataLoaderAdmins(courseId);
+ let adminMes: Me[] = [];
+ if (me.uid === course.teacher || courseAdminuids.includes(me.uid)) {
+ const admins = await dataLoaderAdmins(courseId);
+ const adminUids = admins.map((admin: { uid: string }) =>
+ getIdFromLink(admin.uid)
+ );
+ adminMes = await fetchMes([course.teacher, ...adminUids]);
+ }
const students = await dataLoaderStudents(courseId);
- const admin_uids = admins.map((admin: {uid: string}) => getIdFromLink(admin.uid));
- const student_uids = students.map((student: {uid: string}) => getIdFromLink(student.uid));
- const adminMes = await fetchMes([course.teacher, ...admin_uids]);
+ const student_uids = students.map((student: { uid: string }) =>
+ getIdFromLink(student.uid)
+ );
const studentMes = await fetchMes(student_uids);
- const me = await fetchMe();
- return { course, projects, adminMes, studentMes, me};
+ return { course, projects, adminMes, studentMes, me };
};