Skip to content
Merged
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions src/components/CurrencyListContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, {createContext, useCallback, useMemo} from 'react';
import useOnyx from '@hooks/useOnyx';
import ONYXKEYS from '@src/ONYXKEYS';
import type {CurrencyList} from '@src/types/onyx';
import {getEmptyObject} from '@src/types/utils/EmptyObject';

type CurrencyListContextProviderProps = {
/** Actual content wrapped by this component */
children: React.ReactNode;
};

type CurrencyListContextProps = {
/** The currency list from Onyx */
currencyList: CurrencyList;

/** Function to get currency symbol for a currency(ISO 4217) Code */
getCurrencySymbol: (currencyCode: string) => string | undefined;
};

const CurrencyListContext = createContext<CurrencyListContextProps>({
currencyList: getEmptyObject<CurrencyList>(),
getCurrencySymbol: () => undefined,
});

function CurrencyListContextProvider({children}: CurrencyListContextProviderProps) {
const [currencyList = getEmptyObject<CurrencyList>()] = useOnyx(ONYXKEYS.CURRENCY_LIST, {canBeMissing: true});

const getCurrencySymbol = useCallback(
(currencyCode: string): string | undefined => {
return currencyList[currencyCode]?.symbol;
Comment thread
shubham1206agra marked this conversation as resolved.
Resolved
},
[currencyList],
);

const contextValue = useMemo<CurrencyListContextProps>(
() => ({
currencyList,
getCurrencySymbol,
}),
[currencyList, getCurrencySymbol],
);

return <CurrencyListContext.Provider value={contextValue}>{children}</CurrencyListContext.Provider>;
}

export {CurrencyListContext, CurrencyListContextProvider};

export type {CurrencyListContextProps};
3 changes: 2 additions & 1 deletion src/components/CurrencyPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {ReactNode} from 'react';
import React, {Fragment, useState} from 'react';
import useCurrencyList from '@hooks/useCurrencyList';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import {getCurrencySymbol} from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import FullPageOfflineBlockingView from './BlockingViews/FullPageOfflineBlockingView';
Expand Down Expand Up @@ -41,6 +41,7 @@ type CurrencyPickerProps = {

function CurrencyPicker({label, value, errorText, headerContent, excludeCurrencies, disabled = false, shouldShowFullPageOfflineView = false, onInputChange = () => {}}: CurrencyPickerProps) {
const {translate} = useLocalize();
const {getCurrencySymbol} = useCurrencyList();
const [isPickerVisible, setIsPickerVisible] = useState(false);
const styles = useThemeStyles();

Expand Down
10 changes: 4 additions & 6 deletions src/components/CurrencySelectionList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import React, {useCallback, useMemo, useState} from 'react';
import SelectionList from '@components/SelectionListWithSections';
import RadioListItem from '@components/SelectionListWithSections/RadioListItem';
import SelectableListItem from '@components/SelectionListWithSections/SelectableListItem';
import useCurrencyList from '@hooks/useCurrencyList';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import {getCurrencySymbol} from '@libs/CurrencyUtils';
import getMatchScore from '@libs/getMatchScore';
import ONYXKEYS from '@src/ONYXKEYS';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {CurrencyListItem, CurrencySelectionListProps} from './types';

Expand All @@ -23,12 +21,12 @@ function CurrencySelectionList({
excludedCurrencies = [],
...restProps
}: CurrencySelectionListProps) {
const [currencyList] = useOnyx(ONYXKEYS.CURRENCY_LIST, {canBeMissing: false});
const {currencyList, getCurrencySymbol} = useCurrencyList();
const [searchValue, setSearchValue] = useState('');
const {translate} = useLocalize();
const getUnselectedOptions = useCallback((options: CurrencyListItem[]) => options.filter((option) => !option.isSelected), []);
const {sections, headerMessage} = useMemo(() => {
const currencyOptions: CurrencyListItem[] = Object.entries(currencyList ?? {}).reduce((acc, [currencyCode, currencyInfo]) => {
const currencyOptions: CurrencyListItem[] = Object.entries(currencyList).reduce((acc, [currencyCode, currencyInfo]) => {
const isSelectedCurrency = currencyCode === initiallySelectedCurrencyCode || selectedCurrencies.includes(currencyCode);
if (!excludedCurrencies.includes(currencyCode) && (isSelectedCurrency || !currencyInfo?.retired)) {
acc.push({
Expand Down Expand Up @@ -94,7 +92,7 @@ function CurrencySelectionList({
}

return {sections: result, headerMessage: isEmpty ? translate('common.noResultsFound') : ''};
}, [currencyList, recentlyUsedCurrencies, searchValue, getUnselectedOptions, translate, initiallySelectedCurrencyCode, selectedCurrencies, excludedCurrencies]);
}, [currencyList, recentlyUsedCurrencies, searchValue, getUnselectedOptions, translate, initiallySelectedCurrencyCode, selectedCurrencies, excludedCurrencies, getCurrencySymbol]);

return (
<SelectionList
Expand Down
3 changes: 2 additions & 1 deletion src/components/CurrencySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {useIsFocused} from '@react-navigation/native';
import type {ForwardedRef} from 'react';
import React, {useEffect, useRef} from 'react';
import type {View} from 'react-native';
import useCurrencyList from '@hooks/useCurrencyList';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import {getCurrencySymbol} from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -52,6 +52,7 @@ function CurrencySelector({
}: CurrencySelectorProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {getCurrencySymbol} = useCurrencyList();

const currencyTitleDescStyle = currency && !shouldShowCurrencySymbol ? styles.textNormal : null;

Expand Down
4 changes: 3 additions & 1 deletion src/components/EReceipt.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {useEffect, useRef} from 'react';
import {View} from 'react-native';
import useCurrencyList from '@hooks/useCurrencyList';
import useEReceipt from '@hooks/useEReceipt';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
Expand All @@ -8,7 +9,7 @@ import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {filterPersonalCards, getCardDescription, getCompanyCardDescription} from '@libs/CardUtils';
import {convertToDisplayString, getCurrencySymbol} from '@libs/CurrencyUtils';
import {convertToDisplayString} from '@libs/CurrencyUtils';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getTransactionDetails} from '@libs/ReportUtils';
import variables from '@styles/variables';
Expand Down Expand Up @@ -40,6 +41,7 @@ function EReceipt({transactionID, transactionItem, onLoad, isThumbnail = false}:
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate, preferredLocale} = useLocalize();
const {getCurrencySymbol} = useCurrencyList();
const theme = useTheme();
const icons = useMemoizedLazyExpensifyIcons(['ReceiptBody', 'ExpensifyWordmark']);
const [cardList] = useOnyx(ONYXKEYS.CARD_LIST, {selector: filterPersonalCards, canBeMissing: true});
Expand Down
10 changes: 6 additions & 4 deletions src/components/MoneyRequestConfirmationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {deepEqual} from 'fast-equals';
import React, {memo, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import {InteractionManager, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import useCurrencyList from '@hooks/useCurrencyList';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -279,7 +280,7 @@ function MoneyRequestConfirmationList({
});
const [policyCategoriesDraft] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES_DRAFT}${policyID}`, {canBeMissing: true});
const [lastSelectedDistanceRates] = useOnyx(ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES, {canBeMissing: true});
const [currencyList] = useOnyx(ONYXKEYS.CURRENCY_LIST, {canBeMissing: false});
const {getCurrencySymbol} = useCurrencyList();
const {isBetaEnabled} = usePermissions();
const {isDelegateAccessRestricted, showDelegateNoAccessModal} = useContext(DelegateNoAccessContext);

Expand Down Expand Up @@ -683,7 +684,7 @@ function MoneyRequestConfirmationList({
});
}

const currencySymbol = currencyList?.[iouCurrencyCode ?? '']?.symbol ?? iouCurrencyCode;
const currencySymbol = getCurrencySymbol(iouCurrencyCode ?? '') ?? iouCurrencyCode;
Comment thread
shubham1206agra marked this conversation as resolved.
Resolved
const formattedTotalAmount = convertToDisplayStringWithoutCurrency(iouAmount, iouCurrencyCode);

return [payeeOption, ...selectedParticipants].map((participantOption: Participant) => ({
Expand Down Expand Up @@ -721,7 +722,6 @@ function MoneyRequestConfirmationList({
isTypeSplit,
payeePersonalDetails,
shouldShowReadOnlySplits,
currencyList,
iouCurrencyCode,
iouAmount,
selectedParticipants,
Expand All @@ -738,6 +738,7 @@ function MoneyRequestConfirmationList({
transaction?.comment?.splits,
transaction?.splitShares,
onSplitShareChange,
getCurrencySymbol,
]);

const isSplitModified = useMemo(() => {
Expand Down Expand Up @@ -841,7 +842,7 @@ function MoneyRequestConfirmationList({
*/
setMoneyRequestPendingFields(transactionID, {waypoints: isDistanceRequestWithPendingRoute ? CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD : null});

const distanceMerchant = DistanceRequestUtils.getDistanceMerchant(hasRoute, distance, unit, rate ?? 0, currency ?? CONST.CURRENCY.USD, translate, toLocaleDigit);
const distanceMerchant = DistanceRequestUtils.getDistanceMerchant(hasRoute, distance, unit, rate ?? 0, currency ?? CONST.CURRENCY.USD, translate, toLocaleDigit, getCurrencySymbol);
setMoneyRequestMerchant(transactionID, distanceMerchant, true);
}, [
isDistanceRequestWithPendingRoute,
Expand All @@ -859,6 +860,7 @@ function MoneyRequestConfirmationList({
action,
isReadOnly,
isMovingTransactionFromTrackExpense,
getCurrencySymbol,
]);

// Auto select the category if there is only one enabled category and it is required
Expand Down
4 changes: 3 additions & 1 deletion src/components/MoneyRequestConfirmationListFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {memo, useMemo} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import useCurrencyList from '@hooks/useCurrencyList';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -283,6 +284,7 @@ function MoneyRequestConfirmationListFooter({
const icons = useMemoizedLazyExpensifyIcons(['Stopwatch', 'CalendarSolid']);
const styles = useThemeStyles();
const {translate, toLocaleDigit, localeCompare, preferredLocale} = useLocalize();
const {getCurrencySymbol} = useCurrencyList();
const {isOffline} = useNetwork();

const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
Expand Down Expand Up @@ -545,7 +547,7 @@ function MoneyRequestConfirmationListFooter({
<MenuItemWithTopDescription
key={translate('common.rate')}
shouldShowRightIcon={!!rate && !isReadOnly && iouType !== CONST.IOU.TYPE.SPLIT && !isUnreported}
title={DistanceRequestUtils.getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, isOffline)}
title={DistanceRequestUtils.getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, getCurrencySymbol, isOffline)}
description={translate('common.rate')}
style={[styles.moneyRequestMenuItem]}
titleStyle={styles.flex1}
Expand Down
4 changes: 3 additions & 1 deletion src/components/PerDiemEReceipt.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react';
import {View} from 'react-native';
import useCurrencyList from '@hooks/useCurrencyList';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import {convertAmountToDisplayString, convertToDisplayStringWithoutCurrency, getCurrencySymbol} from '@libs/CurrencyUtils';
import {convertAmountToDisplayString, convertToDisplayStringWithoutCurrency} from '@libs/CurrencyUtils';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getTransactionDetails} from '@libs/ReportUtils';
import variables from '@styles/variables';
Expand Down Expand Up @@ -52,6 +53,7 @@ function PerDiemEReceipt({transactionID}: PerDiemEReceiptProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate, preferredLocale} = useLocalize();
const {getCurrencySymbol} = useCurrencyList();
const icons = useMemoizedLazyExpensifyIcons(['ExpensifyWordmark']);
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(transactionID)}`, {
canBeMissing: true,
Expand Down
6 changes: 5 additions & 1 deletion src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Text from '@components/Text';
import ViolationMessages from '@components/ViolationMessages';
import {WideRHPContext} from '@components/WideRHPContextProvider';
import useActiveRoute from '@hooks/useActiveRoute';
import useCurrencyList from '@hooks/useCurrencyList';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useEnvironment from '@hooks/useEnvironment';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
Expand Down Expand Up @@ -167,6 +168,7 @@ function MoneyRequestView({
const {isOffline} = useNetwork();
const {environmentURL} = useEnvironment();
const {translate, toLocaleDigit, preferredLocale} = useLocalize();
const {getCurrencySymbol} = useCurrencyList();
const {getReportRHPActiveRoute} = useActiveRoute();
const [lastVisitedPath] = useOnyx(ONYXKEYS.LAST_VISITED_PATH, {canBeMissing: true});

Expand Down Expand Up @@ -414,7 +416,9 @@ function MoneyRequestView({
const currency = transactionCurrency ?? CONST.CURRENCY.USD;
const hasRequiredCompanyCardViolation = transactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.COMPANY_CARD_REQUIRED);
const isCustomUnitOutOfPolicy = transactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.CUSTOM_UNIT_OUT_OF_POLICY) || (isDistanceRequest && !rate);
let rateToDisplay = isCustomUnitOutOfPolicy ? translate('common.rateOutOfPolicy') : DistanceRequestUtils.getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, isOffline);
let rateToDisplay = isCustomUnitOutOfPolicy
? translate('common.rateOutOfPolicy')
: DistanceRequestUtils.getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, getCurrencySymbol, isOffline);
const distanceToDisplay = DistanceRequestUtils.getDistanceForDisplay(hasRoute, distance, unit, rate, translate);
let merchantTitle = isEmptyMerchant ? '' : transactionMerchant;
let amountTitle = formattedTransactionAmount?.toString() || '';
Expand Down
5 changes: 3 additions & 2 deletions src/components/Search/SearchAutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {AnimatedTextInputRef} from '@components/RNTextInput';
import type {SelectionListHandle} from '@components/SelectionListWithSections/types';
import TextInput from '@components/TextInput';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import useCurrencyList from '@hooks/useCurrencyList';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useFocusAfterNav from '@hooks/useFocusAfterNav';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -102,8 +103,8 @@ function SearchAutocompleteInput({
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const inputRef = useRef<AnimatedTextInputRef>(null);
const autoFocusAfterNav = useFocusAfterNav(inputRef, shouldDelayFocus);
const [currencyList] = useOnyx(ONYXKEYS.CURRENCY_LIST, {canBeMissing: false});
const currencyAutocompleteList = Object.keys(currencyList ?? {}).filter((currencyCode) => !currencyList?.[currencyCode]?.retired);
const {currencyList} = useCurrencyList();
const currencyAutocompleteList = Object.keys(currencyList).filter((currencyCode) => !currencyList[currencyCode]?.retired);
const currencySharedValue = useSharedValue(currencyAutocompleteList);

const [allPolicyCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES, {canBeMissing: false});
Expand Down
5 changes: 3 additions & 2 deletions src/components/Search/SearchAutocompleteList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {SearchQueryItem, SearchQueryListItemProps} from '@components/Select
import SearchQueryListItem, {isSearchQueryItem} from '@components/SelectionListWithSections/Search/SearchQueryListItem';
import type {SectionListDataType, SelectionListHandle, UserListItemProps} from '@components/SelectionListWithSections/types';
import UserListItem from '@components/SelectionListWithSections/UserListItem';
import useCurrencyList from '@hooks/useCurrencyList';
import useDebounce from '@hooks/useDebounce';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -308,8 +309,8 @@ function SearchAutocompleteList({
[policies, currentUserLogin],
);

const [currencyList] = useOnyx(ONYXKEYS.CURRENCY_LIST, {canBeMissing: false});
const currencyAutocompleteList = useMemo(() => Object.keys(currencyList ?? {}).filter((currency) => !currencyList?.[currency]?.retired), [currencyList]);
const {currencyList} = useCurrencyList();
const currencyAutocompleteList = useMemo(() => Object.keys(currencyList).filter((currency) => !currencyList[currency]?.retired), [currencyList]);
const [recentCurrencyAutocompleteList] = useOnyx(ONYXKEYS.RECENTLY_USED_CURRENCIES, {canBeMissing: true});
const [allPoliciesTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS, {canBeMissing: false});
const [allRecentTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS, {canBeMissing: true});
Expand Down
10 changes: 5 additions & 5 deletions src/components/Search/SearchFiltersCurrencyBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import React, {useMemo} from 'react';
import {View} from 'react-native';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import useCurrencyList from '@hooks/useCurrencyList';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useThemeStyles from '@hooks/useThemeStyles';
import {updateAdvancedFilters} from '@libs/actions/Search';
import {getCurrencySymbol} from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
Expand All @@ -26,16 +26,16 @@ type SearchFiltersCurrencyBaseProps = {
function SearchFiltersCurrencyBase({title, filterKey, multiselect = false}: SearchFiltersCurrencyBaseProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [currencyList] = useOnyx(ONYXKEYS.CURRENCY_LIST, {canBeMissing: false});
const {currencyList, getCurrencySymbol} = useCurrencyList();
const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM, {canBeMissing: false});
const selectedCurrencyData = searchAdvancedFiltersForm?.[filterKey];

const {selectedCurrenciesItems, currencyItems} = useMemo(() => {
const currencies: SearchSingleSelectionPickerItem[] = [];
const selectedCurrencies: SearchSingleSelectionPickerItem[] = [];

for (const currencyCode of Object.keys(currencyList ?? {})) {
if (currencyList?.[currencyCode]?.retired) {
for (const currencyCode of Object.keys(currencyList)) {
if (currencyList[currencyCode]?.retired) {
continue;
}

Expand All @@ -53,7 +53,7 @@ function SearchFiltersCurrencyBase({title, filterKey, multiselect = false}: Sear
}

return {selectedCurrenciesItems: selectedCurrencies, currencyItems: currencies};
}, [currencyList, selectedCurrencyData]);
}, [currencyList, selectedCurrencyData, getCurrencySymbol]);

const handleOnSubmit = (values: string[] | string | undefined) => {
updateAdvancedFilters({[filterKey]: values ?? null} as Partial<SearchAdvancedFiltersForm>);
Expand Down
Loading