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

Duplicate calendar #1048

Merged
merged 12 commits into from
Nov 26, 2024
Merged
7 changes: 6 additions & 1 deletion apps/antalmanac/src/components/dialogs/CopySchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { SelectChangeEvent } from '@mui/material';
import { useState, useEffect, useCallback } from 'react';

import { copySchedule } from '$actions/AppStoreActions';
import { copySchedule, addSchedule } from '$actions/AppStoreActions';
import AppStore from '$stores/AppStore';

interface CopyScheduleDialogProps extends DialogProps {
Expand All @@ -35,6 +35,10 @@ function CopyScheduleDialog(props: CopyScheduleDialogProps) {

const handleCopy = useCallback(() => {
if (selectedSchedule !== scheduleNames.length) {
if (selectedSchedule === scheduleNames.length + 1) {
addSchedule(AppStore.getNextScheduleName('Copy of ' + scheduleNames[index]));
AppStore.changeCurrentSchedule(index);
}
copySchedule(selectedSchedule);
} else {
scheduleNames.forEach((_, scheduleIndex) => {
Expand Down Expand Up @@ -70,6 +74,7 @@ function CopyScheduleDialog(props: CopyScheduleDialogProps) {
{name}
</MenuItem>
))}
<MenuItem value={scheduleNames.length + 1}>Copy to New Schedule</MenuItem>
jotalis marked this conversation as resolved.
Show resolved Hide resolved
<MenuItem value={scheduleNames.length}>Copy to All Schedules</MenuItem>
</Select>
</Box>
Expand Down
4 changes: 4 additions & 0 deletions apps/antalmanac/src/stores/AppStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class AppStore extends EventEmitter {
}
}

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

getDefaultScheduleName() {
return this.schedule.getDefaultScheduleName();
}
Expand Down
12 changes: 12 additions & 0 deletions apps/antalmanac/src/stores/Schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ export class Schedules {
this.skeletonSchedules = [];
}

getNextScheduleName(newScheduleName: string) {
const scheduleNames = this.getScheduleNames();
let nextScheduleName = newScheduleName;
let counter = 1;

while (scheduleNames.includes(nextScheduleName)) {
nextScheduleName = `${newScheduleName}(${counter++})`;
}

return nextScheduleName;
}

getDefaultScheduleName() {
const termName = termData[0].shortName.replaceAll(' ', '-');
const countSameScheduleNames = this.getScheduleNames().filter((name) => name.includes(termName)).length;
Expand Down
Loading