Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create Snackbar When Copying Schedules #811

Merged
merged 12 commits into from
Dec 4, 2023
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 @@ -182,13 +187,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 toggleTheme = (radioGroupEvent: React.ChangeEvent<HTMLInputElement>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import {
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 @@ -100,9 +101,9 @@ function handleClear() {
}
}

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

Expand All @@ -118,6 +119,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 @@ -148,12 +163,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
Loading