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 CallbackOptions {
KevinWu098 marked this conversation as resolved.
Show resolved Hide resolved
onSuccess: (name?: string) => unknown;
onError: (name?: string) => unknown;
KevinWu098 marked this conversation as resolved.
Show resolved Hide resolved
}

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 = (name: string, to: number, options?: CallbackOptions) => {
KevinWu098 marked this conversation as resolved.
Show resolved Hide resolved
logAnalytics({
category: analyticsEnum.addedClasses.title,
action: analyticsEnum.addedClasses.actions.COPY_SCHEDULE,
});

AppStore.copySchedule(to);
try {
AppStore.copySchedule(to);
options?.onSuccess(name);
} catch (error) {
options?.onError(name);
}
KevinWu098 marked this conversation as resolved.
Show resolved Hide resolved
};

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 { CallbackOptions, 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(name: string, index: number, options: CallbackOptions) {
return () => {
copySchedule(index);
copySchedule(name, index, options);
};
}

Expand All @@ -118,6 +119,14 @@ function ClearScheduleButton() {

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

const options = useMemo(() => {
return {
onSuccess: (name?: string) => enqueueSnackbar(`Schedule copied to ${name}.`, { variant: 'success' }),
onError: (name?: string) => enqueueSnackbar(`Could not copy schedule to ${name}.`, { variant: 'error' }),
KevinWu098 marked this conversation as resolved.
Show resolved Hide resolved
};
}, [enqueueSnackbar]);

useEffect(() => {
/**
Expand Down Expand Up @@ -148,12 +157,14 @@ function CopyScheduleButton() {
<MenuItem
key={index}
disabled={AppStore.getCurrentScheduleIndex() === index}
onClick={createCopyHandler(index)}
onClick={createCopyHandler(name, index, options)}
KevinWu098 marked this conversation as resolved.
Show resolved Hide resolved
>
Copy to {name}
</MenuItem>
))}
<MenuItem onClick={createCopyHandler(scheduleNames.length)}>Copy to All Schedules</MenuItem>
<MenuItem onClick={createCopyHandler('All Schedules', scheduleNames.length, options)}>
Copy to All Schedules
</MenuItem>
</Menu>
</>
)}
Expand Down
Loading