Skip to content

Commit

Permalink
fix: removes ability to have duplicate account names on front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigatok committed May 18, 2024
1 parent c311d4a commit 0eba229
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useState } from 'react';

import { toRelaxedNumber } from 'loot-core/src/shared/util';

import { useAccounts } from '../../hooks/useAccounts';
import { type BoundActions } from '../../hooks/useActions';
import { useNavigate } from '../../hooks/useNavigate';
import { theme } from '../../style';
Expand Down Expand Up @@ -32,9 +33,14 @@ export function CreateLocalAccountModal({
const [offbudget, setOffbudget] = useState(false);
const [balance, setBalance] = useState('0');

const [nameError, setNameError] = useState(false);
const [nameError, setNameError] = useState({
error: false,
message: 'Name is required',
});
const [balanceError, setBalanceError] = useState(false);

const accounts = useAccounts();

const validateBalance = balance => !isNaN(parseFloat(balance));

return (
Expand All @@ -49,7 +55,12 @@ export function CreateLocalAccountModal({
event.preventDefault();

const nameError = !name;
setNameError(nameError);
setNameError({ error: nameError, message: 'Name is required' });

if (accounts.some(account => account.name === name)) {
setNameError({ error: true, message: 'Name already exists' });
return;
}

const balanceError = !validateBalance(balance);
setBalanceError(balanceError);
Expand All @@ -75,15 +86,17 @@ export function CreateLocalAccountModal({
const name = event.target.value.trim();
setName(name);
if (name && nameError) {
setNameError(false);
setNameError({ error: false, message: '' });
}
}}
style={{ flex: 1 }}
/>
</InitialFocus>
</InlineField>
{nameError && (
<FormError style={{ marginLeft: 75 }}>Name is required</FormError>
{nameError.error && (
<FormError style={{ marginLeft: 75 }}>
{nameError.message}
</FormError>
)}

<View
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2776.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [Tigatok]
---

No longer allow duplicate names of accounts.

0 comments on commit 0eba229

Please sign in to comment.