From 0eba229d9e44e515f989a6effc350e0f869b0a57 Mon Sep 17 00:00:00 2001 From: Tyler Marshall Date: Fri, 17 May 2024 17:05:02 -0700 Subject: [PATCH] fix: removes ability to have duplicate account names on front-end ref: https://github.com/actualbudget/actual/issues/316 --- .../modals/CreateLocalAccountModal.tsx | 23 +++++++++++++++---- upcoming-release-notes/2776.md | 6 +++++ 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 upcoming-release-notes/2776.md diff --git a/packages/desktop-client/src/components/modals/CreateLocalAccountModal.tsx b/packages/desktop-client/src/components/modals/CreateLocalAccountModal.tsx index 2783acf2c88..bee274a6077 100644 --- a/packages/desktop-client/src/components/modals/CreateLocalAccountModal.tsx +++ b/packages/desktop-client/src/components/modals/CreateLocalAccountModal.tsx @@ -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'; @@ -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 ( @@ -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); @@ -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 }} /> - {nameError && ( - Name is required + {nameError.error && ( + + {nameError.message} + )}