Skip to content

Commit 2f63a25

Browse files
committed
refactor: move server to local plan logic to one file
1 parent e2b3715 commit 2f63a25

4 files changed

Lines changed: 106 additions & 106 deletions

File tree

frontend/src/app/plans/edit/[id]/_components/app-sidebar.tsx

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Link from "next/link";
88
import React from "react";
99

1010
import { getFaculties } from "@/actions/get-faculties";
11-
import type { ExtendedCourse, ExtendedGroup } from "@/atoms/plan-family";
1211
import { GroupsAccordionItem } from "@/components/groups-accordion";
1312
import { Icons } from "@/components/icons";
1413
import { PlanDisplayLink } from "@/components/plan-display-link";
@@ -33,12 +32,8 @@ import { Skeleton } from "@/components/ui/skeleton";
3332
import { fetchClient } from "@/lib/fetch";
3433
import type { usePlanType } from "@/lib/use-plan";
3534
import { registrationReplacer } from "@/lib/utils";
36-
import type {
37-
CourseType,
38-
FacultyType,
39-
LessonType,
40-
PlanResponseType,
41-
} from "@/types";
35+
import { serverToLocalPlan } from "@/lib/utils/server-to-local-plan";
36+
import type { CourseType, FacultyType, PlanResponseType } from "@/types";
4237

4338
import { OfflineAlert } from "./offline-alert";
4439
import { SyncErrorAlert } from "./sync-error-alert";
@@ -240,48 +235,11 @@ export function AppSidebar({
240235
} else {
241236
coursesFunction.mutate(selectedRegistration.id, {
242237
onSuccess: (data) => {
243-
const extendedCourses: ExtendedCourse[] = data
244-
.map((c) => ({
245-
id: c.id,
246-
name: c.name,
247-
isChecked: true,
248-
registrationId: c.registrationId,
249-
type: c.groups.at(0)?.type ?? ("" as LessonType),
250-
groups: c.groups.map(
251-
(g) =>
252-
({
253-
groupId: g.group + c.id + g.type,
254-
groupNumber: g.group.toString(),
255-
groupOnlineId: g.id,
256-
courseId: c.id,
257-
courseName: c.name,
258-
isChecked: false,
259-
courseType: g.type,
260-
day: g.day,
261-
lecturer: g.lecturer,
262-
registrationId: c.registrationId,
263-
week: g.week.replace("-", "") as
264-
| ""
265-
| "TN"
266-
| "TP",
267-
endTime: g.endTime
268-
.split(":")
269-
.slice(0, 2)
270-
.join(":"),
271-
startTime: g.startTime
272-
.split(":")
273-
.slice(0, 2)
274-
.join(":"),
275-
spotsOccupied: g.spotsOccupied,
276-
spotsTotal: g.spotsTotal,
277-
averageRating: g.averageRating,
278-
opinionsCount: g.opinionsCount,
279-
}) satisfies ExtendedGroup,
280-
),
281-
}))
282-
.sort((a, b) => {
283-
return a.name.localeCompare(b.name);
284-
});
238+
const extendedCourses = serverToLocalPlan(
239+
data,
240+
true,
241+
false,
242+
);
285243
plan.addRegistration(
286244
selectedRegistration,
287245
extendedCourses,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { ExtendedCourse, ExtendedGroup } from "@/atoms/plan-family";
2+
import type {
3+
CourseType,
4+
LessonType,
5+
SingleCourse,
6+
SingleGroup,
7+
} from "@/types";
8+
9+
export const serverToLocalPlan = (
10+
courses: CourseType,
11+
shouldCourseBeChecked: ((course: SingleCourse) => boolean) | boolean,
12+
shouldGroupBeChecked:
13+
| ((course: SingleCourse, group: SingleGroup) => boolean)
14+
| boolean,
15+
) => {
16+
const extendedCourses: ExtendedCourse[] = courses
17+
.map((c) => ({
18+
id: c.id,
19+
name: c.name,
20+
isChecked:
21+
typeof shouldCourseBeChecked === "boolean"
22+
? shouldCourseBeChecked
23+
: shouldCourseBeChecked(c),
24+
registrationId: c.registrationId,
25+
type: c.groups.at(0)?.type ?? ("" as LessonType),
26+
groups: c.groups.map(
27+
(g) =>
28+
({
29+
groupId: g.group + c.id + g.type,
30+
groupNumber: g.group.toString(),
31+
groupOnlineId: g.id,
32+
courseId: c.id,
33+
courseName: c.name,
34+
isChecked:
35+
typeof shouldGroupBeChecked === "boolean"
36+
? shouldGroupBeChecked
37+
: shouldGroupBeChecked(c, g),
38+
courseType: g.type,
39+
day: g.day,
40+
lecturer: g.lecturer,
41+
registrationId: c.registrationId,
42+
week: g.week.replace("-", "") as "" | "TN" | "TP",
43+
endTime: g.endTime.split(":").slice(0, 2).join(":"),
44+
startTime: g.startTime.split(":").slice(0, 2).join(":"),
45+
spotsOccupied: g.spotsOccupied,
46+
spotsTotal: g.spotsTotal,
47+
averageRating: g.averageRating,
48+
opinionsCount: g.opinionsCount,
49+
}) satisfies ExtendedGroup,
50+
),
51+
}))
52+
.sort((a, b) => {
53+
return a.name.localeCompare(b.name);
54+
});
55+
return extendedCourses;
56+
};

frontend/src/lib/utils/update-local-plan.ts

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import type { UseMutationResult } from "@tanstack/react-query";
22

3-
import type { ExtendedCourse, ExtendedGroup } from "@/atoms/plan-family";
3+
import type { ExtendedCourse } from "@/atoms/plan-family";
44
import type {
55
CourseType,
6-
LessonType,
76
PlanResponseType,
87
Registration,
8+
SingleCourse,
9+
SingleGroup,
910
} from "@/types";
1011

12+
import { serverToLocalPlan } from "./server-to-local-plan";
13+
1114
type UpdateLocalPlanResult =
1215
| {
1316
status: "ERROR";
@@ -41,40 +44,19 @@ export const updateLocalPlan = async (
4144
for (const registration of onlinePlan.registrations) {
4245
try {
4346
const courses = await coursesFunction.mutateAsync(registration.id);
44-
const extendedCourses: ExtendedCourse[] = courses
45-
.map((c) => {
46-
const groups: ExtendedGroup[] = c.groups.map((g) => ({
47-
groupId: g.group + c.id + g.type,
48-
groupNumber: g.group.toString(),
49-
groupOnlineId: g.id,
50-
courseId: c.id,
51-
courseName: c.name,
52-
isChecked:
53-
onlinePlan.courses
54-
.find((oc) => oc.id === c.id)
55-
?.groups.some((og) => og.id === g.id) ?? false,
56-
courseType: g.type,
57-
day: g.day,
58-
lecturer: g.lecturer,
59-
registrationId: c.registrationId,
60-
week: g.week.replace("-", "") as "" | "TN" | "TP",
61-
endTime: g.endTime.split(":").slice(0, 2).join(":"),
62-
startTime: g.startTime.split(":").slice(0, 2).join(":"),
63-
spotsOccupied: g.spotsOccupied,
64-
spotsTotal: g.spotsTotal,
65-
opinionsCount: g.opinionsCount,
66-
averageRating: g.averageRating,
67-
}));
68-
return {
69-
id: c.id,
70-
name: c.name,
71-
isChecked: onlinePlan.courses.some((oc) => oc.id === c.id),
72-
registrationId: c.registrationId,
73-
type: c.groups.at(0)?.type ?? ("" as LessonType),
74-
groups,
75-
};
76-
})
77-
.sort((a, b) => a.name.localeCompare(b.name));
47+
const extendedCourses = serverToLocalPlan(
48+
courses,
49+
(course: SingleCourse) => {
50+
return onlinePlan.courses.some((oc) => oc.id === course.id);
51+
},
52+
(courseId: SingleCourse, groupId: SingleGroup) => {
53+
return (
54+
onlinePlan.courses
55+
.find((oc) => oc.id === courseId.id)
56+
?.groups.some((og) => og.id === groupId.id) ?? false
57+
);
58+
},
59+
);
7860

7961
// Add unique registrations to the updatedRegistrations array
8062
updatedRegistrations = [...updatedRegistrations, registration].filter(

frontend/src/types/index.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,34 @@ export interface DeletePlanResponseType {
124124
message: string;
125125
}
126126

127-
export type CourseType = {
127+
export interface SingleGroup {
128+
id: number;
129+
name: string;
130+
startTime: string;
131+
endTime: string;
132+
group: string;
133+
lecturer: string;
134+
week: "-" | "TN" | "TP";
135+
day: Day;
136+
type: "C" | "L" | "P" | "S" | "W";
137+
url: string;
138+
courseId: string;
139+
spotsOccupied: number;
140+
spotsTotal: number;
141+
averageRating: number;
142+
createdAt: string;
143+
updatedAt: string;
144+
opinionsCount: number;
145+
}
146+
147+
export interface SingleCourse {
128148
id: string;
129149
name: string;
130150
registrationId: string;
131-
groups: {
132-
id: number;
133-
name: string;
134-
startTime: string;
135-
endTime: string;
136-
group: string;
137-
lecturer: string;
138-
week: "-" | "TN" | "TP";
139-
day: Day;
140-
type: "C" | "L" | "P" | "S" | "W";
141-
url: string;
142-
courseId: string;
143-
spotsOccupied: number;
144-
spotsTotal: number;
145-
averageRating: number;
146-
createdAt: string;
147-
updatedAt: string;
148-
opinionsCount: number;
149-
}[];
150-
}[];
151+
groups: SingleGroup[];
152+
}
153+
154+
export type CourseType = SingleCourse[];
151155

152156
export type FacultyType = {
153157
id: string;

0 commit comments

Comments
 (0)