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

Ensure unique schedule #1049

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Boolean instead of !!. That's less explicit and quite weird to read.

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
Loading