Skip to content

Commit

Permalink
feat: improvements to save state (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 authored Feb 5, 2025
1 parent 0bdad42 commit 2bf9f9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/antalmanac/src/stores/AppStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ class AppStore extends EventEmitter {
this.unsavedChanges = false;

await actionTypesStore.loadScheduleFromLocalSave();
this.schedule.clearPreviousStates();

this.emit('addedCoursesChange');
this.emit('customEventsChange');
Expand Down Expand Up @@ -430,6 +431,8 @@ class AppStore extends EventEmitter {

termsInSchedule = (term: string) =>
new Set([term, ...this.schedule.getCurrentCourses().map((course) => course.term)]);

getPreviousStates = () => this.schedule.getPreviousStates();
}

const store = new AppStore();
Expand Down
19 changes: 19 additions & 0 deletions apps/antalmanac/src/stores/Schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,20 @@ export class Schedules {
return calendarizeFinals(this.getCurrentCourses());
}

/**
* Getter for previous states
*/
getPreviousStates() {
return this.previousStates;
}

/**
* Clears previous states
*/
clearPreviousStates() {
this.previousStates = [];
}

/**
* Appends a copy of the current schedule to previous states to revert to
* Previous states are capped to 50
Expand All @@ -466,6 +480,11 @@ export class Schedules {
* All actions that call `addUndoState()` can be reverted.
*/
revertState() {
// prevent the user from undoing to an empty state
if (this.previousStates.length <= 1) {
return;
}

const state = this.previousStates.pop();
if (state !== undefined) {
this.schedules = state.schedules;
Expand Down

0 comments on commit 2bf9f9b

Please sign in to comment.