11import React , { useCallback , useEffect , useState } from 'react' ;
22import { InteractionManager } from 'react-native' ;
3+ import Onyx from 'react-native-onyx' ;
34import AttachmentCarouselView from '@components/Attachments/AttachmentCarousel/AttachmentCarouselView' ;
45import useCarouselArrows from '@components/Attachments/AttachmentCarousel/useCarouselArrows' ;
56import useAttachmentErrors from '@components/Attachments/AttachmentView/useAttachmentErrors' ;
@@ -12,8 +13,9 @@ import useLocalize from '@hooks/useLocalize';
1213import useOnyx from '@hooks/useOnyx' ;
1314import useThemeStyles from '@hooks/useThemeStyles' ;
1415import Navigation from '@libs/Navigation/Navigation' ;
16+ import { getTransactionOrDraftTransaction } from '@libs/TransactionUtils' ;
1517import type { ReceiptFile } from '@pages/iou/request/step/IOURequestStepScan/types' ;
16- import { removeTransactionReceipt } from '@userActions/TransactionEdit' ;
18+ import { removeDraftTransaction , removeTransactionReceipt , updateDraftTransactions } from '@userActions/TransactionEdit' ;
1719import CONST from '@src/CONST' ;
1820import ONYXKEYS from '@src/ONYXKEYS' ;
1921import type { Route } from '@src/ROUTES' ;
@@ -67,11 +69,39 @@ function ReceiptViewModal({route}: ReceiptViewModalProps) {
6769 }
6870
6971 InteractionManager . runAfterInteractions ( ( ) => {
70- removeTransactionReceipt ( currentReceipt . transactionID ) ;
72+ if ( currentReceipt . transactionID === CONST . IOU . OPTIMISTIC_TRANSACTION_ID ) {
73+ if ( receipts . length === 1 ) {
74+ removeTransactionReceipt ( currentReceipt . transactionID ) ;
75+ return ;
76+ }
77+
78+ const secondTransactionID = receipts . at ( 1 ) ?. transactionID ;
79+ const secondTransaction = secondTransactionID ? getTransactionOrDraftTransaction ( secondTransactionID ) : undefined ;
80+
81+ if ( secondTransaction ) {
82+ updateDraftTransactions ( [
83+ {
84+ onyxMethod : Onyx . METHOD . SET ,
85+ key : `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ CONST . IOU . OPTIMISTIC_TRANSACTION_ID } ` ,
86+ value : {
87+ ...secondTransaction ,
88+ transactionID : CONST . IOU . OPTIMISTIC_TRANSACTION_ID ,
89+ } ,
90+ } ,
91+ {
92+ onyxMethod : Onyx . METHOD . MERGE ,
93+ key : `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ secondTransactionID } ` ,
94+ value : null ,
95+ } ,
96+ ] ) ;
97+ }
98+ return ;
99+ }
100+ removeDraftTransaction ( currentReceipt . transactionID ) ;
71101 } ) ;
72102
73103 Navigation . goBack ( ) ;
74- } , [ currentReceipt ] ) ;
104+ } , [ currentReceipt , receipts ] ) ;
75105
76106 const handleCloseConfirmModal = ( ) => {
77107 setIsDeleteReceiptConfirmModalVisible ( false ) ;
0 commit comments