Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mobile): add convertToDefaultCurrency to amount format for multi currency conversion #258

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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