From 2207db402a87f104de8892bdec402351c9ab334e Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Wed, 8 May 2024 18:58:03 +0700 Subject: [PATCH 01/21] fix implement recently used currencies --- src/ONYXKEYS.ts | 2 + src/ROUTES.ts | 7 +- .../CurrencySelectionList/index.tsx | 30 ++++++++- src/components/CurrencySelectionList/types.ts | 3 + src/libs/Navigation/types.ts | 1 + src/libs/actions/IOU.ts | 65 ++++++++++++++++++- src/libs/actions/Policy.ts | 19 ++++++ .../iou/request/step/IOURequestStepAmount.tsx | 2 +- .../request/step/IOURequestStepCurrency.tsx | 9 ++- .../step/IOURequestStepTaxAmountPage.tsx | 1 + src/types/onyx/RecentlyUsedCurrencies.ts | 3 + src/types/onyx/index.ts | 2 + 12 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 src/types/onyx/RecentlyUsedCurrencies.ts diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index 804c8dadd553..a7f63306ef23 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -326,6 +326,7 @@ const ONYXKEYS = { POLICY_RECENTLY_USED_CATEGORIES: 'policyRecentlyUsedCategories_', POLICY_TAGS: 'policyTags_', POLICY_RECENTLY_USED_TAGS: 'nvp_recentlyUsedTags_', + POLICY_RECENTLY_USED_CURRENCIES: 'nvp_recentlyUsedCurrencies_', // Whether the policy's connection data was attempted to be fetched in // the current user session. As this state only exists client-side, it // should not be included as part of the policy object. The policy @@ -565,6 +566,7 @@ type OnyxCollectionValuesMapping = { [ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS]: OnyxTypes.TransactionViolations; [ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT]: OnyxTypes.Transaction; [ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS]: OnyxTypes.RecentlyUsedTags; + [ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES]: OnyxTypes.RecentlyUsedCurrencies; [ONYXKEYS.COLLECTION.OLD_POLICY_RECENTLY_USED_TAGS]: OnyxTypes.RecentlyUsedTags; [ONYXKEYS.COLLECTION.SELECTED_TAB]: OnyxTypes.SelectedTabRequest; [ONYXKEYS.COLLECTION.PRIVATE_NOTES_DRAFT]: string; diff --git a/src/ROUTES.ts b/src/ROUTES.ts index afe806c193aa..5c826a10a0af 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -348,8 +348,11 @@ const ROUTES = { }, MONEY_REQUEST_STEP_CURRENCY: { route: ':action/:iouType/currency/:transactionID/:reportID/:pageIndex?', - getRoute: (action: IOUAction, iouType: IOUType, transactionID: string, reportID: string, pageIndex = '', currency = '', backTo = '') => - getUrlWithBackToParam(`${action as string}/${iouType as string}/currency/${transactionID}/${reportID}/${pageIndex}?currency=${currency}`, backTo), + getRoute: (action: IOUAction, iouType: IOUType, transactionID: string, reportID: string, pageIndex = '', currency = '', backTo = '', policyID?: string) => + getUrlWithBackToParam( + `${action as string}/${iouType as string}/currency/${transactionID}/${reportID}/${pageIndex}?currency=${currency}${`${policyID ? `&policyID=${policyID}` : ''}`}`, + backTo, + ), }, MONEY_REQUEST_STEP_DATE: { route: ':action/:iouType/date/:transactionID/:reportID/:reportActionID?', diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 361d82140326..654bda46a7d9 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -6,9 +6,10 @@ import RadioListItem from '@components/SelectionList/RadioListItem'; import useLocalize from '@hooks/useLocalize'; import * as CurrencyUtils from '@libs/CurrencyUtils'; import ONYXKEYS from '@src/ONYXKEYS'; +import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type {CurrencyListItem, CurrencySelectionListOnyxProps, CurrencySelectionListProps} from './types'; -function CurrencySelectionList({searchInputLabel, initiallySelectedCurrencyCode, onSelect, currencyList}: CurrencySelectionListProps) { +function CurrencySelectionList({searchInputLabel, initiallySelectedCurrencyCode, onSelect, currencyList, policyRecentlyUsedCurrencies}: CurrencySelectionListProps) { const [searchValue, setSearchValue] = useState(''); const {translate} = useLocalize(); @@ -24,10 +25,35 @@ function CurrencySelectionList({searchInputLabel, initiallySelectedCurrencyCode, }; }); + const policyRecentlyUsedCurrencyOptions: CurrencyListItem[] = + policyRecentlyUsedCurrencies?.map((currencyCode) => { + const currencyInfo = currencyList?.[currencyCode]; + const isSelectedCurrency = currencyCode === initiallySelectedCurrencyCode; + return { + currencyName: currencyInfo?.name ?? '', + text: `${currencyCode} - ${CurrencyUtils.getCurrencySymbol(currencyCode)}`, + currencyCode, + keyForList: currencyCode, + isSelected: isSelectedCurrency, + }; + }) ?? []; + const searchRegex = new RegExp(Str.escapeForRegExp(searchValue.trim()), 'i'); const filteredCurrencies = currencyOptions.filter((currencyOption) => searchRegex.test(currencyOption.text ?? '') || searchRegex.test(currencyOption.currencyName)); const isEmpty = searchValue.trim() && !filteredCurrencies.length; + const shouldDisplayRecentlyOptions = !isEmptyObject(policyRecentlyUsedCurrencyOptions) && !searchValue; + if (shouldDisplayRecentlyOptions) { + return { + sections: isEmpty + ? [] + : [ + {title: translate('common.recents'), data: policyRecentlyUsedCurrencyOptions, shouldShow: shouldDisplayRecentlyOptions}, + {title: translate('common.all'), data: filteredCurrencies}, + ], + headerMessage: isEmpty ? translate('common.noResultsFound') : '', + }; + } return { sections: isEmpty ? [] @@ -38,7 +64,7 @@ function CurrencySelectionList({searchInputLabel, initiallySelectedCurrencyCode, ], headerMessage: isEmpty ? translate('common.noResultsFound') : '', }; - }, [currencyList, searchValue, translate, initiallySelectedCurrencyCode]); + }, [currencyList, searchValue, translate, initiallySelectedCurrencyCode, policyRecentlyUsedCurrencies]); return ( void; }; diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 13204fe97111..dac43adbf6fb 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -597,6 +597,7 @@ type MoneyRequestNavigatorParamList = { pageIndex?: string; backTo?: Routes; currency?: string; + policyID?: string; }; }; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 7171a7d6732a..cf9b1476efaf 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -455,6 +455,7 @@ function buildOnyxDataForMoneyRequest( optimisticNextStep?: OnyxTypes.ReportNextStep | null, isOneOnOneSplit = false, existingTransactionThreadReportID?: string, + optimisticPolicyRecentlyUsedCurrencies?: string[], ): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] { const isScanRequest = TransactionUtils.isScanRequest(transaction); const outstandingChildRequest = ReportUtils.getOutstandingChildRequest(iouReport); @@ -578,6 +579,14 @@ function buildOnyxDataForMoneyRequest( }); } + if (optimisticPolicyRecentlyUsedCurrencies?.length) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${iouReport.policyID}`, + value: optimisticPolicyRecentlyUsedCurrencies, + }); + } + if (!isEmptyObject(optimisticPolicyRecentlyUsedTags)) { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -819,6 +828,7 @@ function buildOnyxDataForInvoice( policy?: OnyxEntry, policyTagList?: OnyxEntry, policyCategories?: OnyxEntry, + optimisticPolicyRecentlyUsedCurrencies?: string[], ): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] { const clearedPendingFields = Object.fromEntries(Object.keys(transaction.pendingFields ?? {}).map((key) => [key, null])); const optimisticData: OnyxUpdate[] = [ @@ -907,6 +917,14 @@ function buildOnyxDataForInvoice( }); } + if (optimisticPolicyRecentlyUsedCurrencies?.length) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${iouReport.policyID}`, + value: optimisticPolicyRecentlyUsedCurrencies, + }); + } + if (!isEmptyObject(optimisticPolicyRecentlyUsedTags)) { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -1656,7 +1674,7 @@ function getSendInvoiceInformation( const optimisticPolicyRecentlyUsedCategories = Policy.buildOptimisticPolicyRecentlyUsedCategories(optimisticInvoiceReport.policyID, category); const optimisticPolicyRecentlyUsedTags = Policy.buildOptimisticPolicyRecentlyUsedTags(optimisticInvoiceReport.policyID, tag); - + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(optimisticInvoiceReport.policyID, currency); // STEP 4: Add optimistic personal details for participant const shouldCreateOptimisticPersonalDetails = isNewChatReport && !allPersonalDetails[receiverAccountID]; if (shouldCreateOptimisticPersonalDetails) { @@ -1713,6 +1731,7 @@ function getSendInvoiceInformation( policy, policyTagList, policyCategories, + optimisticPolicyRecentlyUsedCurrencies, ); return { @@ -1835,7 +1854,8 @@ function getMoneyRequestInformation( const optimisticPolicyRecentlyUsedCategories = Policy.buildOptimisticPolicyRecentlyUsedCategories(iouReport.policyID, category); const optimisticPolicyRecentlyUsedTags = Policy.buildOptimisticPolicyRecentlyUsedTags(iouReport.policyID, tag); - + const optimisticPolicyRecentluUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(iouReport.policyID, currency); + // If there is an existing transaction (which is the case for distance requests), then the data from the existing transaction // needs to be manually merged into the optimistic transaction. This is because buildOnyxDataForMoneyRequest() uses `Onyx.set()` for the transaction // data. This is a big can of worms to change it to `Onyx.merge()` as explored in https://expensify.slack.com/archives/C05DWUDHVK7/p1692139468252109. @@ -1922,6 +1942,9 @@ function getMoneyRequestInformation( policyTagList, policyCategories, optimisticNextStep, + undefined, + undefined, + optimisticPolicyRecentluUsedCurrencies, ); return { @@ -2472,6 +2495,18 @@ function getUpdateMoneyRequestParams( } } + // Update recently used currencies if the category is changed + if ('currency' in transactionChanges) { + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(iouReport?.policyID, transactionChanges.currency); + if (optimisticPolicyRecentlyUsedCurrencies.length) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${iouReport?.policyID}`, + value: optimisticPolicyRecentlyUsedCurrencies, + }); + } + } + // Update recently used categories if the tag is changed if ('tag' in transactionChanges) { const optimisticPolicyRecentlyUsedTags = Policy.buildOptimisticPolicyRecentlyUsedTags(iouReport?.policyID, transactionChanges.tag); @@ -3957,6 +3992,9 @@ function createSplitsAndOnyxData( // Add category to optimistic policy recently used categories when a participant is a workspace const optimisticPolicyRecentlyUsedCategories = isPolicyExpenseChat ? Policy.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category) : []; + // Add category to optimistic policy recently used currencies when a participant is a workspace + const optimisticPolicyRecentlyUsedCurrencies = isPolicyExpenseChat ? Policy.buildOptimisticPolicyRecentlyUsedCurrencies(participant.policyID, currency) : []; + // Add tag to optimistic policy recently used tags when a participant is a workspace const optimisticPolicyRecentlyUsedTags = isPolicyExpenseChat ? Policy.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag) : {}; @@ -3981,6 +4019,8 @@ function createSplitsAndOnyxData( null, null, true, + undefined, + optimisticPolicyRecentlyUsedCurrencies, ); const individualSplit = { @@ -4426,6 +4466,7 @@ function startSplitBill({ } const optimisticPolicyRecentlyUsedCategories = Policy.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category); + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(participant.policyID, currency); const optimisticPolicyRecentlyUsedTags = Policy.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag); if (optimisticPolicyRecentlyUsedCategories.length > 0) { @@ -4436,6 +4477,14 @@ function startSplitBill({ }); } + if (optimisticPolicyRecentlyUsedCurrencies.length > 0) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${participant.policyID}`, + value: optimisticPolicyRecentlyUsedCurrencies, + }); + } + if (!isEmptyObject(optimisticPolicyRecentlyUsedTags)) { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, @@ -4853,6 +4902,18 @@ function editRegularMoneyRequest( } } + // Update recently used currencies if the category is changed + if ('currency' in transactionChanges) { + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(iouReport?.policyID, transactionChanges.currency); + if (optimisticPolicyRecentlyUsedCurrencies.length) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${iouReport?.policyID}`, + value: optimisticPolicyRecentlyUsedCurrencies, + }); + } + } + // Update recently used categories if the tag is changed if ('tag' in transactionChanges) { const optimisticPolicyRecentlyUsedTags = Policy.buildOptimisticPolicyRecentlyUsedTags(iouReport?.policyID, transactionChanges.tag); diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 6dc46383f3fa..1bfe60a1551a 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -86,6 +86,7 @@ import type { PolicyTagList, PolicyTags, RecentlyUsedCategories, + RecentlyUsedCurrencies, RecentlyUsedTags, ReimbursementAccount, Report, @@ -218,6 +219,13 @@ Onyx.connect({ callback: (val) => (allRecentlyUsedCategories = val), }); +let allRecentlyUsedCurrencies: OnyxCollection = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES, + waitForCollectionCallback: true, + callback: (val) => (allRecentlyUsedCurrencies = val), +}); + let allPolicyTags: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY_TAGS, @@ -2770,6 +2778,16 @@ function buildOptimisticPolicyRecentlyUsedCategories(policyID?: string, category return lodashUnion([category], policyRecentlyUsedCategories); } +function buildOptimisticPolicyRecentlyUsedCurrencies(policyID?: string, currency?: string) { + if (!policyID || !currency) { + return []; + } + + const policyRecentlyUsedCurrencies = allRecentlyUsedCurrencies?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${policyID}`] ?? []; + + return lodashUnion([currency], policyRecentlyUsedCurrencies); +} + function buildOptimisticPolicyRecentlyUsedTags(policyID?: string, transactionTags?: string): RecentlyUsedTags { if (!policyID || !transactionTags) { return {}; @@ -5236,6 +5254,7 @@ export { dismissAddedWithPrimaryLoginMessages, openDraftWorkspaceRequest, buildOptimisticPolicyRecentlyUsedCategories, + buildOptimisticPolicyRecentlyUsedCurrencies, buildOptimisticPolicyRecentlyUsedTags, createDraftInitialWorkspace, setWorkspaceInviteMessageDraft, diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 6786198b1dc8..c418c8042ceb 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -128,7 +128,7 @@ function IOURequestStepAmount({ const navigateToCurrencySelectionPage = () => { Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CURRENCY.getRoute(action, iouType, transactionID, reportID, backTo ? 'confirm' : '', currency, Navigation.getActiveRouteWithoutParams()), + ROUTES.MONEY_REQUEST_STEP_CURRENCY.getRoute(action, iouType, transactionID, reportID, backTo ? 'confirm' : '', currency, Navigation.getActiveRouteWithoutParams(), policy?.id), ); }; diff --git a/src/pages/iou/request/step/IOURequestStepCurrency.tsx b/src/pages/iou/request/step/IOURequestStepCurrency.tsx index 8669563f3b9f..4037cb58de6e 100644 --- a/src/pages/iou/request/step/IOURequestStepCurrency.tsx +++ b/src/pages/iou/request/step/IOURequestStepCurrency.tsx @@ -14,7 +14,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES, {getUrlWithBackToParam} from '@src/ROUTES'; import type {Route} from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; -import type {Transaction} from '@src/types/onyx'; +import type {RecentlyUsedCurrencies, Transaction} from '@src/types/onyx'; import StepScreenWrapper from './StepScreenWrapper'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; @@ -22,6 +22,8 @@ import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNo type IOURequestStepCurrencyOnyxProps = { /** The draft transaction object being modified in Onyx */ draftTransaction: OnyxEntry; + /** List of recently used currencies */ + policyRecentlyUsedCurrencies: OnyxEntry; }; type IOURequestStepCurrencyProps = IOURequestStepCurrencyOnyxProps & WithFullTransactionOrNotFoundProps; @@ -31,6 +33,7 @@ function IOURequestStepCurrency({ params: {backTo, iouType, pageIndex, reportID, transactionID, action, currency: selectedCurrency = ''}, }, draftTransaction, + policyRecentlyUsedCurrencies, }: IOURequestStepCurrencyProps) { const {translate} = useLocalize(); const {currency: originalCurrency = ''} = ReportUtils.getTransactionDetails(draftTransaction) ?? {}; @@ -74,6 +77,7 @@ function IOURequestStepCurrency({ > {({didScreenTransitionEnd}) => ( { if (!didScreenTransitionEnd) { @@ -97,6 +101,9 @@ const IOURequestStepCurrencyWithOnyx = withOnyx `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${route.params.policyID ?? 0}`, + }, })(IOURequestStepCurrency); /* eslint-disable rulesdir/no-negated-variables */ diff --git a/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx b/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx index 22a00591b4fc..d90c7e3c155e 100644 --- a/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx +++ b/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx @@ -96,6 +96,7 @@ function IOURequestStepTaxAmountPage({ backTo ? 'confirm' : '', currency, Navigation.getActiveRouteWithoutParams(), + policy?.id, ), ); }; diff --git a/src/types/onyx/RecentlyUsedCurrencies.ts b/src/types/onyx/RecentlyUsedCurrencies.ts new file mode 100644 index 000000000000..59762123c34c --- /dev/null +++ b/src/types/onyx/RecentlyUsedCurrencies.ts @@ -0,0 +1,3 @@ +type RecentlyUsedCurrencies = string[]; + +export default RecentlyUsedCurrencies; diff --git a/src/types/onyx/index.ts b/src/types/onyx/index.ts index 1695daebace8..532465c550ec 100644 --- a/src/types/onyx/index.ts +++ b/src/types/onyx/index.ts @@ -47,6 +47,7 @@ import type PriorityMode from './PriorityMode'; import type PrivatePersonalDetails from './PrivatePersonalDetails'; import type QuickAction from './QuickAction'; import type RecentlyUsedCategories from './RecentlyUsedCategories'; +import type RecentlyUsedCurrencies from './RecentlyUsedCurrencies'; import type RecentlyUsedReportFields from './RecentlyUsedReportFields'; import type RecentlyUsedTags from './RecentlyUsedTags'; import type RecentWaypoint from './RecentWaypoint'; @@ -132,6 +133,7 @@ export type { RecentWaypoint, RecentlyUsedCategories, RecentlyUsedTags, + RecentlyUsedCurrencies, ReimbursementAccount, Report, ReportAction, From 0309bbfd5b54a6d8303fa687ee977d4845c2f6f5 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Thu, 9 May 2024 10:42:38 +0700 Subject: [PATCH 02/21] fix lint --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index cf9b1476efaf..784c460e2798 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1855,7 +1855,7 @@ function getMoneyRequestInformation( const optimisticPolicyRecentlyUsedCategories = Policy.buildOptimisticPolicyRecentlyUsedCategories(iouReport.policyID, category); const optimisticPolicyRecentlyUsedTags = Policy.buildOptimisticPolicyRecentlyUsedTags(iouReport.policyID, tag); const optimisticPolicyRecentluUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(iouReport.policyID, currency); - + // If there is an existing transaction (which is the case for distance requests), then the data from the existing transaction // needs to be manually merged into the optimistic transaction. This is because buildOnyxDataForMoneyRequest() uses `Onyx.set()` for the transaction // data. This is a big can of worms to change it to `Onyx.merge()` as explored in https://expensify.slack.com/archives/C05DWUDHVK7/p1692139468252109. From cfa6401396ebedb234b99c4daab1a37c6852de98 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Wed, 12 Jun 2024 16:13:36 +0700 Subject: [PATCH 03/21] fix handle p2p request --- src/libs/actions/Policy/Policy.ts | 12 +++++++++--- src/types/onyx/RecentlyUsedCurrencies.ts | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 15f6d209477e..ff2afc3dcb97 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -2071,11 +2071,17 @@ function dismissAddedWithPrimaryLoginMessages(policyID: string) { } function buildOptimisticPolicyRecentlyUsedCurrencies(policyID?: string, currency?: string) { - if (!policyID || !currency) { + if (!currency) { return []; } - - const policyRecentlyUsedCurrencies = allRecentlyUsedCurrencies?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${policyID}`] ?? []; + const personalPolicy = PolicyUtils.getPersonalPolicy(); + const personalPolicyID = personalPolicy?.id; + if (!policyID && !personalPolicyID) { + return []; + } + const policyRecentlyUsedCurrencies = policyID + ? allRecentlyUsedCurrencies?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${policyID}`] + : allRecentlyUsedCurrencies?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${personalPolicyID}`]; return lodashUnion([currency], policyRecentlyUsedCurrencies); } diff --git a/src/types/onyx/RecentlyUsedCurrencies.ts b/src/types/onyx/RecentlyUsedCurrencies.ts index 59762123c34c..9e8ee7f9b6e9 100644 --- a/src/types/onyx/RecentlyUsedCurrencies.ts +++ b/src/types/onyx/RecentlyUsedCurrencies.ts @@ -1,3 +1,4 @@ +/** Policy currencies that have been recently used for each policy */ type RecentlyUsedCurrencies = string[]; export default RecentlyUsedCurrencies; From 4ea13c547fa0f4bec87764097d3b94e6c2fccb06 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Tue, 18 Jun 2024 21:31:39 +0700 Subject: [PATCH 04/21] fix type --- src/libs/actions/Policy/Policy.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index baec3de91ad1..85a5ee4d451b 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -51,9 +51,20 @@ import * as TransactionUtils from '@libs/TransactionUtils'; import type {PolicySelector} from '@pages/home/sidebar/SidebarScreen/FloatingActionButtonAndPopover'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {InvitedEmailsToAccountIDs, PersonalDetailsList, Policy, PolicyCategory, ReimbursementAccount, Report, ReportAction, TaxRatesWithDefault, Transaction} from '@src/types/onyx'; +import type { + InvitedEmailsToAccountIDs, + PersonalDetailsList, + Policy, + PolicyCategory, + RecentlyUsedCurrencies, + ReimbursementAccount, + Report, + ReportAction, + TaxRatesWithDefault, + Transaction, +} from '@src/types/onyx'; import type {Errors} from '@src/types/onyx/OnyxCommon'; -import type {Attributes, CompanyAddress, CustomUnit, Rate, RecentlyUsedCurrencies, TaxRate, Unit} from '@src/types/onyx/Policy'; +import type {Attributes, CompanyAddress, CustomUnit, Rate, TaxRate, Unit} from '@src/types/onyx/Policy'; import type {OnyxData} from '@src/types/onyx/Request'; import type {EmptyObject} from '@src/types/utils/EmptyObject'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; From 2326651eddfc8b3ae4fd78cc63308c73ee88b7f3 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Wed, 17 Jul 2024 18:00:10 +0700 Subject: [PATCH 05/21] fix fallback to personal policy --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index bc8fbcc3188b..ba8d73d7dd74 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -10,6 +10,7 @@ import * as TransactionEdit from '@libs/actions/TransactionEdit'; import * as CurrencyUtils from '@libs/CurrencyUtils'; import Navigation from '@libs/Navigation/Navigation'; import * as OptionsListUtils from '@libs/OptionsListUtils'; +import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import {getRequestType} from '@libs/TransactionUtils'; @@ -134,7 +135,16 @@ function IOURequestStepAmount({ const navigateToCurrencySelectionPage = () => { Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CURRENCY.getRoute(action, iouType, transactionID, reportID, backTo ? 'confirm' : '', currency, Navigation.getActiveRouteWithoutParams(), policy?.id), + ROUTES.MONEY_REQUEST_STEP_CURRENCY.getRoute( + action, + iouType, + transactionID, + reportID, + backTo ? 'confirm' : '', + currency, + Navigation.getActiveRouteWithoutParams(), + policy?.id || PolicyUtils.getPersonalPolicy()?.id, + ), ); }; From bd0d2bc223eb8cc02a207e76735489d8f68a3821 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Wed, 17 Jul 2024 18:11:49 +0700 Subject: [PATCH 06/21] fix lint --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index ba8d73d7dd74..e7eca6cb7643 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -143,7 +143,7 @@ function IOURequestStepAmount({ backTo ? 'confirm' : '', currency, Navigation.getActiveRouteWithoutParams(), - policy?.id || PolicyUtils.getPersonalPolicy()?.id, + policy?.id ?? PolicyUtils.getPersonalPolicy()?.id, ), ); }; From 4e0eb11726596f3ee672a87c02e135fbceef3560 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Tue, 30 Jul 2024 16:57:27 +0700 Subject: [PATCH 07/21] fix: only display 5 recently used options --- src/components/CurrencySelectionList/index.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 53a64e39b40d..6148d5f084b2 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -5,6 +5,7 @@ import SelectionList from '@components/SelectionList'; import RadioListItem from '@components/SelectionList/RadioListItem'; import useLocalize from '@hooks/useLocalize'; import * as CurrencyUtils from '@libs/CurrencyUtils'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type {CurrencyListItem, CurrencySelectionListOnyxProps, CurrencySelectionListProps} from './types'; @@ -44,11 +45,16 @@ function CurrencySelectionList({searchInputLabel, initiallySelectedCurrencyCode, const shouldDisplayRecentlyOptions = !isEmptyObject(policyRecentlyUsedCurrencyOptions) && !searchValue; if (shouldDisplayRecentlyOptions) { + const cutPolicyRecentlyUsedCurrencyOptions = policyRecentlyUsedCurrencyOptions.slice(0, CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW); return { sections: isEmpty ? [] : [ - {title: translate('common.recents'), data: policyRecentlyUsedCurrencyOptions, shouldShow: shouldDisplayRecentlyOptions}, + { + title: translate('common.recents'), + data: cutPolicyRecentlyUsedCurrencyOptions, + shouldShow: shouldDisplayRecentlyOptions, + }, {title: translate('common.all'), data: filteredCurrencies}, ], headerMessage: isEmpty ? translate('common.noResultsFound') : '', From dd91aa32a96dfb08fcf658ea0c088ba331c530c1 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Mon, 5 Aug 2024 17:09:45 +0700 Subject: [PATCH 08/21] feat: move selected option to top --- .../CurrencySelectionList/index.tsx | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 6148d5f084b2..992826212416 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -1,5 +1,5 @@ import {Str} from 'expensify-common'; -import React, {useMemo, useState} from 'react'; +import React, {useCallback, useMemo, useState} from 'react'; import {withOnyx} from 'react-native-onyx'; import SelectionList from '@components/SelectionList'; import RadioListItem from '@components/SelectionList/RadioListItem'; @@ -13,7 +13,7 @@ import type {CurrencyListItem, CurrencySelectionListOnyxProps, CurrencySelection function CurrencySelectionList({searchInputLabel, initiallySelectedCurrencyCode, onSelect, currencyList, policyRecentlyUsedCurrencies}: CurrencySelectionListProps) { 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 ?? {}).map(([currencyCode, currencyInfo]) => { const isSelectedCurrency = currencyCode === initiallySelectedCurrencyCode; @@ -43,34 +43,35 @@ function CurrencySelectionList({searchInputLabel, initiallySelectedCurrencyCode, const filteredCurrencies = currencyOptions.filter((currencyOption) => searchRegex.test(currencyOption.text ?? '') || searchRegex.test(currencyOption.currencyName)); const isEmpty = searchValue.trim() && !filteredCurrencies.length; const shouldDisplayRecentlyOptions = !isEmptyObject(policyRecentlyUsedCurrencyOptions) && !searchValue; - + const selectedOption = currencyOptions.find((option) => option.isSelected); + const shouldDisplaySelectedOptionOnTop = selectedOption && !searchValue; + let sections = []; + if (shouldDisplaySelectedOptionOnTop) { + sections.push({ + title: '', + data: [selectedOption], + shouldShow: true, + }); + } if (shouldDisplayRecentlyOptions) { const cutPolicyRecentlyUsedCurrencyOptions = policyRecentlyUsedCurrencyOptions.slice(0, CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW); - return { - sections: isEmpty - ? [] - : [ - { - title: translate('common.recents'), - data: cutPolicyRecentlyUsedCurrencyOptions, - shouldShow: shouldDisplayRecentlyOptions, - }, - {title: translate('common.all'), data: filteredCurrencies}, - ], - headerMessage: isEmpty ? translate('common.noResultsFound') : '', - }; + if (!isEmpty) { + sections.push( + { + title: translate('common.recents'), + data: shouldDisplaySelectedOptionOnTop ? getUnselectedOptions(cutPolicyRecentlyUsedCurrencyOptions) : cutPolicyRecentlyUsedCurrencyOptions, + shouldShow: shouldDisplayRecentlyOptions, + }, + {title: translate('common.all'), data: shouldDisplayRecentlyOptions ? getUnselectedOptions(filteredCurrencies) : filteredCurrencies}, + ); + } + } else if (!isEmpty) { + sections.push({ + data: shouldDisplaySelectedOptionOnTop ? getUnselectedOptions(filteredCurrencies) : filteredCurrencies, + }); } - return { - sections: isEmpty - ? [] - : [ - { - data: filteredCurrencies, - }, - ], - headerMessage: isEmpty ? translate('common.noResultsFound') : '', - }; - }, [currencyList, searchValue, translate, initiallySelectedCurrencyCode, policyRecentlyUsedCurrencies]); + return {sections, headerMessage: isEmpty ? translate('common.noResultsFound') : ''}; + }, [currencyList, searchValue, translate, initiallySelectedCurrencyCode, policyRecentlyUsedCurrencies, getUnselectedOptions]); return ( Date: Mon, 5 Aug 2024 17:43:57 +0700 Subject: [PATCH 09/21] fix lint --- src/components/CurrencySelectionList/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 992826212416..dfe7135bf7f0 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -45,7 +45,7 @@ function CurrencySelectionList({searchInputLabel, initiallySelectedCurrencyCode, const shouldDisplayRecentlyOptions = !isEmptyObject(policyRecentlyUsedCurrencyOptions) && !searchValue; const selectedOption = currencyOptions.find((option) => option.isSelected); const shouldDisplaySelectedOptionOnTop = selectedOption && !searchValue; - let sections = []; + const sections = []; if (shouldDisplaySelectedOptionOnTop) { sections.push({ title: '', From 7d35e6a8ce7a56b2f553b1069fcf94c176e86cfb Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Mon, 12 Aug 2024 17:03:18 +0700 Subject: [PATCH 10/21] fix comment --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index f80e791487fb..d5ee58dddb90 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4241,7 +4241,7 @@ function createSplitsAndOnyxData( // Add category to optimistic policy recently used categories when a participant is a workspace const optimisticPolicyRecentlyUsedCategories = isPolicyExpenseChat ? Category.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category) : []; - // Add category to optimistic policy recently used currencies when a participant is a workspace + // Add currency to optimistic policy recently used currencies when a participant is a workspace const optimisticPolicyRecentlyUsedCurrencies = isPolicyExpenseChat ? Policy.buildOptimisticPolicyRecentlyUsedCurrencies(participant.policyID, currency) : []; // Add tag to optimistic policy recently used tags when a participant is a workspace From b081a80c7aeb2d751da40836a8d3621c2be720c1 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Mon, 12 Aug 2024 17:20:04 +0700 Subject: [PATCH 11/21] fix lint --- src/libs/actions/Policy/Policy.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 238cfcad3ba6..f61e6c6e5cb9 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -2112,11 +2112,9 @@ function buildOptimisticPolicyRecentlyUsedCurrencies(policyID?: string, currency if (!policyID && !personalPolicyID) { return []; } - const policyRecentlyUsedCurrencies = policyID ? allRecentlyUsedCurrencies?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${policyID}`] : allRecentlyUsedCurrencies?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${personalPolicyID}`]; - console.log("111111111", {policyID, personalPolicyID, name: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${personalPolicyID}`}) return lodashUnion([currency], policyRecentlyUsedCurrencies); } From 63584df6606367d24c66b2c026d79bf9ef0662d8 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Mon, 12 Aug 2024 18:05:10 +0700 Subject: [PATCH 12/21] fix lint --- src/components/CurrencySelectionList/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 9108310bc7c4..3342f92fb19d 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -93,7 +93,7 @@ function CurrencySelectionList({ } return {sections: result, headerMessage: isEmpty ? translate('common.noResultsFound') : ''}; - }, [currencyList, searchValue, canSelectMultiple, translate, initiallySelectedCurrencyCode, selectedCurrencies]); + }, [currencyList, searchValue, canSelectMultiple, translate, initiallySelectedCurrencyCode, selectedCurrencies, getUnselectedOptions, policyRecentlyUsedCurrencies]); return ( Date: Tue, 20 Aug 2024 16:09:45 +0700 Subject: [PATCH 13/21] fix lint --- src/components/CurrencySelectionList/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 8964bb90b1e4..a70d6aadebaa 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -84,7 +84,7 @@ function CurrencySelectionList({ } return {sections: result, headerMessage: isEmpty ? translate('common.noResultsFound') : ''}; - }, [currencyList, searchValue, canSelectMultiple, translate, initiallySelectedCurrencyCode, selectedCurrencies, getUnselectedOptions, policyRecentlyUsedCurrencies]); + }, [currencyList, searchValue, translate, initiallySelectedCurrencyCode, selectedCurrencies, getUnselectedOptions, policyRecentlyUsedCurrencies]); return ( Date: Fri, 6 Sep 2024 02:37:30 +0700 Subject: [PATCH 14/21] use nvp_recentlyUsedCurrencies for all --- src/ONYXKEYS.ts | 7 +++--- src/ROUTES.ts | 7 ++---- .../CurrencySelectionList/index.tsx | 6 ++--- src/components/CurrencySelectionList/types.ts | 2 +- src/libs/Navigation/types.ts | 1 - src/libs/actions/IOU.ts | 24 +++++++++---------- src/libs/actions/Policy/Policy.ts | 17 ++++--------- .../iou/request/step/IOURequestStepAmount.tsx | 11 +-------- .../request/step/IOURequestStepCurrency.tsx | 12 +++++----- .../step/IOURequestStepTaxAmountPage.tsx | 1 - src/types/onyx/RecentlyUsedCurrencies.ts | 4 ---- src/types/onyx/index.ts | 2 -- 12 files changed, 33 insertions(+), 61 deletions(-) delete mode 100644 src/types/onyx/RecentlyUsedCurrencies.ts diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index eff42c1680f4..69090d622e5b 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -409,6 +409,9 @@ const ONYXKEYS = { /** Stores the route to open after changing app permission from settings */ LAST_ROUTE: 'lastRoute', + /** Stores recently used currencies */ + RECENTLY_USED_CURRENCIES: 'nvp_recentlyUsedCurrencies', + /** Collection Keys */ COLLECTION: { DOWNLOAD: 'download_', @@ -420,7 +423,6 @@ const ONYXKEYS = { POLICY_RECENTLY_USED_CATEGORIES: 'policyRecentlyUsedCategories_', POLICY_TAGS: 'policyTags_', POLICY_RECENTLY_USED_TAGS: 'nvp_recentlyUsedTags_', - POLICY_RECENTLY_USED_CURRENCIES: 'nvp_recentlyUsedCurrencies_', // Whether the policy's connection data was attempted to be fetched in // the current user session. As this state only exists client-side, it // should not be included as part of the policy object. The policy @@ -772,7 +774,6 @@ type OnyxCollectionValuesMapping = { [ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS]: OnyxTypes.TransactionViolations; [ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT]: OnyxTypes.Transaction; [ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS]: OnyxTypes.RecentlyUsedTags; - [ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES]: OnyxTypes.RecentlyUsedCurrencies; [ONYXKEYS.COLLECTION.OLD_POLICY_RECENTLY_USED_TAGS]: OnyxTypes.RecentlyUsedTags; [ONYXKEYS.COLLECTION.SELECTED_TAB]: OnyxTypes.SelectedTabRequest; [ONYXKEYS.COLLECTION.PRIVATE_NOTES_DRAFT]: string; @@ -799,7 +800,7 @@ type OnyxValuesMapping = { // ONYXKEYS.NVP_TRYNEWDOT is HybridApp onboarding data [ONYXKEYS.NVP_TRYNEWDOT]: OnyxTypes.TryNewDot; - + [ONYXKEYS.RECENTLY_USED_CURRENCIES]: string[]; [ONYXKEYS.ACTIVE_CLIENTS]: string[]; [ONYXKEYS.DEVICE_ID]: string; [ONYXKEYS.IS_SIDEBAR_LOADED]: boolean; diff --git a/src/ROUTES.ts b/src/ROUTES.ts index 5e994514e55b..f67298828d7d 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -422,11 +422,8 @@ const ROUTES = { }, MONEY_REQUEST_STEP_CURRENCY: { route: ':action/:iouType/currency/:transactionID/:reportID/:pageIndex?', - getRoute: (action: IOUAction, iouType: IOUType, transactionID: string, reportID: string, pageIndex = '', currency = '', backTo = '', policyID?: string) => - getUrlWithBackToParam( - `${action as string}/${iouType as string}/currency/${transactionID}/${reportID}/${pageIndex}?currency=${currency}${`${policyID ? `&policyID=${policyID}` : ''}`}`, - backTo, - ), + getRoute: (action: IOUAction, iouType: IOUType, transactionID: string, reportID: string, pageIndex = '', currency = '', backTo = '') => + getUrlWithBackToParam(`${action as string}/${iouType as string}/currency/${transactionID}/${reportID}/${pageIndex}?currency=${currency}`, backTo), }, MONEY_REQUEST_STEP_DATE: { route: ':action/:iouType/date/:transactionID/:reportID/:reportActionID?', diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 82b8f112e188..b99c130bc91f 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -18,7 +18,7 @@ function CurrencySelectionList({ currencyList, selectedCurrencies = [], canSelectMultiple = false, - policyRecentlyUsedCurrencies, + recentlyUsedCurrencies, }: CurrencySelectionListProps) { const [searchValue, setSearchValue] = useState(''); const {translate} = useLocalize(); @@ -39,7 +39,7 @@ function CurrencySelectionList({ }, [] as CurrencyListItem[]); const policyRecentlyUsedCurrencyOptions: CurrencyListItem[] = - policyRecentlyUsedCurrencies?.map((currencyCode) => { + recentlyUsedCurrencies?.map((currencyCode) => { const currencyInfo = currencyList?.[currencyCode]; const isSelectedCurrency = currencyCode === initiallySelectedCurrencyCode; return { @@ -87,7 +87,7 @@ function CurrencySelectionList({ } return {sections: result, headerMessage: isEmpty ? translate('common.noResultsFound') : ''}; - }, [currencyList, searchValue, translate, initiallySelectedCurrencyCode, selectedCurrencies, getUnselectedOptions, policyRecentlyUsedCurrencies]); + }, [currencyList, searchValue, translate, initiallySelectedCurrencyCode, selectedCurrencies, getUnselectedOptions, recentlyUsedCurrencies]); return ( void; diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index c6953d8c00ba..c8c2c0f0e41d 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -955,7 +955,6 @@ type MoneyRequestNavigatorParamList = { pageIndex?: string; backTo?: Routes; currency?: string; - policyID?: string; }; }; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c555402cba93..c8a40dfc2a11 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -636,10 +636,9 @@ function buildOnyxDataForMoneyRequest( } if (optimisticPolicyRecentlyUsedCurrencies?.length) { - const personalPolicy = PolicyUtils.getPersonalPolicy(); optimisticData.push({ onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${iouReport.policyID ?? personalPolicy?.id}`, + key: ONYXKEYS.RECENTLY_USED_CURRENCIES, value: optimisticPolicyRecentlyUsedCurrencies, }); } @@ -1014,10 +1013,9 @@ function buildOnyxDataForInvoice( } if (optimisticPolicyRecentlyUsedCurrencies?.length) { - const personalPolicy = PolicyUtils.getPersonalPolicy(); optimisticData.push({ onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${iouReport.policyID ?? personalPolicy?.id}`, + key: ONYXKEYS.RECENTLY_USED_CURRENCIES, value: optimisticPolicyRecentlyUsedCurrencies, }); } @@ -1910,7 +1908,7 @@ function getSendInvoiceInformation( const optimisticPolicyRecentlyUsedCategories = Category.buildOptimisticPolicyRecentlyUsedCategories(optimisticInvoiceReport.policyID, category); const optimisticPolicyRecentlyUsedTags = Tag.buildOptimisticPolicyRecentlyUsedTags(optimisticInvoiceReport.policyID, tag); - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(optimisticInvoiceReport.policyID, currency); + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(currency); // STEP 4: Add optimistic personal details for participant const shouldCreateOptimisticPersonalDetails = isNewChatReport && !allPersonalDetails[receiverAccountID]; @@ -2091,7 +2089,7 @@ function getMoneyRequestInformation( const optimisticPolicyRecentlyUsedCategories = Category.buildOptimisticPolicyRecentlyUsedCategories(iouReport.policyID, category); const optimisticPolicyRecentlyUsedTags = Tag.buildOptimisticPolicyRecentlyUsedTags(iouReport.policyID, tag); - const optimisticPolicyRecentluUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(iouReport.policyID, currency); + const optimisticPolicyRecentluUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(currency); // If there is an existing transaction (which is the case for distance requests), then the data from the existing transaction // needs to be manually merged into the optimistic transaction. This is because buildOnyxDataForMoneyRequest() uses `Onyx.set()` for the transaction @@ -2688,11 +2686,11 @@ function getUpdateMoneyRequestParams( // Update recently used currencies if the category is changed if ('currency' in transactionChanges) { - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(iouReport?.policyID, transactionChanges.currency); + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(transactionChanges.currency); if (optimisticPolicyRecentlyUsedCurrencies.length) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${iouReport?.policyID}`, + key: ONYXKEYS.RECENTLY_USED_CURRENCIES, value: optimisticPolicyRecentlyUsedCurrencies, }); } @@ -4256,7 +4254,7 @@ function createSplitsAndOnyxData( const optimisticPolicyRecentlyUsedCategories = isPolicyExpenseChat ? Category.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category) : []; // Add currency to optimistic policy recently used currencies when a participant is a workspace - const optimisticPolicyRecentlyUsedCurrencies = isPolicyExpenseChat ? Policy.buildOptimisticPolicyRecentlyUsedCurrencies(participant.policyID, currency) : []; + const optimisticPolicyRecentlyUsedCurrencies = isPolicyExpenseChat ? Policy.buildOptimisticPolicyRecentlyUsedCurrencies(currency) : []; // Add tag to optimistic policy recently used tags when a participant is a workspace const optimisticPolicyRecentlyUsedTags = isPolicyExpenseChat ? Tag.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag) : {}; @@ -4750,7 +4748,7 @@ function startSplitBill({ const optimisticPolicyRecentlyUsedCategories = Category.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category); const optimisticPolicyRecentlyUsedTags = Tag.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag); - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(participant.policyID, currency); + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(currency); if (optimisticPolicyRecentlyUsedCategories.length > 0) { optimisticData.push({ @@ -4763,7 +4761,7 @@ function startSplitBill({ if (optimisticPolicyRecentlyUsedCurrencies.length > 0) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${participant.policyID}`, + key: ONYXKEYS.RECENTLY_USED_CURRENCIES, value: optimisticPolicyRecentlyUsedCurrencies, }); } @@ -5354,11 +5352,11 @@ function editRegularMoneyRequest( // Update recently used currencies if the category is changed if ('currency' in transactionChanges) { - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(iouReport?.policyID, transactionChanges.currency); + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(transactionChanges.currency); if (optimisticPolicyRecentlyUsedCurrencies.length) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${iouReport?.policyID}`, + key: ONYXKEYS.RECENTLY_USED_CURRENCIES, value: optimisticPolicyRecentlyUsedCurrencies, }); } diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 4451f05f8290..6510ce0352ab 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -183,9 +183,9 @@ Onyx.connect({ callback: (val) => (reimbursementAccount = val), }); -let allRecentlyUsedCurrencies: OnyxCollection = {}; +let allRecentlyUsedCurrencies: string[]; Onyx.connect({ - key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES, + key: ONYXKEYS.RECENTLY_USED_CURRENCIES, waitForCollectionCallback: true, callback: (val) => (allRecentlyUsedCurrencies = val), }); @@ -2216,19 +2216,12 @@ function dismissAddedWithPrimaryLoginMessages(policyID: string) { Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {primaryLoginsInvited: null}); } -function buildOptimisticPolicyRecentlyUsedCurrencies(policyID?: string, currency?: string) { +function buildOptimisticPolicyRecentlyUsedCurrencies(currency?: string) { if (!currency) { return []; } - const personalPolicy = PolicyUtils.getPersonalPolicy(); - const personalPolicyID = personalPolicy?.id; - if (!policyID && !personalPolicyID) { - return []; - } - const policyRecentlyUsedCurrencies = policyID - ? allRecentlyUsedCurrencies?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${policyID}`] - : allRecentlyUsedCurrencies?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${personalPolicyID}`]; - return lodashUnion([currency], policyRecentlyUsedCurrencies); + + return lodashUnion([currency], allRecentlyUsedCurrencies); } /** diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index a292d6b81dd2..069f86ce0899 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -134,16 +134,7 @@ function IOURequestStepAmount({ const navigateToCurrencySelectionPage = () => { Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CURRENCY.getRoute( - action, - iouType, - transactionID, - reportID, - backTo ? 'confirm' : '', - currency, - Navigation.getActiveRouteWithoutParams(), - policy?.id ?? PolicyUtils.getPersonalPolicy()?.id, - ), + ROUTES.MONEY_REQUEST_STEP_CURRENCY.getRoute(action, iouType, transactionID, reportID, backTo ? 'confirm' : '', currency, Navigation.getActiveRouteWithoutParams()), ); }; diff --git a/src/pages/iou/request/step/IOURequestStepCurrency.tsx b/src/pages/iou/request/step/IOURequestStepCurrency.tsx index 0d2a60e8c1b5..24f47a91c18b 100644 --- a/src/pages/iou/request/step/IOURequestStepCurrency.tsx +++ b/src/pages/iou/request/step/IOURequestStepCurrency.tsx @@ -14,7 +14,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES, {getUrlWithBackToParam} from '@src/ROUTES'; import type {Route} from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; -import type {RecentlyUsedCurrencies, Transaction} from '@src/types/onyx'; +import type {Transaction} from '@src/types/onyx'; import StepScreenWrapper from './StepScreenWrapper'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; @@ -23,7 +23,7 @@ type IOURequestStepCurrencyOnyxProps = { /** The draft transaction object being modified in Onyx */ draftTransaction: OnyxEntry; /** List of recently used currencies */ - policyRecentlyUsedCurrencies: OnyxEntry; + recentlyUsedCurrencies: string[]; }; type IOURequestStepCurrencyProps = IOURequestStepCurrencyOnyxProps & WithFullTransactionOrNotFoundProps; @@ -33,7 +33,7 @@ function IOURequestStepCurrency({ params: {backTo, iouType, pageIndex, reportID, transactionID, action, currency: selectedCurrency = ''}, }, draftTransaction, - policyRecentlyUsedCurrencies, + recentlyUsedCurrencies, }: IOURequestStepCurrencyProps) { const {translate} = useLocalize(); const {currency: originalCurrency = ''} = ReportUtils.getTransactionDetails(draftTransaction) ?? {}; @@ -78,7 +78,7 @@ function IOURequestStepCurrency({ > {({didScreenTransitionEnd}) => ( { if (!didScreenTransitionEnd) { @@ -102,8 +102,8 @@ const IOURequestStepCurrencyWithOnyx = withOnyx `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CURRENCIES}${route.params.policyID ?? 0}`, + recentlyUsedCurrencies: { + key: ONYXKEYS.RECENTLY_USED_CURRENCIES, }, })(IOURequestStepCurrency); diff --git a/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx b/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx index 9747a5407c4d..b3ed65be34a6 100644 --- a/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx +++ b/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx @@ -104,7 +104,6 @@ function IOURequestStepTaxAmountPage({ backTo ? 'confirm' : '', currency, Navigation.getActiveRouteWithoutParams(), - policy?.id, ), ); }; diff --git a/src/types/onyx/RecentlyUsedCurrencies.ts b/src/types/onyx/RecentlyUsedCurrencies.ts deleted file mode 100644 index 9e8ee7f9b6e9..000000000000 --- a/src/types/onyx/RecentlyUsedCurrencies.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** Policy currencies that have been recently used for each policy */ -type RecentlyUsedCurrencies = string[]; - -export default RecentlyUsedCurrencies; diff --git a/src/types/onyx/index.ts b/src/types/onyx/index.ts index 16e5b804918c..0073e47bb65c 100644 --- a/src/types/onyx/index.ts +++ b/src/types/onyx/index.ts @@ -61,7 +61,6 @@ import type PrivatePersonalDetails from './PrivatePersonalDetails'; import type PrivateSubscription from './PrivateSubscription'; import type QuickAction from './QuickAction'; import type RecentlyUsedCategories from './RecentlyUsedCategories'; -import type RecentlyUsedCurrencies from './RecentlyUsedCurrencies'; import type RecentlyUsedReportFields from './RecentlyUsedReportFields'; import type RecentlyUsedTags from './RecentlyUsedTags'; import type RecentWaypoint from './RecentWaypoint'; @@ -166,7 +165,6 @@ export type { RecentWaypoint, RecentlyUsedCategories, RecentlyUsedTags, - RecentlyUsedCurrencies, ReimbursementAccount, Report, ReportNameValuePairs, From 2125f7138b1cd376d9cb143225d2befabd01a4fe Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Fri, 6 Sep 2024 02:48:03 +0700 Subject: [PATCH 15/21] fix type --- src/libs/actions/Policy/Policy.ts | 4 +--- src/pages/iou/request/step/IOURequestStepCurrency.tsx | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 6510ce0352ab..580462fa55b6 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -73,7 +73,6 @@ import type { PersonalDetailsList, Policy, PolicyCategory, - RecentlyUsedCurrencies, ReimbursementAccount, Report, ReportAction, @@ -186,8 +185,7 @@ Onyx.connect({ let allRecentlyUsedCurrencies: string[]; Onyx.connect({ key: ONYXKEYS.RECENTLY_USED_CURRENCIES, - waitForCollectionCallback: true, - callback: (val) => (allRecentlyUsedCurrencies = val), + callback: (val) => (allRecentlyUsedCurrencies = val ?? []), }); /** diff --git a/src/pages/iou/request/step/IOURequestStepCurrency.tsx b/src/pages/iou/request/step/IOURequestStepCurrency.tsx index 24f47a91c18b..b51d6a8998a4 100644 --- a/src/pages/iou/request/step/IOURequestStepCurrency.tsx +++ b/src/pages/iou/request/step/IOURequestStepCurrency.tsx @@ -23,7 +23,7 @@ type IOURequestStepCurrencyOnyxProps = { /** The draft transaction object being modified in Onyx */ draftTransaction: OnyxEntry; /** List of recently used currencies */ - recentlyUsedCurrencies: string[]; + recentlyUsedCurrencies: OnyxEntry; }; type IOURequestStepCurrencyProps = IOURequestStepCurrencyOnyxProps & WithFullTransactionOrNotFoundProps; From 381d5b9b5efcba0b7ad4c8c607719ac673ee4a00 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Mon, 9 Sep 2024 14:07:39 +0700 Subject: [PATCH 16/21] fix: use Array.isArray check --- .../CurrencySelectionList/index.tsx | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index b99c130bc91f..3a601375a152 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -38,18 +38,19 @@ function CurrencySelectionList({ return acc; }, [] as CurrencyListItem[]); - const policyRecentlyUsedCurrencyOptions: CurrencyListItem[] = - recentlyUsedCurrencies?.map((currencyCode) => { - const currencyInfo = currencyList?.[currencyCode]; - const isSelectedCurrency = currencyCode === initiallySelectedCurrencyCode; - return { - currencyName: currencyInfo?.name ?? '', - text: `${currencyCode} - ${CurrencyUtils.getCurrencySymbol(currencyCode)}`, - currencyCode, - keyForList: currencyCode, - isSelected: isSelectedCurrency, - }; - }) ?? []; + const policyRecentlyUsedCurrencyOptions: CurrencyListItem[] = Array.isArray(recentlyUsedCurrencies) + ? recentlyUsedCurrencies?.map((currencyCode) => { + const currencyInfo = currencyList?.[currencyCode]; + const isSelectedCurrency = currencyCode === initiallySelectedCurrencyCode; + return { + currencyName: currencyInfo?.name ?? '', + text: `${currencyCode} - ${CurrencyUtils.getCurrencySymbol(currencyCode)}`, + currencyCode, + keyForList: currencyCode, + isSelected: isSelectedCurrency, + }; + }) + : []; const searchRegex = new RegExp(Str.escapeForRegExp(searchValue.trim()), 'i'); const filteredCurrencies = currencyOptions.filter((currencyOption) => searchRegex.test(currencyOption.text ?? '') || searchRegex.test(currencyOption.currencyName)); From c4bc961d72b91b1e6b19dbfa58cddad8582ffcc5 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Tue, 10 Sep 2024 19:55:46 +0700 Subject: [PATCH 17/21] fix rename buildOptimisticRecentlyUsedCurrencies function --- src/libs/actions/IOU.ts | 13 ++++++------- src/libs/actions/Policy/Policy.ts | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c8a40dfc2a11..eb415ff9f2a6 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1908,7 +1908,7 @@ function getSendInvoiceInformation( const optimisticPolicyRecentlyUsedCategories = Category.buildOptimisticPolicyRecentlyUsedCategories(optimisticInvoiceReport.policyID, category); const optimisticPolicyRecentlyUsedTags = Tag.buildOptimisticPolicyRecentlyUsedTags(optimisticInvoiceReport.policyID, tag); - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(currency); + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); // STEP 4: Add optimistic personal details for participant const shouldCreateOptimisticPersonalDetails = isNewChatReport && !allPersonalDetails[receiverAccountID]; @@ -2089,7 +2089,7 @@ function getMoneyRequestInformation( const optimisticPolicyRecentlyUsedCategories = Category.buildOptimisticPolicyRecentlyUsedCategories(iouReport.policyID, category); const optimisticPolicyRecentlyUsedTags = Tag.buildOptimisticPolicyRecentlyUsedTags(iouReport.policyID, tag); - const optimisticPolicyRecentluUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(currency); + const optimisticPolicyRecentluUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); // If there is an existing transaction (which is the case for distance requests), then the data from the existing transaction // needs to be manually merged into the optimistic transaction. This is because buildOnyxDataForMoneyRequest() uses `Onyx.set()` for the transaction @@ -2686,7 +2686,7 @@ function getUpdateMoneyRequestParams( // Update recently used currencies if the category is changed if ('currency' in transactionChanges) { - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(transactionChanges.currency); + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(transactionChanges.currency); if (optimisticPolicyRecentlyUsedCurrencies.length) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, @@ -4253,8 +4253,7 @@ function createSplitsAndOnyxData( // Add category to optimistic policy recently used categories when a participant is a workspace const optimisticPolicyRecentlyUsedCategories = isPolicyExpenseChat ? Category.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category) : []; - // Add currency to optimistic policy recently used currencies when a participant is a workspace - const optimisticPolicyRecentlyUsedCurrencies = isPolicyExpenseChat ? Policy.buildOptimisticPolicyRecentlyUsedCurrencies(currency) : []; + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); // Add tag to optimistic policy recently used tags when a participant is a workspace const optimisticPolicyRecentlyUsedTags = isPolicyExpenseChat ? Tag.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag) : {}; @@ -4748,7 +4747,7 @@ function startSplitBill({ const optimisticPolicyRecentlyUsedCategories = Category.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category); const optimisticPolicyRecentlyUsedTags = Tag.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag); - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(currency); + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); if (optimisticPolicyRecentlyUsedCategories.length > 0) { optimisticData.push({ @@ -5352,7 +5351,7 @@ function editRegularMoneyRequest( // Update recently used currencies if the category is changed if ('currency' in transactionChanges) { - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticPolicyRecentlyUsedCurrencies(transactionChanges.currency); + const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(transactionChanges.currency); if (optimisticPolicyRecentlyUsedCurrencies.length) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 580462fa55b6..945984ff8d28 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -2214,7 +2214,7 @@ function dismissAddedWithPrimaryLoginMessages(policyID: string) { Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {primaryLoginsInvited: null}); } -function buildOptimisticPolicyRecentlyUsedCurrencies(currency?: string) { +function buildOptimisticRecentlyUsedCurrencies(currency?: string) { if (!currency) { return []; } @@ -3999,7 +3999,7 @@ export { dismissAddedWithPrimaryLoginMessages, openDraftWorkspaceRequest, createDraftInitialWorkspace, - buildOptimisticPolicyRecentlyUsedCurrencies, + buildOptimisticRecentlyUsedCurrencies, setWorkspaceInviteMessageDraft, setWorkspaceApprovalMode, setWorkspaceAutoReportingFrequency, From cdeafbde649d02d9aac89b3f64de75ed3a4e4e3f Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Tue, 10 Sep 2024 20:04:15 +0700 Subject: [PATCH 18/21] fix rename variable --- src/libs/actions/IOU.ts | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index eb415ff9f2a6..7142276d3b62 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -509,7 +509,7 @@ function buildOnyxDataForMoneyRequest( optimisticNextStep?: OnyxTypes.ReportNextStep | null, isOneOnOneSplit = false, existingTransactionThreadReportID?: string, - optimisticPolicyRecentlyUsedCurrencies?: string[], + optimisticRecentlyUsedCurrencies?: string[], ): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] { const isScanRequest = TransactionUtils.isScanRequest(transaction); const outstandingChildRequest = ReportUtils.getOutstandingChildRequest(iouReport); @@ -635,11 +635,11 @@ function buildOnyxDataForMoneyRequest( }); } - if (optimisticPolicyRecentlyUsedCurrencies?.length) { + if (optimisticRecentlyUsedCurrencies?.length) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, key: ONYXKEYS.RECENTLY_USED_CURRENCIES, - value: optimisticPolicyRecentlyUsedCurrencies, + value: optimisticRecentlyUsedCurrencies, }); } @@ -921,7 +921,7 @@ function buildOnyxDataForInvoice( policy?: OnyxEntry, policyTagList?: OnyxEntry, policyCategories?: OnyxEntry, - optimisticPolicyRecentlyUsedCurrencies?: string[], + optimisticRecentlyUsedCurrencies?: string[], companyName?: string, companyWebsite?: string, ): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] { @@ -1012,11 +1012,11 @@ function buildOnyxDataForInvoice( }); } - if (optimisticPolicyRecentlyUsedCurrencies?.length) { + if (optimisticRecentlyUsedCurrencies?.length) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, key: ONYXKEYS.RECENTLY_USED_CURRENCIES, - value: optimisticPolicyRecentlyUsedCurrencies, + value: optimisticRecentlyUsedCurrencies, }); } @@ -1908,7 +1908,7 @@ function getSendInvoiceInformation( const optimisticPolicyRecentlyUsedCategories = Category.buildOptimisticPolicyRecentlyUsedCategories(optimisticInvoiceReport.policyID, category); const optimisticPolicyRecentlyUsedTags = Tag.buildOptimisticPolicyRecentlyUsedTags(optimisticInvoiceReport.policyID, tag); - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); + const optimisticRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); // STEP 4: Add optimistic personal details for participant const shouldCreateOptimisticPersonalDetails = isNewChatReport && !allPersonalDetails[receiverAccountID]; @@ -1962,7 +1962,7 @@ function getSendInvoiceInformation( policy, policyTagList, policyCategories, - optimisticPolicyRecentlyUsedCurrencies, + optimisticRecentlyUsedCurrencies, companyName, companyWebsite, ); @@ -2686,12 +2686,12 @@ function getUpdateMoneyRequestParams( // Update recently used currencies if the category is changed if ('currency' in transactionChanges) { - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(transactionChanges.currency); - if (optimisticPolicyRecentlyUsedCurrencies.length) { + const optimisticRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(transactionChanges.currency); + if (optimisticRecentlyUsedCurrencies.length) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, key: ONYXKEYS.RECENTLY_USED_CURRENCIES, - value: optimisticPolicyRecentlyUsedCurrencies, + value: optimisticRecentlyUsedCurrencies, }); } } @@ -4253,7 +4253,7 @@ function createSplitsAndOnyxData( // Add category to optimistic policy recently used categories when a participant is a workspace const optimisticPolicyRecentlyUsedCategories = isPolicyExpenseChat ? Category.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category) : []; - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); + const optimisticRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); // Add tag to optimistic policy recently used tags when a participant is a workspace const optimisticPolicyRecentlyUsedTags = isPolicyExpenseChat ? Tag.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag) : {}; @@ -4280,7 +4280,7 @@ function createSplitsAndOnyxData( null, true, undefined, - optimisticPolicyRecentlyUsedCurrencies, + optimisticRecentlyUsedCurrencies, ); const individualSplit = { @@ -4747,7 +4747,7 @@ function startSplitBill({ const optimisticPolicyRecentlyUsedCategories = Category.buildOptimisticPolicyRecentlyUsedCategories(participant.policyID, category); const optimisticPolicyRecentlyUsedTags = Tag.buildOptimisticPolicyRecentlyUsedTags(participant.policyID, tag); - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); + const optimisticRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(currency); if (optimisticPolicyRecentlyUsedCategories.length > 0) { optimisticData.push({ @@ -4757,11 +4757,11 @@ function startSplitBill({ }); } - if (optimisticPolicyRecentlyUsedCurrencies.length > 0) { + if (optimisticRecentlyUsedCurrencies.length > 0) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, key: ONYXKEYS.RECENTLY_USED_CURRENCIES, - value: optimisticPolicyRecentlyUsedCurrencies, + value: optimisticRecentlyUsedCurrencies, }); } @@ -5351,12 +5351,12 @@ function editRegularMoneyRequest( // Update recently used currencies if the category is changed if ('currency' in transactionChanges) { - const optimisticPolicyRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(transactionChanges.currency); - if (optimisticPolicyRecentlyUsedCurrencies.length) { + const optimisticRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(transactionChanges.currency); + if (optimisticRecentlyUsedCurrencies.length) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, key: ONYXKEYS.RECENTLY_USED_CURRENCIES, - value: optimisticPolicyRecentlyUsedCurrencies, + value: optimisticRecentlyUsedCurrencies, }); } } From 6acba93a61c86cac045ae2c7343df39c3542663a Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Wed, 11 Sep 2024 15:58:19 +0700 Subject: [PATCH 19/21] fix rename variable --- src/components/CurrencySelectionList/index.tsx | 8 ++++---- src/libs/actions/IOU.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 3a601375a152..6f6ce6fc40fd 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -38,7 +38,7 @@ function CurrencySelectionList({ return acc; }, [] as CurrencyListItem[]); - const policyRecentlyUsedCurrencyOptions: CurrencyListItem[] = Array.isArray(recentlyUsedCurrencies) + const recentlyUsedCurrencyOptions: CurrencyListItem[] = Array.isArray(recentlyUsedCurrencies) ? recentlyUsedCurrencies?.map((currencyCode) => { const currencyInfo = currencyList?.[currencyCode]; const isSelectedCurrency = currencyCode === initiallySelectedCurrencyCode; @@ -55,7 +55,7 @@ function CurrencySelectionList({ const searchRegex = new RegExp(Str.escapeForRegExp(searchValue.trim()), 'i'); const filteredCurrencies = currencyOptions.filter((currencyOption) => searchRegex.test(currencyOption.text ?? '') || searchRegex.test(currencyOption.currencyName)); const isEmpty = searchValue.trim() && !filteredCurrencies.length; - const shouldDisplayRecentlyOptions = !isEmptyObject(policyRecentlyUsedCurrencyOptions) && !searchValue; + const shouldDisplayRecentlyOptions = !isEmptyObject(recentlyUsedCurrencyOptions) && !searchValue; const selectedOptions = filteredCurrencies.filter((option) => option.isSelected); const shouldDisplaySelectedOptionOnTop = selectedOptions.length > 0; const unselectedOptions = getUnselectedOptions(filteredCurrencies); @@ -70,12 +70,12 @@ function CurrencySelectionList({ } if (shouldDisplayRecentlyOptions) { - const cutPolicyRecentlyUsedCurrencyOptions = policyRecentlyUsedCurrencyOptions.slice(0, CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW); + const cutRecentlyUsedCurrencyOptions = recentlyUsedCurrencyOptions.slice(0, CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW); if (!isEmpty) { result.push( { title: translate('common.recents'), - data: shouldDisplaySelectedOptionOnTop ? getUnselectedOptions(cutPolicyRecentlyUsedCurrencyOptions) : cutPolicyRecentlyUsedCurrencyOptions, + data: shouldDisplaySelectedOptionOnTop ? getUnselectedOptions(cutRecentlyUsedCurrencyOptions) : cutRecentlyUsedCurrencyOptions, shouldShow: shouldDisplayRecentlyOptions, }, {title: translate('common.all'), data: shouldDisplayRecentlyOptions ? unselectedOptions : filteredCurrencies}, diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 7142276d3b62..10f5d157ce24 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2684,7 +2684,7 @@ function getUpdateMoneyRequestParams( } } - // Update recently used currencies if the category is changed + // Update recently used currencies if the currency is changed if ('currency' in transactionChanges) { const optimisticRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(transactionChanges.currency); if (optimisticRecentlyUsedCurrencies.length) { @@ -5349,7 +5349,7 @@ function editRegularMoneyRequest( } } - // Update recently used currencies if the category is changed + // Update recently used currencies if the currency is changed if ('currency' in transactionChanges) { const optimisticRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(transactionChanges.currency); if (optimisticRecentlyUsedCurrencies.length) { From d9d65fa64a399c2a91f9ae0b7f8acc69821bf754 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Thu, 12 Sep 2024 14:07:41 +0700 Subject: [PATCH 20/21] fix update buildOptimisticRecentlyUsedCurrencies --- src/components/CurrencySelectionList/index.tsx | 3 +-- src/libs/actions/Policy/Policy.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 6f6ce6fc40fd..216e655c4ce7 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -70,12 +70,11 @@ function CurrencySelectionList({ } if (shouldDisplayRecentlyOptions) { - const cutRecentlyUsedCurrencyOptions = recentlyUsedCurrencyOptions.slice(0, CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW); if (!isEmpty) { result.push( { title: translate('common.recents'), - data: shouldDisplaySelectedOptionOnTop ? getUnselectedOptions(cutRecentlyUsedCurrencyOptions) : cutRecentlyUsedCurrencyOptions, + data: shouldDisplaySelectedOptionOnTop ? getUnselectedOptions(recentlyUsedCurrencyOptions) : recentlyUsedCurrencyOptions, shouldShow: shouldDisplayRecentlyOptions, }, {title: translate('common.all'), data: shouldDisplayRecentlyOptions ? unselectedOptions : filteredCurrencies}, diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 945984ff8d28..ba02576da039 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -2219,7 +2219,7 @@ function buildOptimisticRecentlyUsedCurrencies(currency?: string) { return []; } - return lodashUnion([currency], allRecentlyUsedCurrencies); + return lodashUnion([currency], allRecentlyUsedCurrencies).slice(0, CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW); } /** From 1f7939c26b97b914939cef74de3f8dcef64d5c40 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Thu, 12 Sep 2024 14:19:08 +0700 Subject: [PATCH 21/21] fix lint --- src/components/CurrencySelectionList/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/CurrencySelectionList/index.tsx b/src/components/CurrencySelectionList/index.tsx index 216e655c4ce7..201ed7bab730 100644 --- a/src/components/CurrencySelectionList/index.tsx +++ b/src/components/CurrencySelectionList/index.tsx @@ -6,7 +6,6 @@ import RadioListItem from '@components/SelectionList/RadioListItem'; import SelectableListItem from '@components/SelectionList/SelectableListItem'; import useLocalize from '@hooks/useLocalize'; import * as CurrencyUtils from '@libs/CurrencyUtils'; -import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type {CurrencyListItem, CurrencySelectionListOnyxProps, CurrencySelectionListProps} from './types';