11import { format } from 'date-fns' ;
22import { fastMerge , Str } from 'expensify-common' ;
3+ import cloneDeep from 'lodash/cloneDeep' ;
34import { InteractionManager } from 'react-native' ;
45import type { NullishDeep , OnyxCollection , OnyxEntry , OnyxInputValue , OnyxUpdate } from 'react-native-onyx' ;
56import Onyx from 'react-native-onyx' ;
@@ -7411,7 +7412,7 @@ function updateMoneyRequestAmountAndCurrency({
74117412 * @param reportAction - The reportAction of the transaction in the IOU report
74127413 * @return the url to navigate back once the money request is deleted
74137414 */
7414- function prepareToCleanUpMoneyRequest ( transactionID : string , reportAction : OnyxTypes . ReportAction , shouldRemoveIOUTransactionID = true ) {
7415+ function prepareToCleanUpMoneyRequest ( transactionID : string , reportAction : OnyxTypes . ReportAction , shouldRemoveIOUTransactionID = true , transactionIDsPendingDeletion ?: string [ ] ) {
74157416 // STEP 1: Get all collections we're updating
74167417 const iouReportID = isMoneyRequestAction ( reportAction ) ? getOriginalMessage ( reportAction ) ?. IOUReportID : undefined ;
74177418 const iouReport = allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ iouReportID } ` ] ?? null ;
@@ -7459,12 +7460,12 @@ function prepareToCleanUpMoneyRequest(transactionID: string, reportAction: OnyxT
74597460 canUserPerformWriteAction = ! ! canUserPerformWriteActionReportUtils ( chatReport ) ;
74607461 }
74617462 // If we are deleting the last transaction on a report, then delete the report too
7462- const shouldDeleteIOUReport = getReportTransactions ( iouReportID ) . length === 1 ;
7463+ const shouldDeleteIOUReport = getReportTransactions ( iouReportID ) . filter ( ( trans ) => ! transactionIDsPendingDeletion ?. includes ( trans . transactionID ) ) . length === 1 ;
74637464
74647465 // STEP 4: Update the iouReport and reportPreview with new totals and messages if it wasn't deleted
74657466 let updatedIOUReport : OnyxInputValue < OnyxTypes . Report > ;
74667467 const currency = getCurrency ( transaction ) ;
7467- const updatedReportPreviewAction : Partial < OnyxTypes . ReportAction < typeof CONST . REPORT . ACTIONS . TYPE . REPORT_PREVIEW > > = { ... reportPreviewAction } ;
7468+ const updatedReportPreviewAction : Partial < OnyxTypes . ReportAction < typeof CONST . REPORT . ACTIONS . TYPE . REPORT_PREVIEW > > = cloneDeep ( reportPreviewAction ?? { } ) ;
74687469 updatedReportPreviewAction . pendingAction = shouldDeleteIOUReport ? CONST . RED_BRICK_ROAD_PENDING_ACTION . DELETE : CONST . RED_BRICK_ROAD_PENDING_ACTION . UPDATE ;
74697470 if ( iouReport && isExpenseReport ( iouReport ) ) {
74707471 updatedIOUReport = { ...iouReport } ;
@@ -7775,7 +7776,7 @@ function cleanUpMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repo
77757776 * @param isSingleTransactionView - whether we are in the transaction thread report
77767777 * @return the url to navigate back once the money request is deleted
77777778 */
7778- function deleteMoneyRequest ( transactionID : string | undefined , reportAction : OnyxTypes . ReportAction , isSingleTransactionView = false ) {
7779+ function deleteMoneyRequest ( transactionID : string | undefined , reportAction : OnyxTypes . ReportAction , isSingleTransactionView = false , transactionIDsPendingDeletion ?: string [ ] ) {
77797780 if ( ! transactionID ) {
77807781 return ;
77817782 }
@@ -7794,7 +7795,7 @@ function deleteMoneyRequest(transactionID: string | undefined, reportAction: Ony
77947795 transactionViolations,
77957796 iouReport,
77967797 reportPreviewAction,
7797- } = prepareToCleanUpMoneyRequest ( transactionID , reportAction , false ) ;
7798+ } = prepareToCleanUpMoneyRequest ( transactionID , reportAction , false , transactionIDsPendingDeletion ) ;
77987799
77997800 const urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete ( transactionID , reportAction , isSingleTransactionView ) ;
78007801
0 commit comments