Co-authored-by: doprz <52579214+doprz@users.noreply.github.com> Co-authored-by: Samuel Gunter <29130894+Samathingamajig@users.noreply.github.com>
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
|
|
import { generateRandomId } from '@shared/util/random';
|
|
|
|
import handleDuplicate from './handleDuplicate';
|
|
|
|
/**
|
|
* Creates a new schedule with the given name
|
|
*
|
|
* @param scheduleName - The name of the schedule to create
|
|
* @returns Undefined if successful, otherwise an error message
|
|
*/
|
|
export default async function duplicateSchedule(scheduleId: string): Promise<string | undefined> {
|
|
const schedules = await UserScheduleStore.get('schedules');
|
|
const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
|
|
|
|
if (scheduleIndex === -1) {
|
|
throw new Error(`Schedule ${scheduleId} does not exist`);
|
|
}
|
|
|
|
const schedule = schedules[scheduleIndex]!;
|
|
|
|
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)),
|
|
hours: schedule.hours,
|
|
updatedAt: Date.now(),
|
|
} satisfies typeof schedule);
|
|
|
|
await UserScheduleStore.set('schedules', schedules);
|
|
|
|
// Automatically switch to the duplicated schedule
|
|
await UserScheduleStore.set('activeIndex', scheduleIndex + 1);
|
|
|
|
return undefined;
|
|
}
|