feat: use "copy of" for duplicated schedules and place them under the original schedule #358 (#397)

* feat: use "copy of" for duplicated schedules and place them under the original schedule #358

* fix: code style and consistency
This commit is contained in:
Kabir Ramzan
2024-10-28 17:56:02 -05:00
committed by GitHub
parent 8de88d6ad7
commit 94744e01b9

View File

@@ -10,15 +10,18 @@ import handleDuplicate from './handleDuplicate';
*/ */
export default async function duplicateSchedule(scheduleId: string): Promise<string | undefined> { export default async function duplicateSchedule(scheduleId: string): Promise<string | undefined> {
const schedules = await UserScheduleStore.get('schedules'); const schedules = await UserScheduleStore.get('schedules');
const schedule = schedules.find(schedule => schedule.id === scheduleId); const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
if (schedule === undefined) { if (scheduleIndex === -1) {
throw new Error(`Schedule ${scheduleId} does not exist`); throw new Error(`Schedule ${scheduleId} does not exist`);
} }
const updatedName = await handleDuplicate(schedule.name); const schedule = schedules[scheduleIndex]!;
schedules.push({ const copyOfName = `Copy of ${schedule.name}`;
const updatedName = await handleDuplicate(copyOfName);
schedules.splice(scheduleIndex + 1, 0, {
id: generateRandomId(), id: generateRandomId(),
name: updatedName, name: updatedName,
courses: JSON.parse(JSON.stringify(schedule.courses)), courses: JSON.parse(JSON.stringify(schedule.courses)),