Skip to content

Commit

Permalink
feat: add reordering as an action stored in local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jotalis committed Dec 29, 2024
1 parent 378acd8 commit fd4ef8e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
13 changes: 12 additions & 1 deletion apps/antalmanac/src/actions/ActionTypesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ export interface ClearScheduleAction {

export interface CopyScheduleAction {
type: 'copySchedule';
index: number;
newScheduleName: string;
}

export interface ReorderScheduleAction {
type: 'reorderSchedule';
from: number;
to: number;
}

export interface ChangeCourseColorAction {
type: 'changeCourseColor';
sectionCode: string;
Expand All @@ -78,6 +85,7 @@ export type ActionType =
| ChangeCustomEventColorAction
| ClearScheduleAction
| CopyScheduleAction
| ReorderScheduleAction
| ChangeCourseColorAction
| UndoAction;

Expand Down Expand Up @@ -159,7 +167,10 @@ class ActionTypesStore extends EventEmitter {
AppStore.schedule.clearCurrentSchedule();
break;
case 'copySchedule':
AppStore.schedule.copySchedule(action.newScheduleName);
AppStore.schedule.copySchedule(action.index, action.newScheduleName);
break;
case 'reorderSchedule':
AppStore.schedule.reorderSchedule(action.from, action.to);
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ export function SelectSchedulePopover() {
};
}, []);

const scheduleMappingToUse = useMemo(
() => (skeletonMode ? skeletonScheduleMapping : scheduleMapping),
[skeletonMode, skeletonScheduleMapping, scheduleMapping]
);
const scheduleMappingToUse = skeletonMode ? skeletonScheduleMapping : scheduleMapping;

return (
<Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function SortableList<T extends BaseItem>({ items, onChange, renderItem }
const activeIndex = items.findIndex(({ id }) => id === active.id);
const overIndex = items.findIndex(({ id }) => id === over.id);
onChange(arrayMove(items, activeIndex, overIndex));
AppStore.reorderSchedules(activeIndex, overIndex);
AppStore.reorderSchedule(activeIndex, overIndex);
}
setActive(null);
}}
Expand Down
13 changes: 11 additions & 2 deletions apps/antalmanac/src/stores/AppStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
ChangeCustomEventColorAction,
ClearScheduleAction,
CopyScheduleAction,
ReorderScheduleAction,
ChangeCourseColorAction,
UndoAction,
} from '$actions/ActionTypesStore';
Expand Down Expand Up @@ -300,6 +301,7 @@ class AppStore extends EventEmitter {
this.unsavedChanges = true;
const action: CopyScheduleAction = {
type: 'copySchedule',
index: index,
newScheduleName: newScheduleName,
};
actionTypesStore.autoSaveSchedule(action);
Expand All @@ -310,8 +312,15 @@ class AppStore extends EventEmitter {
this.emit('customEventsChange');
}

reorderSchedules(from: number, to: number) {
this.schedule.reorderSchedules(from, to);
reorderSchedule(from: number, to: number) {
this.schedule.reorderSchedule(from, to);
this.unsavedChanges = true;
const action: ReorderScheduleAction = {
type: 'reorderSchedule',
from: from,
to: to,
};
actionTypesStore.autoSaveSchedule(action);
this.emit('currentScheduleIndexChange');
this.emit('scheduleNamesChange', { triggeredBy: 'reorder' });
this.emit('reorderSchedule');
Expand Down
2 changes: 1 addition & 1 deletion apps/antalmanac/src/stores/Schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class Schedules {
* Reorder schedules by moving a schedule from one index to another.
* This modifies the order of schedules and updates the current schedule index to maintain the correct reference.
*/
reorderSchedules(from: number, to: number) {
reorderSchedule(from: number, to: number) {
this.addUndoState();
const [removed] = this.schedules.splice(from, 1);
this.schedules.splice(to, 0, removed);
Expand Down

0 comments on commit fd4ef8e

Please sign in to comment.