From 92330dc7fd3f4c0812af663a99054b37157d3601 Mon Sep 17 00:00:00 2001 From: FabrizioCostaMedich Date: Wed, 3 Jul 2024 00:26:50 +0200 Subject: [PATCH] fix(exams): sort exams by date in the TeachingScreen.tsx, Ref #504 --- .../teaching/screens/TeachingScreen.tsx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/features/teaching/screens/TeachingScreen.tsx b/src/features/teaching/screens/TeachingScreen.tsx index ea65ffb1..8fc3f17c 100644 --- a/src/features/teaching/screens/TeachingScreen.tsx +++ b/src/features/teaching/screens/TeachingScreen.tsx @@ -28,6 +28,8 @@ import { Theme } from '@lib/ui/types/Theme'; import { ExamStatusEnum } from '@polito/api-client'; import { NativeStackScreenProps } from '@react-navigation/native-stack'; +import { DateTime } from 'luxon'; + import { BottomBarSpacer } from '../../../core/components/BottomBarSpacer'; import { usePreferencesContext } from '../../../core/contexts/PreferencesContext'; import { useNotifications } from '../../../core/hooks/useNotifications'; @@ -83,8 +85,19 @@ export const TeachingScreen = ({ navigation }: Props) => { return ( examsQuery.data - .filter(e => !hiddenCourses.includes(e.uniqueShortcode)) - .sort(e => (e.status === ExamStatusEnum.Booked ? -1 : 1)) + .filter( + e => + !hiddenCourses.includes(e.uniqueShortcode) && + e.examEndsAt!.valueOf() > DateTime.now().toJSDate().valueOf(), + ) + .sort((a, b) => { + const status = + (a.status === ExamStatusEnum.Booked ? -1 : 0) + + (b.status === ExamStatusEnum.Booked ? 1 : 0); + return status !== 0 + ? status + : a.examStartsAt!.valueOf() - b.examStartsAt!.valueOf(); + }) .slice(0, 4) ?? [] ); }, [coursePreferences, coursesQuery.data, examsQuery.data]);