2023-04-03 19:42:37 +02:00
|
|
|
import { INVISIBLE_CHARACTER } from '../../components/component-library/text/deprecated';
|
2023-03-06 18:48:28 +01:00
|
|
|
|
2022-12-22 18:27:31 +01:00
|
|
|
export function getAccountNameErrorMessage(
|
|
|
|
accounts,
|
|
|
|
context,
|
|
|
|
newAccountName,
|
|
|
|
defaultAccountName,
|
|
|
|
) {
|
|
|
|
const isDuplicateAccountName = accounts.some(
|
|
|
|
(item) => item.name === newAccountName,
|
|
|
|
);
|
|
|
|
|
|
|
|
const localizedWordForAccount = context
|
|
|
|
.t('newAccountNumberName')
|
|
|
|
.replace(' $1', '');
|
|
|
|
|
|
|
|
// Match strings starting with ${localizedWordForAccount} and then any numeral, case insensitive
|
|
|
|
// Trim spaces before and after
|
|
|
|
const reservedRegEx = new RegExp(
|
|
|
|
`^\\s*${localizedWordForAccount} \\d+\\s*$`,
|
|
|
|
'iu',
|
|
|
|
);
|
|
|
|
const isReservedAccountName = reservedRegEx.test(newAccountName);
|
|
|
|
|
|
|
|
const isValidAccountName =
|
|
|
|
newAccountName === defaultAccountName || // What is written in the text field is the same as the placeholder
|
|
|
|
(!isDuplicateAccountName && !isReservedAccountName);
|
|
|
|
|
|
|
|
let errorMessage;
|
|
|
|
if (isValidAccountName) {
|
2023-03-06 18:48:28 +01:00
|
|
|
errorMessage = INVISIBLE_CHARACTER; // Using an invisible character, so the spacing stays constant
|
2022-12-22 18:27:31 +01:00
|
|
|
} else if (isDuplicateAccountName) {
|
|
|
|
errorMessage = context.t('accountNameDuplicate');
|
|
|
|
} else if (isReservedAccountName) {
|
|
|
|
errorMessage = context.t('accountNameReserved');
|
|
|
|
}
|
|
|
|
|
|
|
|
return { isValidAccountName, errorMessage };
|
|
|
|
}
|