Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(courses): switch courses with no id to a previous edition, if available #532

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};