Fixed default currency code in currency field (#5028)

Default currency logic was not handling a specific case where the
default currency is empty in the field metadata.

I fixed the ternary cascade and made it more explicit, thus also
avoiding falling into having an empty currency code being persisted.
This commit is contained in:
Lucas Bordeau
2024-04-18 11:25:17 +02:00
committed by GitHub
parent 7065495223
commit bc5cfd046c

View File

@ -1,3 +1,5 @@
import { isNonEmptyString } from '@sniptt/guards';
import { CurrencyCode } from '@/object-record/record-field/types/CurrencyCode'; import { CurrencyCode } from '@/object-record/record-field/types/CurrencyCode';
import { FieldCurrencyValue } from '@/object-record/record-field/types/FieldMetadata'; import { FieldCurrencyValue } from '@/object-record/record-field/types/FieldMetadata';
import { CurrencyInput } from '@/ui/field/input/components/CurrencyInput'; import { CurrencyInput } from '@/ui/field/input/components/CurrencyInput';
@ -30,13 +32,24 @@ export const CurrencyFieldInput = ({
defaultValue, defaultValue,
} = useCurrencyField(); } = useCurrencyField();
const currencyCode = const defaultCurrencyCodeWithoutSQLQuotes = (
draftValue?.currencyCode ?? defaultValue as FieldCurrencyValue
((defaultValue as FieldCurrencyValue).currencyCode.replace( ).currencyCode.replace(/'/g, '') as CurrencyCode;
/'/g,
'', const defaultCurrencyCodeIsNotEmpty = isNonEmptyString(
) as CurrencyCode) ?? defaultCurrencyCodeWithoutSQLQuotes,
CurrencyCode.USD; );
const draftCurrencyCode = draftValue?.currencyCode;
const draftCurrencyCodeIsEmptyIsNotEmpty =
isNonEmptyString(draftCurrencyCode);
const currencyCode = draftCurrencyCodeIsEmptyIsNotEmpty
? draftCurrencyCode
: defaultCurrencyCodeIsNotEmpty
? defaultCurrencyCodeWithoutSQLQuotes
: CurrencyCode.USD;
const handleEnter = (newValue: string) => { const handleEnter = (newValue: string) => {
onEnter?.(() => { onEnter?.(() => {