Skip to content

Commit 24c8d19

Browse files
Merge pull request Expensify#64636 from truph01/fix/62529
fix: optimistically update message count
2 parents a10a5dc + 33c4d9e commit 24c8d19

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/libs/actions/Search.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Onyx from 'react-native-onyx';
2-
import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
2+
import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
33
import type {ValueOf} from 'type-fest';
44
import type {FormOnyxValues} from '@components/Form/types';
55
import type {PaymentData, SearchQueryJSON} from '@components/Search/types';
@@ -13,6 +13,7 @@ import fileDownload from '@libs/fileDownload';
1313
import enhanceParameters from '@libs/Network/enhanceParameters';
1414
import {rand64} from '@libs/NumberUtils';
1515
import {getPersonalPolicy, getSubmitToAccountID, getValidConnectedIntegration} from '@libs/PolicyUtils';
16+
import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils';
1617
import type {OptimisticExportIntegrationAction} from '@libs/ReportUtils';
1718
import {buildOptimisticExportIntegrationAction, hasHeldExpenses, isExpenseReport, isInvoiceReport, isIOUReport as isIOUReportUtil} from '@libs/ReportUtils';
1819
import type {SearchKey} from '@libs/SearchUIUtils';
@@ -22,7 +23,7 @@ import CONST from '@src/CONST';
2223
import ONYXKEYS from '@src/ONYXKEYS';
2324
import {FILTER_KEYS} from '@src/types/form/SearchAdvancedFiltersForm';
2425
import type {SearchAdvancedFiltersForm} from '@src/types/form/SearchAdvancedFiltersForm';
25-
import type {LastPaymentMethod, LastPaymentMethodType, Policy} from '@src/types/onyx';
26+
import type {LastPaymentMethod, LastPaymentMethodType, Policy, ReportActions, Transaction} from '@src/types/onyx';
2627
import type {PaymentInformation} from '@src/types/onyx/LastPaymentMethod';
2728
import type {ConnectionName} from '@src/types/onyx/Policy';
2829
import type {SearchPolicy, SearchReport, SearchTransaction} from '@src/types/onyx/SearchResults';
@@ -386,8 +387,24 @@ function updateSearchResultsWithTransactionThreadReportID(hash: number, transact
386387
Onyx.merge(`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`, onyxUpdate);
387388
}
388389

389-
function holdMoneyRequestOnSearch(hash: number, transactionIDList: string[], comment: string) {
390+
function holdMoneyRequestOnSearch(hash: number, transactionIDList: string[], comment: string, allTransactions: OnyxCollection<Transaction>, allReportActions: OnyxCollection<ReportActions>) {
390391
const {optimisticData, finallyData} = getOnyxLoadingData(hash);
392+
transactionIDList.forEach((transactionID) => {
393+
const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`];
394+
const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transaction?.reportID}`] ?? {};
395+
const iouReportAction = getIOUActionForTransactionID(Object.values(reportActions ?? {}), transactionID);
396+
if (iouReportAction) {
397+
optimisticData.push({
398+
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transaction?.reportID}`,
399+
onyxMethod: Onyx.METHOD.MERGE,
400+
value: {
401+
[iouReportAction.reportActionID]: {
402+
childVisibleActionCount: (iouReportAction?.childVisibleActionCount ?? 0) + 1,
403+
},
404+
},
405+
});
406+
}
407+
});
391408

392409
API.write(WRITE_COMMANDS.HOLD_MONEY_REQUEST_ON_SEARCH, {hash, transactionIDList, comment}, {optimisticData, finallyData});
393410
}

src/pages/Search/SearchHoldReasonPage.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {useCallback, useEffect} from 'react';
22
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
33
import {useSearchContext} from '@components/Search/SearchContext';
44
import useLocalize from '@hooks/useLocalize';
5+
import useOnyx from '@hooks/useOnyx';
56
import {clearErrorFields, clearErrors} from '@libs/actions/FormActions';
67
import {holdMoneyRequestOnSearch} from '@libs/actions/Search';
78
import Navigation from '@libs/Navigation/Navigation';
@@ -18,20 +19,22 @@ function SearchHoldReasonPage({route}: PlatformStackScreenProps<Omit<SearchRepor
1819
const {translate} = useLocalize();
1920
const {backTo = '', reportID} = route.params ?? {};
2021
const context = useSearchContext();
22+
const [allTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: true});
2123

24+
const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS, {canBeMissing: true});
2225
const onSubmit = useCallback(
2326
({comment}: FormOnyxValues<typeof ONYXKEYS.FORMS.MONEY_REQUEST_HOLD_FORM>) => {
2427
if (route.name === SCREENS.SEARCH.MONEY_REQUEST_REPORT_HOLD_TRANSACTIONS) {
2528
putTransactionsOnHold(context.selectedTransactionIDs, comment, reportID);
2629
context.clearSelectedTransactions(true);
2730
} else {
28-
holdMoneyRequestOnSearch(context.currentSearchHash, Object.keys(context.selectedTransactions), comment);
31+
holdMoneyRequestOnSearch(context.currentSearchHash, Object.keys(context.selectedTransactions), comment, allTransactions, allReportActions);
2932
context.clearSelectedTransactions();
3033
}
3134

3235
Navigation.goBack();
3336
},
34-
[route.name, context, reportID],
37+
[route.name, context, reportID, allTransactions, allReportActions],
3538
);
3639

3740
const validate = useCallback(

0 commit comments

Comments
 (0)