From 79d240d854deff1dd63008a40c9112c389b07219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Qu=E1=BB=91c=20Kh=C3=A1nh?= Date: Wed, 14 Aug 2024 17:30:07 +0700 Subject: [PATCH] feat(mobile): add convertToDefaultCurrency to amount format for multi currency conversion --- .../components/common/amount-format.tsx | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/apps/mobile/components/common/amount-format.tsx b/apps/mobile/components/common/amount-format.tsx index 802b9c81..0d60298d 100644 --- a/apps/mobile/components/common/amount-format.tsx +++ b/apps/mobile/components/common/amount-format.tsx @@ -41,6 +41,7 @@ type AmountFormatProps = { displayNegativeSign?: boolean displayPositiveSign?: boolean displayPositiveColor?: boolean + convertToDefaultCurrency?: boolean } & VariantProps export function AmountFormat({ @@ -51,6 +52,7 @@ export function AmountFormat({ displayNegativeSign, displayPositiveSign, displayPositiveColor, + convertToDefaultCurrency, }: AmountFormatProps) { const defaultCurrency = useDefaultCurrency() @@ -64,12 +66,20 @@ export function AmountFormat({ return '' }, [amount, displayNegativeSign, displayPositiveSign]) - const roundedAmount = useMemo(() => { - if (SHOULD_ROUND_VALUE_CURRENCIES.includes(currency || defaultCurrency)) { - return Math.round(amount) + const displayAmount = useMemo(() => { + const roundedAmount = SHOULD_ROUND_VALUE_CURRENCIES.includes( + currency || defaultCurrency, + ) + ? Math.round(amount) + : amount + + if (!convertToDefaultCurrency) { + return Math.abs(roundedAmount).toLocaleString() } - return amount - }, [amount, currency, defaultCurrency]) + + // TODO: correct amount with currency exchange rate + return Math.abs(roundedAmount).toLocaleString() + }, [amount, convertToDefaultCurrency, currency, defaultCurrency]) return ( {sign} - {Math.abs(roundedAmount).toLocaleString()}{' '} + {displayAmount}{' '} - {currency || defaultCurrency} + {convertToDefaultCurrency + ? defaultCurrency + : currency || defaultCurrency} )