@@ -3,8 +3,10 @@ import type {ListItem} from '@components/SelectionList/types';
33import useOnyx from '@hooks/useOnyx' ;
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,104 @@ type TransactionGroupListItem = 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 } ) ;
2628
2729 const isEditing = action === CONST . IOU . ACTION . EDIT ;
30+ const isCreateReport = action === CONST . IOU . ACTION . CREATE ;
31+ const isFromGlobalCreate = ! ! transaction ?. isFromGlobalCreate ;
32+ const reportOrDraftReport = getReportOrDraftReport ( reportIDFromRoute ) ;
2833
29- const selectReport = ( item : TransactionGroupListItem ) => {
34+ const handleGoBackWithReportID = ( id : string ) => {
35+ if ( isEditing ) {
36+ Navigation . dismissModalWithReport ( { reportID : id } ) ;
37+ } else {
38+ Navigation . goBack ( backTo ) ;
39+ }
40+ } ;
41+
42+ const handleGlobalCreateReport = ( item : TransactionGroupListItem ) => {
3043 if ( ! transaction ) {
3144 return ;
3245 }
33- if ( item . value !== transaction . reportID ) {
34- setTransactionReport ( transaction . transactionID , item . value , ! isEditing ) ;
35- if ( isEditing ) {
36- changeTransactionsReport ( [ transaction . transactionID ] , item . value ) ;
37- }
46+ const reportOrDraftReportFromValue = getReportOrDraftReport ( item . value ) ;
47+ const participants = [
48+ {
49+ selected : true ,
50+ accountID : 0 ,
51+ isPolicyExpenseChat : true ,
52+ reportID : reportOrDraftReportFromValue ?. chatReportID ,
53+ policyID : reportOrDraftReportFromValue ?. policyID ,
54+ } ,
55+ ] ;
56+
57+ setTransactionReport (
58+ transaction . transactionID ,
59+ {
60+ reportID : item . value ,
61+ participants,
62+ } ,
63+ true ,
64+ ) ;
65+
66+ const iouConfirmationPageRoute = ROUTES . MONEY_REQUEST_STEP_CONFIRMATION . getRoute ( action , iouType , transactionID , reportOrDraftReportFromValue ?. chatReportID ) ;
67+ // If the backTo parameter is set, we should navigate back to the confirmation screen that is already on the stack.
68+ if ( backTo ) {
69+ Navigation . goBack ( iouConfirmationPageRoute , { compareParams : false } ) ;
70+ } else {
71+ Navigation . navigate ( iouConfirmationPageRoute ) ;
72+ }
73+ } ;
74+
75+ const handleRegularReportSelection = ( item : TransactionGroupListItem ) => {
76+ if ( ! transaction ) {
77+ return ;
3878 }
79+ setTransactionReport (
80+ transaction . transactionID ,
81+ {
82+ reportID : item . value ,
83+ } ,
84+ ! isEditing ,
85+ ) ;
86+
3987 if ( isEditing ) {
40- Navigation . dismissModalWithReport ( { reportID : item . value } ) ;
41- } else {
42- Navigation . goBack ( backTo ) ;
88+ changeTransactionsReport ( [ transaction . transactionID ] , item . value ) ;
89+ }
90+
91+ handleGoBackWithReportID ( item . value ) ;
92+ } ;
93+
94+ const selectReport = ( item : TransactionGroupListItem ) => {
95+ if ( ! transaction ) {
96+ return ;
4397 }
98+
99+ const isSameReport = item . value === transaction . reportID ;
100+
101+ // Early return for same report selection
102+ if ( isSameReport ) {
103+ handleGoBackWithReportID ( item . value ) ;
104+ return ;
105+ }
106+
107+ // Handle global create report
108+ if ( isCreateReport && isFromGlobalCreate ) {
109+ handleGlobalCreateReport ( item ) ;
110+ return ;
111+ }
112+
113+ // Handle regular report selection
114+ handleRegularReportSelection ( item ) ;
44115 } ;
45116
46117 return (
47118 < IOURequestEditReportCommon
48119 backTo = { backTo }
49120 transactionsReports = { transactionReport ? [ transactionReport ] : [ ] }
50121 selectReport = { selectReport }
122+ policyID = { ! isEditing && ! isFromGlobalCreate ? reportOrDraftReport ?. policyID : undefined }
51123 />
52124 ) ;
53125}
0 commit comments