Skip to content

Commit

Permalink
Trim custom list names in gui
Browse files Browse the repository at this point in the history
  • Loading branch information
hulthe authored and raksooo committed Jul 4, 2024
1 parent 771e250 commit 4aed337
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
26 changes: 15 additions & 11 deletions gui/src/renderer/components/select-location/CustomListDialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,25 @@ export function EditListDialog(props: EditListProps) {
const { updateCustomList } = useAppContext();

const [newName, setNewName] = useState(props.list.name);
const newNameTrimmed = newName.trim();
const newNameValid = newNameTrimmed !== '';
const [error, setError, unsetError] = useBoolean();

// Update name in list and save it.
const save = useCallback(async () => {
try {
const updatedList = { ...props.list, name: newName };
const result = await updateCustomList(updatedList);
if (result && result.type === 'name already exists') {
setError();
} else {
props.hide();
if (newNameValid) {
try {
const updatedList = { ...props.list, name: newNameTrimmed };
const result = await updateCustomList(updatedList);
if (result && result.type === 'name already exists') {
setError();
} else {
props.hide();
}
} catch (e) {
const error = e as Error;
log.error(`Failed to edit custom list ${props.list.id}: ${error.message}`);
}
} catch (e) {
const error = e as Error;
log.error(`Failed to edit custom list ${props.list.id}: ${error.message}`);
}
}, [props.list, newName, props.hide]);

Expand All @@ -175,7 +179,7 @@ export function EditListDialog(props: EditListProps) {
<ModalAlert
isOpen={props.isOpen}
buttons={[
<AppButton.BlueButton key="save" onClick={save}>
<AppButton.BlueButton key="save" disabled={!newNameValid} onClick={save}>
{messages.gettext('Save')}
</AppButton.BlueButton>,
<AppButton.BlueButton key="cancel" onClick={props.hide}>
Expand Down
5 changes: 3 additions & 2 deletions gui/src/renderer/components/select-location/CustomLists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ interface AddListFormProps {

function AddListForm(props: AddListFormProps) {
const [name, setName] = useState('');
const nameValid = name.trim() !== '';
const nameTrimmed = name.trim();
const nameValid = nameTrimmed !== '';
const [error, setError, unsetError] = useBoolean();
const containerRef = useStyledRef<HTMLDivElement>();
const inputRef = useStyledRef<HTMLInputElement>();
Expand All @@ -137,7 +138,7 @@ function AddListForm(props: AddListFormProps) {
const createList = useCallback(async () => {
if (nameValid) {
try {
const result = await props.onCreateList(name);
const result = await props.onCreateList(nameTrimmed);
if (result) {
setError();
}
Expand Down

0 comments on commit 4aed337

Please sign in to comment.