From e01bda8c40774bc321dc1a450e680d0e322afd41 Mon Sep 17 00:00:00 2001 From: Emanuele Coricciati Date: Fri, 11 Oct 2024 18:24:10 +0200 Subject: [PATCH] fix(courses): switch course with no id to a previous edition --- src/core/queries/courseHooks.ts | 54 ++++++++++++------- .../courses/components/CourseListItem.tsx | 10 ++-- src/features/courses/utils/courses.ts | 28 ++++++++++ 3 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 src/features/courses/utils/courses.ts diff --git a/src/core/queries/courseHooks.ts b/src/core/queries/courseHooks.ts index e9a64126..bf675b21 100644 --- a/src/core/queries/courseHooks.ts +++ b/src/core/queries/courseHooks.ts @@ -21,6 +21,7 @@ import { } from '@tanstack/react-query'; import { CourseLectureSection } from '../../features/courses/types/CourseLectureSections'; +import { isCourseDetailed } from '../../features/courses/utils/courses'; import { notNullish } from '../../utils/predicates'; import { pluckData } from '../../utils/queries'; import { courseColors } from '../constants'; @@ -56,10 +57,10 @@ const setupCourses = ( courses?.forEach(c => { const newC = c as CourseOverview; - + const hasDetails = isCourseDetailed(newC); newC.uniqueShortcode = c.shortcode + c.moduleNumber; - if (c.id && !(newC.uniqueShortcode in coursePreferences)) { + if (hasDetails && !(newC.uniqueShortcode in coursePreferences)) { const usedColors = Object.values(coursePreferences) .map(cp => cp.color) .filter(notNullish); @@ -158,24 +159,39 @@ export const useGetCourseEditions = (courseId: number) => { c.id === courseId || c.previousEditions.some(e => e.id === courseId), ); const editions: MenuAction[] = []; - if (!course || !course.previousEditions.length) return editions; - - editions.push( - { - id: `${course.id}`, - title: course.year, - state: courseId === course?.id ? 'on' : undefined, - }, - ...course.previousEditions.map( - e => - ({ - id: `${e.id}`, - title: e.year, - state: courseId === e.id ? 'on' : undefined, - } as MenuAction), - ), - ); + if (course.id) { + editions.push( + { + id: `${course.id}`, + title: course.year, + state: courseId === course?.id ? 'on' : undefined, + }, + ...course.previousEditions.map( + e => + ({ + id: `${e.id}`, + title: e.year, + state: courseId === e.id ? 'on' : undefined, + } as MenuAction), + ), + ); + } else { + const prevEditions = course.previousEditions + .filter(e => e.id !== null) + .sort((a, b) => +b.year - +a.year) + .slice(1); + editions.push( + ...prevEditions.map( + e => + ({ + id: `${e.id}`, + title: e.year, + state: courseId === e.id ? 'on' : undefined, + } as MenuAction), + ), + ); + } return editions; }, diff --git a/src/features/courses/components/CourseListItem.tsx b/src/features/courses/components/CourseListItem.tsx index 080cd902..f26e683f 100644 --- a/src/features/courses/components/CourseListItem.tsx +++ b/src/features/courses/components/CourseListItem.tsx @@ -17,6 +17,7 @@ import { getCourseKey } from '../../../core/queries/courseHooks'; import { CourseOverview } from '../../../core/types/api'; import { AGENDA_QUERY_PREFIX } from '../../agenda/queries/agendaHooks'; import { LECTURES_QUERY_PREFIX } from '../../agenda/queries/lectureHooks'; +import { getLatestCourseInfo, isCourseDetailed } from '../utils/courses'; import { CourseIndicator } from './CourseIndicator'; interface Props { @@ -79,7 +80,8 @@ export const CourseListItem = ({ const { colors, spacing, fontSizes } = useTheme(); const { t } = useTranslation(); - const hasDetails = course.id != null; + const hasDetails = isCourseDetailed(course); + const courseInfo = getLatestCourseInfo(course); const queryClient = useQueryClient(); const isDataMissing = useCallback( @@ -116,9 +118,9 @@ export const CourseListItem = ({ ? { screen: 'Course', params: { - id: course.id, - courseName: course.name, - uniqueShortcode: course.uniqueShortcode, + id: courseInfo?.id, + courseName: courseInfo?.name, + uniqueShortcode: courseInfo?.uniqueShortcode, }, } : undefined diff --git a/src/features/courses/utils/courses.ts b/src/features/courses/utils/courses.ts new file mode 100644 index 00000000..90d3d527 --- /dev/null +++ b/src/features/courses/utils/courses.ts @@ -0,0 +1,28 @@ +import { CourseOverview } from '../../../core/types/api'; + +export const isCourseDetailed = (course: CourseOverview) => { + if (course.id !== null) return true; + if (course.previousEditions.some(edition => edition.id !== null)) return true; + return false; +}; + +export const getLatestCourseInfo = (course: CourseOverview) => { + if (course.id !== null) { + return { + id: course.id, + name: course.name, + uniqueShortcode: course.uniqueShortcode, + }; + } + const latestEdition = course.previousEditions + .filter(edition => edition.id !== null) + .sort((a, b) => +b.year - +a.year)[0]; + if (latestEdition) { + return { + id: latestEdition.id, + name: course.name, + uniqueShortcode: course.uniqueShortcode, + }; + } + return null; +};