Skip to content

Commit

Permalink
fix(courses): switch course with no id to a previous edition
Browse files Browse the repository at this point in the history
  • Loading branch information
emacoricciati committed Oct 11, 2024
1 parent 3ac5546 commit e01bda8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 23 deletions.
54 changes: 35 additions & 19 deletions src/core/queries/courseHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
},
Expand Down
10 changes: 6 additions & 4 deletions src/features/courses/components/CourseListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/features/courses/utils/courses.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit e01bda8

Please sign in to comment.