feat: select default Unit for Currency field (#2996)

Closes #2347

Co-authored-by: Thais GUIGON <thaisguigon@macbook-pro.home>
This commit is contained in:
Thaïs
2023-12-15 11:01:06 +01:00
committed by GitHub
parent 5f7442cf23
commit 1eb5bebaf7
15 changed files with 190 additions and 18 deletions

View File

@ -1,6 +1,7 @@
import { useContext } from 'react';
import { useRecoilState } from 'recoil';
import { CurrencyCode } from '@/object-record/field/types/CurrencyCode';
import { FieldInitialValue } from '@/object-record/field/types/FieldInitialValue';
import { canBeCastAsIntegerOrNull } from '~/utils/cast-as-integer-or-null';
import {
@ -22,17 +23,17 @@ const initializeValue = (
fieldValue: FieldCurrencyValue,
) => {
if (fieldInitialValue?.isEmpty) {
return { amount: null, currencyCode: 'USD' };
return { amount: null, currencyCode: CurrencyCode.USD };
}
if (!isNaN(Number(fieldInitialValue?.value))) {
return {
amount: Number(fieldInitialValue?.value),
currencyCode: 'USD',
currencyCode: CurrencyCode.USD,
};
}
if (!fieldValue) {
return { amount: null, currencyCode: 'USD' };
return { amount: null, currencyCode: CurrencyCode.USD };
}
return {
@ -73,7 +74,7 @@ export const useCurrencyField = () => {
amountMicros: isNaN(amount)
? null
: convertCurrencyToCurrencyMicros(amount),
currencyCode: currencyCode,
currencyCode,
};
if (!isFieldCurrencyValue(newCurrencyValue)) {

View File

@ -0,0 +1,10 @@
export enum CurrencyCode {
CAD = 'CAD',
CHF = 'CHF',
CNY = 'CNY',
EUR = 'EUR',
GBP = 'GBP',
HKD = 'HKD',
JPY = 'JPY',
USD = 'USD',
}

View File

@ -1,6 +1,8 @@
import { EntityForSelect } from '@/object-record/relation-picker/types/EntityForSelect';
import { ThemeColor } from '@/ui/theme/constants/colors';
import { CurrencyCode } from './CurrencyCode';
export type FieldUuidMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
@ -111,7 +113,7 @@ export type FieldPhoneValue = string;
export type FieldEmailValue = string;
export type FieldLinkValue = { url: string; label: string };
export type FieldCurrencyValue = {
currencyCode: string;
currencyCode: CurrencyCode;
amountMicros: number | null;
};
export type FieldFullNameValue = { firstName: string; lastName: string };

View File

@ -1,9 +1,10 @@
import { z } from 'zod';
import { CurrencyCode } from '../CurrencyCode';
import { FieldCurrencyValue } from '../FieldMetadata';
const currencySchema = z.object({
currencyCode: z.string().nullable(),
currencyCode: z.nativeEnum(CurrencyCode).nullable(),
amountMicros: z.number().nullable(),
});