Skip to content

Commit

Permalink
Fixed default currency code in currency field (twentyhq#5028)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lucasbordeau authored Apr 18, 2024
1 parent 7065495 commit bc5cfd0
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isNonEmptyString } from '@sniptt/guards';

import { CurrencyCode } from '@/object-record/record-field/types/CurrencyCode';
import { FieldCurrencyValue } from '@/object-record/record-field/types/FieldMetadata';
import { CurrencyInput } from '@/ui/field/input/components/CurrencyInput';
Expand Down Expand Up @@ -30,13 +32,24 @@ export const CurrencyFieldInput = ({
defaultValue,
} = useCurrencyField();

const currencyCode =
draftValue?.currencyCode ??
((defaultValue as FieldCurrencyValue).currencyCode.replace(
/'/g,
'',
) as CurrencyCode) ??
CurrencyCode.USD;
const defaultCurrencyCodeWithoutSQLQuotes = (
defaultValue as FieldCurrencyValue
).currencyCode.replace(/'/g, '') as CurrencyCode;

const defaultCurrencyCodeIsNotEmpty = isNonEmptyString(
defaultCurrencyCodeWithoutSQLQuotes,
);

const draftCurrencyCode = draftValue?.currencyCode;

const draftCurrencyCodeIsEmptyIsNotEmpty =
isNonEmptyString(draftCurrencyCode);

const currencyCode = draftCurrencyCodeIsEmptyIsNotEmpty
? draftCurrencyCode
: defaultCurrencyCodeIsNotEmpty
? defaultCurrencyCodeWithoutSQLQuotes
: CurrencyCode.USD;

const handleEnter = (newValue: string) => {
onEnter?.(() => {
Expand Down

0 comments on commit bc5cfd0

Please sign in to comment.