fix: 4th attempt for: now able to delete schedule even if active (#435)

* fix: 2nd attempt for: now able to delete schedule even if active

* fix: 3rd attempt for: now able to delete schedule even if active

* fix: 4th attempt for: now able to delete schedule even if active

---------

Co-authored-by: doprz <52579214+doprz@users.noreply.github.com>
This commit is contained in:
Long Phan
2025-01-02 12:31:41 -06:00
committed by GitHub
parent e61ab565c3
commit 24256798ba
2 changed files with 34 additions and 56 deletions

View File

@@ -18,20 +18,18 @@ export default async function deleteSchedule(scheduleId: string): Promise<string
if (scheduleIndex === -1) { if (scheduleIndex === -1) {
throw new Error(`Schedule ${scheduleId} does not exist`); throw new Error(`Schedule ${scheduleId} does not exist`);
} }
if (scheduleIndex === activeIndex) {
throw new Error(`Cannot delete active schedule`);
}
if (scheduleIndex < activeIndex) {
await UserScheduleStore.set('activeIndex', activeIndex - 1);
}
schedules.splice(scheduleIndex, 1); schedules.splice(scheduleIndex, 1);
await UserScheduleStore.set('schedules', schedules); await UserScheduleStore.set('schedules', schedules);
if (activeIndex >= schedules.length) { let newActiveIndex = activeIndex;
await UserScheduleStore.set('activeIndex', schedules.length - 1); if (scheduleIndex < activeIndex) {
newActiveIndex = activeIndex - 1;
} else if (activeIndex >= schedules.length) {
newActiveIndex = schedules.length - 1;
} }
await UserScheduleStore.set('activeIndex', newActiveIndex);
return undefined; return undefined;
} }

View File

@@ -61,54 +61,34 @@ export default function ScheduleListItem({ schedule, dragHandleProps, onClick }:
}; };
const handleDelete = () => { const handleDelete = () => {
if (schedule.id === activeSchedule.id) { showDialog({
showDialog({ title: `Are you sure?`,
title: `Unable to delete active schedule.`, description: (
<>
description: ( <Text>Deleting</Text>
<> <Text className='text-ut-burntorange'> {schedule.name} </Text>
<Text>Deleting the active schedule</Text> <Text>is permanent and will remove all added courses from that schedule.</Text>
<Text className='text-ut-burntorange'> {schedule.name} </Text> </>
<Text>is not allowed. Please switch to another schedule and try again.</Text> ),
</> // eslint-disable-next-line react/no-unstable-nested-components
), buttons: close => (
// eslint-disable-next-line react/no-unstable-nested-components <>
buttons: close => ( <Button variant='single' color='ut-black' onClick={close}>
<Button variant='filled' color='ut-burntorange' onClick={close}> Cancel
I Understand
</Button> </Button>
), <Button
}); variant='filled'
} else { color='theme-red'
showDialog({ onClick={() => {
title: `Are you sure?`, close();
description: ( deleteSchedule(schedule.id);
<> }}
<Text>Deleting</Text> >
<Text className='text-ut-burntorange'> {schedule.name} </Text> Delete Permanently
<Text>is permanent and will remove all added courses from that schedule.</Text> </Button>
</> </>
), ),
// eslint-disable-next-line react/no-unstable-nested-components });
buttons: close => (
<>
<Button variant='single' color='ut-black' onClick={close}>
Cancel
</Button>
<Button
variant='filled'
color='theme-red'
onClick={() => {
close();
deleteSchedule(schedule.id);
}}
>
Delete Permanently
</Button>
</>
),
});
}
}; };
return ( return (