@@ -3,8 +3,10 @@ import {useOnyx} from 'react-native-onyx';
33import type { ListItem } from '@components/SelectionList/types' ;
44import { changeTransactionsReport , setTransactionReport } from '@libs/actions/Transaction' ;
55import Navigation from '@libs/Navigation/Navigation' ;
6+ import { getReportOrDraftReport } from '@libs/ReportUtils' ;
67import CONST from '@src/CONST' ;
78import ONYXKEYS from '@src/ONYXKEYS' ;
9+ import ROUTES from '@src/ROUTES' ;
810import type SCREENS from '@src/SCREENS' ;
911import IOURequestEditReportCommon from './IOURequestEditReportCommon' ;
1012import withFullTransactionOrNotFound from './withFullTransactionOrNotFound' ;
@@ -20,34 +22,103 @@ type ReportListItem = ListItem & {
2022type IOURequestStepReportProps = WithWritableReportOrNotFoundProps < typeof SCREENS . MONEY_REQUEST . STEP_REPORT > & WithFullTransactionOrNotFoundProps < typeof SCREENS . MONEY_REQUEST . STEP_REPORT > ;
2123
2224function IOURequestStepReport ( { route, transaction} : IOURequestStepReportProps ) {
23- const { backTo, action} = route . params ;
25+ const { backTo, action, iouType , transactionID , reportID : reportIDFromRoute } = route . params ;
2426 const reportID = transaction ?. reportID === '0' ? transaction ?. participants ?. at ( 0 ) ?. reportID : transaction ?. reportID ;
2527 const [ transactionReport ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` , { canBeMissing : true } ) ;
26-
28+ const isCreateReport = action === CONST . IOU . ACTION . CREATE ;
2729 const isEditing = action === CONST . IOU . ACTION . EDIT ;
30+ const isFromGlobalCreate = ! ! transaction ?. isFromGlobalCreate ;
31+ const reportOrDraftReport = getReportOrDraftReport ( reportIDFromRoute ) ;
2832
29- const selectReport = ( item : ReportListItem ) => {
33+ const handleGoBackWithReportID = ( id : string ) => {
34+ if ( isEditing ) {
35+ Navigation . dismissModalWithReport ( { reportID : id } ) ;
36+ } else {
37+ Navigation . goBack ( backTo ) ;
38+ }
39+ } ;
40+
41+ const handleGlobalCreateReport = ( item : ReportListItem ) => {
3042 if ( ! transaction ) {
3143 return ;
3244 }
33- if ( item . value !== transaction . reportID ) {
34- setTransactionReport ( transaction . transactionID , item . value , ! isEditing ) ;
35- if ( isEditing ) {
36- changeTransactionsReport ( [ transaction . transactionID ] , item . value ) ;
37- }
45+ const reportOrDraftReportFromValue = getReportOrDraftReport ( item . value ) ;
46+ const participants = [
47+ {
48+ selected : true ,
49+ accountID : 0 ,
50+ isPolicyExpenseChat : true ,
51+ reportID : reportOrDraftReportFromValue ?. chatReportID ,
52+ policyID : reportOrDraftReportFromValue ?. policyID ,
53+ } ,
54+ ] ;
55+
56+ setTransactionReport (
57+ transaction . transactionID ,
58+ {
59+ reportID : item . value ,
60+ participants,
61+ } ,
62+ true ,
63+ ) ;
64+
65+ const iouConfirmationPageRoute = ROUTES . MONEY_REQUEST_STEP_CONFIRMATION . getRoute ( action , iouType , transactionID , reportOrDraftReportFromValue ?. chatReportID ) ;
66+ // If the backTo parameter is set, we should navigate back to the confirmation screen that is already on the stack.
67+ if ( backTo ) {
68+ Navigation . goBack ( iouConfirmationPageRoute , { compareParams : false } ) ;
69+ } else {
70+ Navigation . navigate ( iouConfirmationPageRoute ) ;
71+ }
72+ } ;
73+
74+ const handleRegularReportSelection = ( item : ReportListItem ) => {
75+ if ( ! transaction ) {
76+ return ;
3877 }
78+ setTransactionReport (
79+ transaction . transactionID ,
80+ {
81+ reportID : item . value ,
82+ } ,
83+ ! isEditing ,
84+ ) ;
85+
3986 if ( isEditing ) {
40- Navigation . dismissModalWithReport ( { reportID : item . value } ) ;
41- } else {
42- Navigation . goBack ( backTo ) ;
87+ changeTransactionsReport ( [ transaction . transactionID ] , item . value ) ;
88+ }
89+
90+ handleGoBackWithReportID ( item . value ) ;
91+ } ;
92+
93+ const selectReport = ( item : ReportListItem ) => {
94+ if ( ! transaction ) {
95+ return ;
4396 }
97+
98+ const isSameReport = item . value === transaction . reportID ;
99+
100+ // Early return for same report selection
101+ if ( isSameReport ) {
102+ handleGoBackWithReportID ( item . value ) ;
103+ return ;
104+ }
105+
106+ // Handle global create report
107+ if ( isCreateReport && isFromGlobalCreate ) {
108+ handleGlobalCreateReport ( item ) ;
109+ return ;
110+ }
111+
112+ // Handle regular report selection
113+ handleRegularReportSelection ( item ) ;
44114 } ;
45115
46116 return (
47117 < IOURequestEditReportCommon
48118 backTo = { backTo }
49119 transactionsReports = { transactionReport ? [ transactionReport ] : [ ] }
50120 selectReport = { selectReport }
121+ policyID = { ! isEditing && ! isFromGlobalCreate ? reportOrDraftReport ?. policyID : undefined }
51122 />
52123 ) ;
53124}
0 commit comments