Skip to content

Commit

Permalink
feat: Create Snackbar When Copying Schedules (#811)
Browse files Browse the repository at this point in the history
Co-authored-by: Aponia <[email protected]>
  • Loading branch information
KevinWu098 and ap0nia authored Dec 4, 2023
1 parent b928fb8 commit 75fd50f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
14 changes: 12 additions & 2 deletions apps/antalmanac/src/actions/AppStoreActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import AppStore from '$stores/AppStore';
import trpc from '$lib/api/trpc';
import { courseNumAsDecimal } from '$lib/analytics';

export interface CopyScheduleOptions {
onSuccess: (index: number) => unknown;
onError: (index: number) => unknown;
}

export const addCourse = (
section: WebsocSection,
courseDetails: CourseDetails,
Expand Down Expand Up @@ -181,13 +186,18 @@ export const changeCourseColor = (sectionCode: string, term: string, newColor: s
AppStore.changeCourseColor(sectionCode, term, newColor);
};

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

AppStore.copySchedule(to);
try {
AppStore.copySchedule(to);
options?.onSuccess(to);
} catch (error) {
options?.onError(to);
}
};

export const addSchedule = (scheduleName: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { Box, Chip, IconButton, Menu, MenuItem, Paper, SxProps, TextField, Toolt
import { ContentCopy, DeleteOutline } from '@mui/icons-material';
import { AACourse } from '@packages/antalmanac-types';

import { useSnackbar } from 'notistack';
import { ColumnToggleButton } from '../CoursePane/CoursePaneButtonRow';
import SectionTableLazyWrapper from '../SectionTable/SectionTableLazyWrapper';
import CustomEventDetailView from './CustomEventDetailView';
import AppStore from '$stores/AppStore';
import analyticsEnum, { logAnalytics } from '$lib/analytics';
import { clearSchedules, copySchedule, updateScheduleNote } from '$actions/AppStoreActions';
import { CopyScheduleOptions, clearSchedules, copySchedule, updateScheduleNote } from '$actions/AppStoreActions';
import { clickToCopy } from '$lib/helpers';

/**
Expand Down Expand Up @@ -88,9 +89,9 @@ function handleClear() {
}
}

function createCopyHandler(index: number) {
function createCopyHandler(index: number, options: CopyScheduleOptions) {
return () => {
copySchedule(index);
copySchedule(index, options);
};
}

Expand All @@ -106,6 +107,20 @@ function ClearScheduleButton() {

function CopyScheduleButton() {
const [scheduleNames, setScheduleNames] = useState(AppStore.getScheduleNames());
const { enqueueSnackbar } = useSnackbar();

const options = useMemo(() => {
return {
onSuccess: (index: number) => {
const name = index === scheduleNames.length ? 'All Schedules' : scheduleNames[index];
enqueueSnackbar(`Schedule copied to ${name}.`, { variant: 'success' });
},
onError: (index: number) => {
const name = index === scheduleNames.length ? 'All Schedules' : scheduleNames[index];
enqueueSnackbar(`Could not copy schedule to ${name}.`, { variant: 'error' });
},
};
}, [enqueueSnackbar, scheduleNames]);

useEffect(() => {
/**
Expand Down Expand Up @@ -136,12 +151,14 @@ function CopyScheduleButton() {
<MenuItem
key={index}
disabled={AppStore.getCurrentScheduleIndex() === index}
onClick={createCopyHandler(index)}
onClick={createCopyHandler(index, options)}
>
Copy to {name}
</MenuItem>
))}
<MenuItem onClick={createCopyHandler(scheduleNames.length)}>Copy to All Schedules</MenuItem>
<MenuItem onClick={createCopyHandler(scheduleNames.length, options)}>
Copy to All Schedules
</MenuItem>
</Menu>
</>
)}
Expand Down

0 comments on commit 75fd50f

Please sign in to comment.