Skip to content

Commit

Permalink
presets and sets: trim names
Browse files Browse the repository at this point in the history
  • Loading branch information
fde31 committed Jan 8, 2025
1 parent 0ea5d8c commit 28ab953
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
11 changes: 6 additions & 5 deletions src/components/presets/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ export const PresetItem: FunctionComponent<PresetItemProps> = memo(function Wrap
const onRenamePreset = useCallback((e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
inputRef.current?.focus();
if (preset.name === name) {
const trimmedName = name.trim();
if (preset.name === trimmedName) {
setIsEditing(false);
} else if (!name?.length) {
} else if (!trimmedName?.length) {
setError("Please provide a valid preset name");
} else if (!validateUniqueName(name)) {
setError(`A preset with the name "${name}" already exists`);
} else if (!validateUniqueName(trimmedName)) {
setError(`A preset with the name "${trimmedName}" already exists`);
} else {
onRename(preset, name);
onRename(preset, trimmedName);
}
}, [name, onRename, preset, setError, inputRef, setIsEditing, validateUniqueName]);

Expand Down
5 changes: 3 additions & 2 deletions src/components/presets/save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ export const SavePresetForm: FunctionComponent<SavePresetFormProps> = memo(funct

const onSavePreset = (e: FormEvent<HTMLFormElement>): void => {
e.preventDefault();
if (!name?.length) {
const trimmedName = name.trim();
if (!trimmedName?.length) {
setError("Please provide a valid preset name");
} else {
setError(undefined);
onSave(name);
onSave(trimmedName);
setName("");
}
};
Expand Down
11 changes: 6 additions & 5 deletions src/components/sets/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ export const GraphSetItem: FunctionComponent<GraphSetItemProps> = memo(function
const onRenameSet = useCallback((e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
inputRef.current?.focus();
if (set.name === name) {
const trimmedName = name.trim();
if (set.name === trimmedName) {
setIsEditing(false);
} else if (!name?.length) {
} else if (!trimmedName?.length) {
setError("Please provide a valid set name");
} else if (!validateUniqueName(name)) {
setError((`A set with the name "${name} already exists"`));
} else if (!validateUniqueName(trimmedName)) {
setError((`A set with the name "${trimmedName} already exists"`));
} else {
onRename(set, name);
onRename(set, trimmedName);
}
}, [name, onRename, set, setError, inputRef, setIsEditing, validateUniqueName]);

Expand Down
5 changes: 3 additions & 2 deletions src/components/sets/save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ export const SaveGraphSetForm: FunctionComponent<SaveGraphSetFormProps> = memo(f

const onSaveSet = (e: FormEvent<HTMLFormElement>): void => {
e.preventDefault();
if (!name?.length) {
const trimmedName = name.trim();
if (!trimmedName?.length) {
setError("Please provide a valid set name");
} else {
setError(undefined);
onSave(name);
onSave(trimmedName);
setName("");
}
};
Expand Down

0 comments on commit 28ab953

Please sign in to comment.