Skip to content

Commit

Permalink
feat(mobile): add convertToDefaultCurrency to amount format for multi…
Browse files Browse the repository at this point in the history
… currency conversion
  • Loading branch information
bkdev98 committed Sep 2, 2024
1 parent f52bb9b commit 79d240d
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions apps/mobile/components/common/amount-format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type AmountFormatProps = {
displayNegativeSign?: boolean
displayPositiveSign?: boolean
displayPositiveColor?: boolean
convertToDefaultCurrency?: boolean
} & VariantProps<typeof amountVariants>

export function AmountFormat({
Expand All @@ -51,6 +52,7 @@ export function AmountFormat({
displayNegativeSign,
displayPositiveSign,
displayPositiveColor,
convertToDefaultCurrency,
}: AmountFormatProps) {
const defaultCurrency = useDefaultCurrency()

Expand All @@ -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 (
<Text
Expand All @@ -84,9 +94,11 @@ export function AmountFormat({
)}
>
{sign}
{Math.abs(roundedAmount).toLocaleString()}{' '}
{displayAmount}{' '}
<Text className={cn(currencyVariants({ size }))}>
{currency || defaultCurrency}
{convertToDefaultCurrency
? defaultCurrency
: currency || defaultCurrency}
</Text>
</Text>
)
Expand Down

0 comments on commit 79d240d

Please sign in to comment.