Skip to content

Commit c1b4b98

Browse files
authored
Merge pull request Expensify#89445 from shubham1206agra/refactor-convertToDisplayStringWithoutCurrency-1
2 parents db26f8e + 2c89967 commit c1b4b98

14 files changed

Lines changed: 255 additions & 205 deletions

File tree

src/components/CurrencyListContextProvider/default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const defaultCurrencyListActionsContextValue: CurrencyListActionsContextType = {
1010
getCurrencySymbol: () => undefined,
1111
getCurrencyDecimals: () => 2,
1212
convertToDisplayString: () => '',
13+
convertToDisplayStringWithoutCurrency: () => '',
1314
};
1415

1516
export {defaultCurrencyListStateContextValue, defaultCurrencyListActionsContextValue};

src/components/CurrencyListContextProvider/index.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, {createContext, useCallback, useContext, useMemo} from 'react';
22
import useLocalize from '@hooks/useLocalize';
33
import useOnyx from '@hooks/useOnyx';
44
import {convertToFrontendAmountAsInteger} from '@libs/CurrencyUtils';
5-
import {format} from '@libs/NumberFormatUtils';
5+
import {format, formatToParts} from '@libs/NumberFormatUtils';
66
import CONST from '@src/CONST';
77
import ONYXKEYS from '@src/ONYXKEYS';
88
import type {CurrencyList} from '@src/types/onyx';
@@ -54,6 +54,28 @@ function CurrencyListContextProvider({children}: React.PropsWithChildren) {
5454
[getCurrencyDecimals, preferredLocale],
5555
);
5656

57+
const convertToDisplayStringWithoutCurrency = useCallback(
58+
(amountInCents: number, currencyCode: string = CONST.CURRENCY.USD): string => {
59+
const decimals = getCurrencyDecimals(currencyCode);
60+
const convertedAmount = convertToFrontendAmountAsInteger(amountInCents, decimals);
61+
return formatToParts(preferredLocale, convertedAmount, {
62+
style: 'currency',
63+
currency: currencyCode,
64+
65+
// We are forcing the number of decimals because we override the default number of decimals in the backend for some currencies
66+
// See: https://github.com/Expensify/PHP-Libs/pull/834
67+
minimumFractionDigits: decimals,
68+
// For currencies that have decimal places > 2, floor to 2 instead as we don't support more than 2 decimal places.
69+
maximumFractionDigits: 2,
70+
})
71+
.filter((x) => x.type !== 'currency')
72+
.filter((x) => x.type !== 'literal' || x.value.trim().length !== 0)
73+
.map((x) => x.value)
74+
.join('');
75+
},
76+
[getCurrencyDecimals, preferredLocale],
77+
);
78+
5779
const stateValue = useMemo<CurrencyListStateContextType>(
5880
() => ({
5981
currencyList,
@@ -66,8 +88,9 @@ function CurrencyListContextProvider({children}: React.PropsWithChildren) {
6688
getCurrencySymbol,
6789
getCurrencyDecimals,
6890
convertToDisplayString,
91+
convertToDisplayStringWithoutCurrency,
6992
}),
70-
[getCurrencySymbol, getCurrencyDecimals, convertToDisplayString],
93+
[getCurrencySymbol, getCurrencyDecimals, convertToDisplayString, convertToDisplayStringWithoutCurrency],
7194
);
7295

7396
return (

src/components/CurrencyListContextProvider/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type CurrencyListActionsContextType = {
1414

1515
/** Function to convert amount in cents to display string based on the currency and locale */
1616
convertToDisplayString: (amount: number | undefined, currencyCode: string | undefined) => string;
17+
18+
/** Function to convert amount in cents to display string without currency symbol */
19+
convertToDisplayStringWithoutCurrency: (amount: number, currencyCode?: string) => string;
1720
};
1821

1922
export type {CurrencyListStateContextType, CurrencyListActionsContextType};

src/components/MoneyRequestConfirmationList/hooks/useSplitParticipants.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {useCurrencyListActions} from '@hooks/useCurrencyList';
88
import useLocalize from '@hooks/useLocalize';
99
import useThemeStyles from '@hooks/useThemeStyles';
1010
import {resetSplitShares, setIndividualShare} from '@libs/actions/IOU/Split';
11-
import {convertToBackendAmount, convertToDisplayStringWithoutCurrency} from '@libs/CurrencyUtils';
11+
import {convertToBackendAmount} from '@libs/CurrencyUtils';
1212
import {calculateAmount} from '@libs/IOUUtils';
1313
import {getIOUConfirmationOptionsFromPayeePersonalDetail} from '@libs/OptionsListUtils';
1414
import CONST from '@src/CONST';
@@ -54,7 +54,7 @@ type UseSplitParticipantsParams = {
5454
function useSplitParticipants({isTypeSplit, shouldShowReadOnlySplits, payeePersonalDetails, selectedParticipants, transaction, iouAmount, iouCurrencyCode}: UseSplitParticipantsParams) {
5555
const styles = useThemeStyles();
5656
const {translate} = useLocalize();
57-
const {convertToDisplayString, getCurrencySymbol} = useCurrencyListActions();
57+
const {convertToDisplayString, convertToDisplayStringWithoutCurrency, getCurrencySymbol} = useCurrencyListActions();
5858

5959
const transactionID = transaction?.transactionID;
6060
const onSplitShareChange = (accountID: number, value: number) => {

src/components/PerDiemEReceipt.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import useLocalize from '@hooks/useLocalize';
66
import useOnyx from '@hooks/useOnyx';
77
import useStyleUtils from '@hooks/useStyleUtils';
88
import useThemeStyles from '@hooks/useThemeStyles';
9-
import {convertAmountToDisplayString, convertToDisplayStringWithoutCurrency} from '@libs/CurrencyUtils';
9+
import {convertAmountToDisplayString} from '@libs/CurrencyUtils';
1010
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
1111
import {getTransactionDetails} from '@libs/ReportUtils';
1212
import variables from '@styles/variables';
@@ -53,7 +53,7 @@ function PerDiemEReceipt({transactionID}: PerDiemEReceiptProps) {
5353
const styles = useThemeStyles();
5454
const StyleUtils = useStyleUtils();
5555
const {translate} = useLocalize();
56-
const {getCurrencySymbol} = useCurrencyListActions();
56+
const {getCurrencySymbol, convertToDisplayStringWithoutCurrency} = useCurrencyListActions();
5757
const icons = useMemoizedLazyExpensifyIcons(['ExpensifyWordmark']);
5858
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transactionID)}`);
5959

0 commit comments

Comments
 (0)