* feat: enhance schedule deletion to create a new schedule if none remain * feat: set active index to new schedule if only one exists * chore: run lint * feat: enhance schedule deletion to create a new schedule if none remain * feat: set active index to new schedule if only one exists * chore: run lint * feat: reset schedules on update, refactor invariant to within deleteSchedule * chore: pnpm lint --------- Co-authored-by: Samuel Gunter <29130894+Samathingamajig@users.noreply.github.com> Co-authored-by: Samuel Gunter <sgunter@utexas.edu>
51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
|
|
|
|
import createSchedule from './createSchedule';
|
|
|
|
/**
|
|
* Deletes a schedule with the specified name.
|
|
*
|
|
* @param scheduleId - The id of the schedule to delete.
|
|
* @returns A promise that resolves to a string if there is an error, or undefined if the schedule is deleted successfully.
|
|
*/
|
|
export default async function deleteSchedule(scheduleId: string): Promise<string | undefined> {
|
|
const [schedules, activeIndex] = await Promise.all([
|
|
UserScheduleStore.get('schedules'),
|
|
UserScheduleStore.get('activeIndex'),
|
|
]);
|
|
|
|
const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
|
|
if (scheduleIndex === -1) {
|
|
throw new Error(`Schedule ${scheduleId} does not exist`);
|
|
}
|
|
|
|
schedules.splice(scheduleIndex, 1);
|
|
await UserScheduleStore.set('schedules', schedules);
|
|
|
|
// By invariant, there must always be at least one schedule
|
|
if (schedules.length === 0) {
|
|
createSchedule('Schedule 1');
|
|
}
|
|
|
|
let newActiveIndex = activeIndex;
|
|
if (scheduleIndex < activeIndex) {
|
|
newActiveIndex = activeIndex - 1;
|
|
} else if (activeIndex >= schedules.length) {
|
|
newActiveIndex = schedules.length - 1;
|
|
}
|
|
await UserScheduleStore.set('activeIndex', newActiveIndex);
|
|
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* Deletes all schedules.
|
|
*
|
|
* @returns A promise that resolves when all schedules are deleted
|
|
*/
|
|
export async function deleteAllSchedules(): Promise<void> {
|
|
await UserScheduleStore.set('schedules', []);
|
|
await UserScheduleStore.set('activeIndex', 0);
|
|
await createSchedule('Schedule 1');
|
|
}
|