|
1 | 1 | import type { UseMutationResult } from "@tanstack/react-query"; |
2 | 2 |
|
3 | | -import type { ExtendedCourse, ExtendedGroup } from "@/atoms/plan-family"; |
| 3 | +import type { ExtendedCourse } from "@/atoms/plan-family"; |
4 | 4 | import type { |
5 | 5 | CourseType, |
6 | | - LessonType, |
7 | 6 | PlanResponseType, |
8 | 7 | Registration, |
| 8 | + SingleCourse, |
| 9 | + SingleGroup, |
9 | 10 | } from "@/types"; |
10 | 11 |
|
| 12 | +import { serverToLocalPlan } from "./server-to-local-plan"; |
| 13 | + |
11 | 14 | type UpdateLocalPlanResult = |
12 | 15 | | { |
13 | 16 | status: "ERROR"; |
@@ -41,40 +44,19 @@ export const updateLocalPlan = async ( |
41 | 44 | for (const registration of onlinePlan.registrations) { |
42 | 45 | try { |
43 | 46 | 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 | + ); |
78 | 60 |
|
79 | 61 | // Add unique registrations to the updatedRegistrations array |
80 | 62 | updatedRegistrations = [...updatedRegistrations, registration].filter( |
|
0 commit comments