From 2d4c5e0966164764be5660974b376f51599e9e34 Mon Sep 17 00:00:00 2001 From: vicksey Date: Thu, 21 Nov 2024 17:21:35 -0800 Subject: [PATCH 1/2] Ensure unique schedule names when adding and renaming. --- .../src/components/dialogs/AddSchedule.tsx | 17 +++++++++++++++- .../src/components/dialogs/RenameSchedule.tsx | 20 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/apps/antalmanac/src/components/dialogs/AddSchedule.tsx b/apps/antalmanac/src/components/dialogs/AddSchedule.tsx index 3bf91f87a..7ee0fdbc2 100644 --- a/apps/antalmanac/src/components/dialogs/AddSchedule.tsx +++ b/apps/antalmanac/src/components/dialogs/AddSchedule.tsx @@ -13,6 +13,7 @@ function AddScheduleDialog({ onClose, onKeyDown, ...props }: DialogProps) { const isDark = useThemeStore((store) => store.isDark); const [name, setName] = useState(AppStore.getDefaultScheduleName()); + const [errorMessage, setErrorMessage] = useState(''); const handleCancel = () => { onClose?.({}, 'escapeKeyDown'); @@ -20,11 +21,18 @@ function AddScheduleDialog({ onClose, onKeyDown, ...props }: DialogProps) { const handleNameChange = (event: React.ChangeEvent) => { setName(event.target.value); + setErrorMessage(''); }; const submitName = () => { + const existingNames = AppStore.schedule.getScheduleNames(); + if (existingNames.includes(name.trim())) { + setErrorMessage('Schedule name already exists'); + return; + } addSchedule(name); setName(AppStore.schedule.getDefaultScheduleName()); + setErrorMessage(''); onClose?.({}, 'escapeKeyDown'); }; @@ -52,7 +60,14 @@ function AddScheduleDialog({ onClose, onKeyDown, ...props }: DialogProps) { - + diff --git a/apps/antalmanac/src/components/dialogs/RenameSchedule.tsx b/apps/antalmanac/src/components/dialogs/RenameSchedule.tsx index 0d3611a2a..04c09f456 100644 --- a/apps/antalmanac/src/components/dialogs/RenameSchedule.tsx +++ b/apps/antalmanac/src/components/dialogs/RenameSchedule.tsx @@ -39,20 +39,29 @@ function RenameScheduleDialog(props: ScheduleNameDialogProps) { const [name, setName] = useState(scheduleNames[index]); + const [errorMessage, setErrorMessage] = useState(''); + const disabled = useMemo(() => { return name?.trim() === ''; - }, [name]); + }, [name, errorMessage]); const handleCancel = useCallback(() => { onClose?.({}, 'escapeKeyDown'); setName(scheduleNames[index]); + setErrorMessage(''); }, [onClose, scheduleNames, index]); const handleNameChange = useCallback((event: React.ChangeEvent) => { setName(event.target.value); + setErrorMessage(''); }, []); const submitName = useCallback(() => { + const trimmedName = name.trim(); + if (scheduleNames.includes(trimmedName) && scheduleNames[index] !== trimmedName) { + setErrorMessage('Schedule name already exists'); + return; + } renameSchedule(name, index); onClose?.({}, 'escapeKeyDown'); }, [onClose, name, index]); @@ -92,7 +101,14 @@ function RenameScheduleDialog(props: ScheduleNameDialogProps) { - + From e8d7ff1cecd03daee57f6684bc8c44bc6972cb05 Mon Sep 17 00:00:00 2001 From: vicksey Date: Wed, 8 Jan 2025 09:42:28 -0800 Subject: [PATCH 2/2] Schedule name field moved for reusability. --- .../src/components/dialogs/AddSchedule.tsx | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/apps/antalmanac/src/components/dialogs/AddSchedule.tsx b/apps/antalmanac/src/components/dialogs/AddSchedule.tsx index 7ee0fdbc2..5993984a5 100644 --- a/apps/antalmanac/src/components/dialogs/AddSchedule.tsx +++ b/apps/antalmanac/src/components/dialogs/AddSchedule.tsx @@ -6,6 +6,30 @@ import { addSchedule } from '$actions/AppStoreActions'; import AppStore from '$stores/AppStore'; import { useThemeStore } from '$stores/SettingsStore'; +/** + * Reusable component for the Schedule Name Field. + */ +function ScheduleNameField({ + name, + errorMessage, + onNameChange, +}: { + name: string; + errorMessage: string; + onNameChange: (event: React.ChangeEvent) => void; +}) { + return ( + + ); +} + /** * Dialog with a text field to add a schedule. */ @@ -60,13 +84,10 @@ function AddScheduleDialog({ onClose, onKeyDown, ...props }: DialogProps) { -