Skip to content

Commit

Permalink
Ensure unique schedule names when adding and renaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
vicksey committed Nov 22, 2024
1 parent 5ac6200 commit 2d4c5e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
17 changes: 16 additions & 1 deletion apps/antalmanac/src/components/dialogs/AddSchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ 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');
};

const handleNameChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
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');
};

Expand Down Expand Up @@ -52,7 +60,14 @@ function AddScheduleDialog({ onClose, onKeyDown, ...props }: DialogProps) {

<DialogContent>
<Box padding={1}>
<TextField fullWidth label="Name" onChange={handleNameChange} value={name} />
<TextField
fullWidth
label="Name"
onChange={handleNameChange}
value={name}
error={!!errorMessage}
helperText={errorMessage}
/>
</Box>
</DialogContent>

Expand Down
20 changes: 18 additions & 2 deletions apps/antalmanac/src/components/dialogs/RenameSchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLTextAreaElement>) => {
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]);
Expand Down Expand Up @@ -92,7 +101,14 @@ function RenameScheduleDialog(props: ScheduleNameDialogProps) {

<DialogContent>
<Box padding={1}>
<TextField fullWidth label="Name" onChange={handleNameChange} value={name} />
<TextField
fullWidth
label="Name"
onChange={handleNameChange}
value={name}
error={!!errorMessage}
helperText={errorMessage}
/>
</Box>
</DialogContent>

Expand Down

0 comments on commit 2d4c5e0

Please sign in to comment.