Skip to content

Commit

Permalink
Merge branch 'main' into dnd-schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
jotalis committed Dec 30, 2024
2 parents f0c58e8 + 8b6db5d commit 536c935
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 27 deletions.
32 changes: 30 additions & 2 deletions apps/antalmanac/src/actions/ActionTypesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,25 @@ export interface ClearScheduleAction {
type: 'clearSchedule';
}

export interface AddScheduleAction {
type: 'addSchedule';
newScheduleName: string;
}

export interface RenameScheduleAction {
type: 'renameSchedule';
scheduleIndex: number;
newScheduleName: string;
}

export interface DeleteScheduleAction {
type: 'deleteSchedule';
scheduleIndex: number;
}

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

Expand All @@ -84,6 +100,9 @@ export type ActionType =
| EditCustomEventAction
| ChangeCustomEventColorAction
| ClearScheduleAction
| AddScheduleAction
| RenameScheduleAction
| DeleteScheduleAction
| CopyScheduleAction
| ReorderScheduleAction
| ChangeCourseColorAction
Expand Down Expand Up @@ -166,8 +185,17 @@ class ActionTypesStore extends EventEmitter {
case 'clearSchedule':
AppStore.schedule.clearCurrentSchedule();
break;
case 'addSchedule':
AppStore.schedule.addNewSchedule(action.newScheduleName);
break;
case 'renameSchedule':
AppStore.schedule.renameSchedule(action.scheduleIndex, action.newScheduleName);
break;
case 'copySchedule':
AppStore.schedule.copySchedule(action.index, action.newScheduleName);
AppStore.schedule.copySchedule(action.scheduleIndex, action.newScheduleName);
break;
case 'deleteSchedule':
AppStore.schedule.deleteSchedule(action.scheduleIndex);
break;
case 'reorderSchedule':
AppStore.schedule.reorderSchedule(action.from, action.to);
Expand Down
8 changes: 4 additions & 4 deletions apps/antalmanac/src/actions/AppStoreActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ export const changeCourseColor = (sectionCode: string, term: string, newColor: s
AppStore.changeCourseColor(sectionCode, term, newColor);
};

export const copySchedule = (index: number, newScheduleName: string, options?: CopyScheduleOptions) => {
export const copySchedule = (scheduleIndex: number, newScheduleName: string, options?: CopyScheduleOptions) => {
logAnalytics({
category: analyticsEnum.addedClasses.title,
action: analyticsEnum.addedClasses.actions.COPY_SCHEDULE,
});

try {
AppStore.copySchedule(index, newScheduleName);
AppStore.copySchedule(scheduleIndex, newScheduleName);
options?.onSuccess(newScheduleName);
} catch (error) {
options?.onError(newScheduleName);
Expand All @@ -268,8 +268,8 @@ export const addSchedule = (scheduleName: string) => {
AppStore.addSchedule(scheduleName);
};

export const renameSchedule = (scheduleName: string, scheduleIndex: number) => {
AppStore.renameSchedule(scheduleName, scheduleIndex);
export const renameSchedule = (scheduleIndex: number, scheduleName: string) => {
AppStore.renameSchedule(scheduleIndex, scheduleName);
};

export const deleteSchedule = (scheduleIndex: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const ScheduleNameDialog = forwardRef((props: ScheduleNameDialogProps, ref) => {
onClose?.();

if (rename) {
renameSchedule(scheduleName, scheduleRenameIndex as number); // typecast works b/c this function only runs when `const rename = scheduleRenameIndex !== undefined` is true.
renameSchedule(scheduleRenameIndex as number, scheduleName); // typecast works b/c this function only runs when `const rename = scheduleRenameIndex !== undefined` is true.
} else {
addSchedule(scheduleName);
}
Expand Down
6 changes: 3 additions & 3 deletions apps/antalmanac/src/components/buttons/Copy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContentCopy } from '@mui/icons-material';
import { Box, IconButton, SxProps, Tooltip } from '@mui/material';
import { IconButton, SxProps, Tooltip } from '@mui/material';
import { useCallback, useState } from 'react';

import CopyScheduleDialog from '$components/dialogs/CopySchedule';
Expand All @@ -22,7 +22,7 @@ export function CopyScheduleButton({ index, disabled, buttonSx }: CopyScheduleBu
}, []);

return (
<Box>
<>
<Tooltip title="Copy Schedule">
<span>
<IconButton sx={buttonSx} onClick={handleOpen} size="small" disabled={disabled}>
Expand All @@ -31,6 +31,6 @@ export function CopyScheduleButton({ index, disabled, buttonSx }: CopyScheduleBu
</span>
</Tooltip>
<CopyScheduleDialog fullWidth open={open} index={index} onClose={handleClose} />
</Box>
</>
);
}
4 changes: 2 additions & 2 deletions apps/antalmanac/src/components/dialogs/AddSchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function AddScheduleDialog({ onClose, onKeyDown, ...props }: DialogProps) {
const isDark = useThemeStore((store) => store.isDark);

const [name, setName] = useState(
AppStore.getNextScheduleName(AppStore.getDefaultScheduleName(), AppStore.getScheduleNames().length)
AppStore.getNextScheduleName(() => AppStore.getScheduleNames().length, AppStore.getDefaultScheduleName())
);

const handleCancel = () => {
Expand Down Expand Up @@ -48,7 +48,7 @@ function AddScheduleDialog({ onClose, onKeyDown, ...props }: DialogProps) {
};

const handleScheduleNamesChange = useCallback(() => {
setName(AppStore.getNextScheduleName(AppStore.getDefaultScheduleName(), AppStore.getScheduleNames().length));
setName(AppStore.getNextScheduleName(AppStore.getScheduleNames().length, AppStore.getDefaultScheduleName()));
}, []);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion apps/antalmanac/src/components/dialogs/RenameSchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function RenameScheduleDialog(props: ScheduleNameDialogProps) {
}, []);

const submitName = useCallback(() => {
renameSchedule(name, index);
renameSchedule(index, name);
onClose?.({}, 'escapeKeyDown');
}, [onClose, name, index]);

Expand Down
36 changes: 29 additions & 7 deletions apps/antalmanac/src/stores/AppStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import type {
ChangeCustomEventColorAction,
ClearScheduleAction,
CopyScheduleAction,
RenameScheduleAction,
DeleteScheduleAction,
ReorderScheduleAction,
ChangeCourseColorAction,
UndoAction,
AddScheduleAction,
} from '$actions/ActionTypesStore';
import { CalendarEvent, CourseEvent } from '$components/Calendar/CourseCalendarEvent';
import { SnackbarPosition } from '$components/NotificationSnackbar';
Expand Down Expand Up @@ -72,8 +75,8 @@ class AppStore extends EventEmitter {
}
}

getNextScheduleName(newScheduleName: string, scheduleIndex: number) {
return this.schedule.getNextScheduleName(newScheduleName, scheduleIndex);
getNextScheduleName(scheduleIndex: number, newScheduleName: string) {
return this.schedule.getNextScheduleName(scheduleIndex, newScheduleName);
}

getDefaultScheduleName() {
Expand Down Expand Up @@ -281,13 +284,26 @@ class AppStore extends EventEmitter {
// another key/value pair to keep track of the section codes for that schedule,
// and redirect the user to the new schedule
this.schedule.addNewSchedule(newScheduleName);
this.unsavedChanges = true;
const action: AddScheduleAction = {
type: 'addSchedule',
newScheduleName: newScheduleName,
};
actionTypesStore.autoSaveSchedule(action);
this.emit('scheduleNamesChange');
this.emit('currentScheduleIndexChange');
this.emit('scheduleNotesChange');
}

renameSchedule(scheduleName: string, scheduleIndex: number) {
this.schedule.renameSchedule(scheduleName, scheduleIndex);
renameSchedule(scheduleIndex: number, newScheduleName: string) {
this.schedule.renameSchedule(scheduleIndex, newScheduleName);
this.unsavedChanges = true;
const action: RenameScheduleAction = {
type: 'renameSchedule',
scheduleIndex: scheduleIndex,
newScheduleName: newScheduleName,
};
actionTypesStore.autoSaveSchedule(action);
this.emit('scheduleNamesChange');
}

Expand All @@ -296,12 +312,12 @@ class AppStore extends EventEmitter {
window.localStorage.removeItem('unsavedActions');
}

copySchedule(index: number, newScheduleName: string) {
this.schedule.copySchedule(index, newScheduleName);
copySchedule(scheduleIndex: number, newScheduleName: string) {
this.schedule.copySchedule(scheduleIndex, newScheduleName);
this.unsavedChanges = true;
const action: CopyScheduleAction = {
type: 'copySchedule',
index: index,
scheduleIndex: scheduleIndex,
newScheduleName: newScheduleName,
};
actionTypesStore.autoSaveSchedule(action);
Expand Down Expand Up @@ -380,6 +396,12 @@ class AppStore extends EventEmitter {

deleteSchedule(scheduleIndex: number) {
this.schedule.deleteSchedule(scheduleIndex);
this.unsavedChanges = true;
const action: DeleteScheduleAction = {
type: 'deleteSchedule',
scheduleIndex: scheduleIndex,
};
actionTypesStore.autoSaveSchedule(action);
this.emit('scheduleNamesChange');
this.emit('currentScheduleIndexChange');
this.emit('addedCoursesChange');
Expand Down
13 changes: 6 additions & 7 deletions apps/antalmanac/src/stores/Schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class Schedules {
this.skeletonSchedules = [];
}

getNextScheduleName(newScheduleName: string, scheduleIndex: number) {
getNextScheduleName(scheduleIndex: number, newScheduleName: string) {
const scheduleNames = this.getScheduleNames();
scheduleNames.splice(scheduleIndex, 1);
let nextScheduleName = newScheduleName;
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Schedules {
this.addUndoState();
const scheduleNoteId = Math.random();
this.schedules.push({
scheduleName: this.getNextScheduleName(newScheduleName, this.getNumberOfSchedules()),
scheduleName: this.getNextScheduleName(this.getNumberOfSchedules(), newScheduleName),
courses: [],
customEvents: [],
scheduleNoteId: scheduleNoteId,
Expand All @@ -122,9 +122,9 @@ export class Schedules {
* Rename schedule with the specified index.
* @param newScheduleName The name of the new schedule. If a schedule with the same name already exists, a number will be appended to the name.
*/
renameSchedule(newScheduleName: string, scheduleIndex: number) {
renameSchedule(scheduleIndex: number, newScheduleName: string) {
this.addUndoState();
this.schedules[scheduleIndex].scheduleName = this.getNextScheduleName(newScheduleName, scheduleIndex);
this.schedules[scheduleIndex].scheduleName = this.getNextScheduleName(scheduleIndex, newScheduleName);
}

/**
Expand All @@ -148,9 +148,9 @@ export class Schedules {
/**
* Copy the schedule at the provided index to a newly created schedule with the specified name.
*/
copySchedule(index: number, newScheduleName: string) {
copySchedule(scheduleIndex: number, newScheduleName: string) {
this.addNewSchedule(newScheduleName);
this.currentScheduleIndex = index; // temporarily set current schedule to the one being copied
this.currentScheduleIndex = scheduleIndex; // temporarily set current schedule to the one being copied
const to = this.getNumberOfSchedules() - 1;

for (const course of this.getCurrentCourses()) {
Expand Down Expand Up @@ -179,7 +179,6 @@ export class Schedules {
this.currentScheduleIndex += 1;
}
}

getCurrentCourses() {
return this.schedules[this.currentScheduleIndex]?.courses || [];
}
Expand Down

0 comments on commit 536c935

Please sign in to comment.