diff --git a/src/pages/background/lib/duplicateSchedule.ts b/src/pages/background/lib/duplicateSchedule.ts index 753d1b79..1104a958 100644 --- a/src/pages/background/lib/duplicateSchedule.ts +++ b/src/pages/background/lib/duplicateSchedule.ts @@ -10,15 +10,18 @@ import handleDuplicate from './handleDuplicate'; */ export default async function duplicateSchedule(scheduleId: string): Promise { 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`); } - 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(), name: updatedName, courses: JSON.parse(JSON.stringify(schedule.courses)),