1- import React from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import { View } from 'react-native' ;
33import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView' ;
44import Button from '@components/Button' ;
@@ -20,13 +20,15 @@ import {
2020 getMergeFieldValue ,
2121 getSourceTransactionFromMergeTransaction ,
2222 getTargetTransactionFromMergeTransaction ,
23+ getTransactionThreadReportID ,
2324 isEmptyMergeValue ,
2425} from '@libs/MergeTransactionUtils' ;
2526import type { MergeFieldKey , MergeValue } from '@libs/MergeTransactionUtils' ;
2627import Navigation from '@libs/Navigation/Navigation' ;
2728import type { PlatformStackScreenProps } from '@libs/Navigation/PlatformStackNavigation/types' ;
2829import type { MergeTransactionNavigatorParamList } from '@libs/Navigation/types' ;
2930import { getCurrency } from '@libs/TransactionUtils' ;
31+ import { openReport } from '@userActions/Report' ;
3032import type { TranslationPaths } from '@src/languages/types' ;
3133import ONYXKEYS from '@src/ONYXKEYS' ;
3234import ROUTES from '@src/ROUTES' ;
@@ -42,21 +44,22 @@ function DetailsReviewPage({route}: DetailsReviewPageProps) {
4244 const styles = useThemeStyles ( ) ;
4345 const { transactionID, backTo} = route . params ;
4446
47+ const [ allReports ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT , { canBeMissing : false } ) ;
4548 const [ mergeTransaction , mergeTransactionMetadata ] = useOnyx ( `${ ONYXKEYS . COLLECTION . MERGE_TRANSACTION } ${ transactionID } ` , { canBeMissing : false } ) ;
4649 const [ targetTransaction = getTargetTransactionFromMergeTransaction ( mergeTransaction ) ] = useOnyx ( `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ mergeTransaction ?. targetTransactionID } ` , {
4750 canBeMissing : true ,
4851 } ) ;
4952 const [ sourceTransaction = getSourceTransactionFromMergeTransaction ( mergeTransaction ) ] = useOnyx ( `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ mergeTransaction ?. sourceTransactionID } ` , {
5053 canBeMissing : true ,
5154 } ) ;
55+ const targetTransactionThreadReportID = getTransactionThreadReportID ( targetTransaction ) ;
56+ const targetTransactionThreadReport = allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ targetTransactionThreadReportID } ` ] ;
5257
53- // State for selected values and error
54- // eslint-disable-next-line @typescript-eslint/no-unused-vars
55- const [ hasErrors , setHasErrors ] = React . useState < Partial < Record < MergeFieldKey , boolean > > > ( { } ) ;
58+ const [ hasErrors , setHasErrors ] = useState < Partial < Record < MergeFieldKey , boolean > > > ( { } ) ;
59+ const [ diffFields , setDiffFields ] = useState < MergeFieldKey [ ] > ( [ ] ) ;
60+ const [ isCheckingDataBeforeGoNext , setIsCheckingDataBeforeGoNext ] = useState < boolean > ( false ) ;
5661
57- const [ diffFields , setDiffFields ] = React . useState < MergeFieldKey [ ] > ( [ ] ) ;
58-
59- React . useEffect ( ( ) => {
62+ useEffect ( ( ) => {
6063 if ( ! transactionID || ! targetTransaction || ! sourceTransaction ) {
6164 return ;
6265 }
@@ -67,6 +70,25 @@ function DetailsReviewPage({route}: DetailsReviewPageProps) {
6770 setDiffFields ( conflictFields as MergeFieldKey [ ] ) ;
6871 } , [ targetTransaction , sourceTransaction , transactionID ] ) ;
6972
73+ useEffect ( ( ) => {
74+ if ( ! isCheckingDataBeforeGoNext ) {
75+ return ;
76+ }
77+
78+ // When user selects a card transaction to merge, that card transaction becomes the target transaction.
79+ // The App may not have the transaction thread report loaded for card transactions, so we need to trigger
80+ // OpenReport to ensure the transaction thread report is available for confirmation page
81+ if ( ! targetTransactionThreadReportID && targetTransaction ?. reportID ) {
82+ return openReport ( targetTransaction . reportID ) ;
83+ }
84+ if ( targetTransactionThreadReportID && ! targetTransactionThreadReport ) {
85+ return openReport ( targetTransactionThreadReportID ) ;
86+ }
87+
88+ Navigation . navigate ( ROUTES . MERGE_TRANSACTION_CONFIRMATION_PAGE . getRoute ( transactionID , Navigation . getActiveRoute ( ) ) ) ;
89+ setIsCheckingDataBeforeGoNext ( false ) ;
90+ } , [ isCheckingDataBeforeGoNext , targetTransactionThreadReportID , targetTransaction ?. reportID , targetTransactionThreadReport , transactionID ] ) ;
91+
7092 // Handle selection
7193 const handleSelect = ( field : MergeFieldKey , value : MergeValue ) => {
7294 // Clear error if it has
@@ -98,7 +120,7 @@ function DetailsReviewPage({route}: DetailsReviewPageProps) {
98120 setHasErrors ( newHasErrors ) ;
99121
100122 if ( isEmptyObject ( newHasErrors ) ) {
101- Navigation . navigate ( ROUTES . MERGE_TRANSACTION_CONFIRMATION_PAGE . getRoute ( transactionID , Navigation . getActiveRoute ( ) ) ) ;
123+ setIsCheckingDataBeforeGoNext ( true ) ;
102124 }
103125 } ;
104126
@@ -191,6 +213,7 @@ function DetailsReviewPage({route}: DetailsReviewPageProps) {
191213 text = { translate ( 'common.continue' ) }
192214 onPress = { handleContinue }
193215 isDisabled = { ! isEmptyObject ( hasErrors ) }
216+ isLoading = { isCheckingDataBeforeGoNext }
194217 pressOnEnter
195218 />
196219 </ FixedFooter >
0 commit comments