Skip to content

Commit

Permalink
fix: 4th attempt for: now able to delete schedule even if active (#435)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Wizardbacon13 and doprz authored Jan 2, 2025
1 parent e61ab56 commit 2425679
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 56 deletions.
16 changes: 7 additions & 9 deletions src/pages/background/lib/deleteSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ export default async function deleteSchedule(scheduleId: string): Promise<string
if (scheduleIndex === -1) {
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);
await UserScheduleStore.set('schedules', schedules);

if (activeIndex >= schedules.length) {
await UserScheduleStore.set('activeIndex', schedules.length - 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;
}

Expand Down
74 changes: 27 additions & 47 deletions src/views/components/common/ScheduleListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,54 +61,34 @@ export default function ScheduleListItem({ schedule, dragHandleProps, onClick }:
};

const handleDelete = () => {
if (schedule.id === activeSchedule.id) {
showDialog({
title: `Unable to delete active schedule.`,

description: (
<>
<Text>Deleting the active 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 => (
<Button variant='filled' color='ut-burntorange' onClick={close}>
I Understand
showDialog({
title: `Are you sure?`,
description: (
<>
<Text>Deleting</Text>
<Text className='text-ut-burntorange'> {schedule.name} </Text>
<Text>is permanent and will remove all added courses from that schedule.</Text>
</>
),
// eslint-disable-next-line react/no-unstable-nested-components
buttons: close => (
<>
<Button variant='single' color='ut-black' onClick={close}>
Cancel
</Button>
),
});
} else {
showDialog({
title: `Are you sure?`,
description: (
<>
<Text>Deleting</Text>
<Text className='text-ut-burntorange'> {schedule.name} </Text>
<Text>is permanent and will remove all added courses from that schedule.</Text>
</>
),
// 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>
</>
),
});
}
<Button
variant='filled'
color='theme-red'
onClick={() => {
close();
deleteSchedule(schedule.id);
}}
>
Delete Permanently
</Button>
</>
),
});
};

return (
Expand Down

0 comments on commit 2425679

Please sign in to comment.