Skip to content

Commit 4b7d17f

Browse files
committed
Refactor the code and fix tiny bug
1 parent 6203251 commit 4b7d17f

File tree

2 files changed

+449
-449
lines changed

2 files changed

+449
-449
lines changed

course-matrix/frontend/src/pages/TimetableBuilder/Calendar.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function Calendar({
7878
const navigate = useNavigate();
7979
const [queryParams] = useSearchParams();
8080
const isEditingTimetable = queryParams.has("edit");
81-
const timetableId = parseInt(queryParams.get("edit") ?? "0");
81+
const editingTimetableId = parseInt(queryParams.get("edit") ?? "0");
8282

8383
const [createTimetable] = useCreateTimetableMutation();
8484
const [createEvent] = useCreateEventMutation();
@@ -152,10 +152,10 @@ function Calendar({
152152
return;
153153
}
154154
setTimetableTitle(
155-
timetablesData?.find((timetable) => timetable.id === timetableId)
155+
timetablesData?.find((timetable) => timetable.id === editingTimetableId)
156156
?.timetable_title ?? "",
157157
);
158-
}, [timetablesData, timetableId, timetableTitle, isEditingTimetable]);
158+
}, [timetablesData, editingTimetableId, timetableTitle, isEditingTimetable]);
159159

160160
const handleCreate = async () => {
161161
// Create timetable
@@ -169,10 +169,10 @@ function Calendar({
169169
}
170170

171171
// Create course events for the newly created timetable
172-
const timetableId = data[0]?.id;
172+
const newTimetableId = data?.id;
173173
const promises = newOfferingIds.map(async (offeringId) => {
174174
const { error: offeringError } = await createEvent({
175-
calendar_id: timetableId,
175+
calendar_id: newTimetableId,
176176
offering_id: offeringId,
177177
semester_start_date: semesterStartDate,
178178
semester_end_date: semesterEndDate,
@@ -187,7 +187,7 @@ function Calendar({
187187
const restrictions = form.getValues("restrictions") ?? [];
188188
const restrictionPromises = restrictions.map(async (restriction) => {
189189
const restrictionObject = {
190-
calendar_id: timetableId,
190+
calendar_id: newTimetableId,
191191
type: restriction.type,
192192
days: restriction.days,
193193
start_time: restriction.startTime,
@@ -204,12 +204,12 @@ function Calendar({
204204
await Promise.all(restrictionPromises);
205205

206206
// Redirect to the home page to see the newly created timetable
207-
navigate("/home");
207+
// navigate("/home");
208208
};
209209

210210
const handleUpdate = async () => {
211211
// Delete the existing timetable
212-
const { error: deleteError } = await deleteTimetable(timetableId);
212+
const { error: deleteError } = await deleteTimetable(editingTimetableId);
213213
if (deleteError) {
214214
console.error(deleteError);
215215
}

0 commit comments

Comments
 (0)