1
1
import lodashGet from 'lodash/get' ;
2
+ import lodashSize from 'lodash/size' ;
2
3
import PropTypes from 'prop-types' ;
3
4
import React , { useCallback , useEffect , useRef , useState } from 'react' ;
4
5
import { View } from 'react-native' ;
5
6
import { withOnyx } from 'react-native-onyx' ;
6
7
import HeaderWithBackButton from '@components/HeaderWithBackButton' ;
7
8
import ScreenWrapper from '@components/ScreenWrapper' ;
9
+ import transactionPropTypes from '@components/transactionPropTypes' ;
8
10
import useInitialValue from '@hooks/useInitialValue' ;
9
11
import useLocalize from '@hooks/useLocalize' ;
12
+ import compose from '@libs/compose' ;
10
13
import * as DeviceCapabilities from '@libs/DeviceCapabilities' ;
11
14
import * as MoneyRequestUtils from '@libs/MoneyRequestUtils' ;
12
15
import Navigation from '@libs/Navigation/Navigation' ;
16
+ import * as TransactionUtils from '@libs/TransactionUtils' ;
13
17
import { iouDefaultProps , iouPropTypes } from '@pages/iou/propTypes' ;
14
18
import styles from '@styles/styles' ;
15
19
import * as IOU from '@userActions/IOU' ;
@@ -36,14 +40,18 @@ const propTypes = {
36
40
37
41
/** The current tab we have navigated to in the request modal. String that corresponds to the request type. */
38
42
selectedTab : PropTypes . oneOf ( [ CONST . TAB . DISTANCE , CONST . TAB . MANUAL , CONST . TAB . SCAN ] ) ,
43
+
44
+ /** Transaction that stores the distance request data */
45
+ transaction : transactionPropTypes ,
39
46
} ;
40
47
41
48
const defaultProps = {
42
49
iou : iouDefaultProps ,
50
+ transaction : { } ,
43
51
selectedTab : undefined ,
44
52
} ;
45
53
46
- function MoneyRequestParticipantsPage ( { iou, selectedTab, route} ) {
54
+ function MoneyRequestParticipantsPage ( { iou, selectedTab, route, transaction } ) {
47
55
const { translate} = useLocalize ( ) ;
48
56
const prevMoneyRequestId = useRef ( iou . id ) ;
49
57
const optionsSelectorRef = useRef ( ) ;
@@ -54,7 +62,9 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
54
62
const isScanRequest = MoneyRequestUtils . isScanRequest ( selectedTab ) ;
55
63
const isSplitRequest = iou . id === CONST . IOU . TYPE . SPLIT ;
56
64
const [ headerTitle , setHeaderTitle ] = useState ( ) ;
57
-
65
+ const waypoints = lodashGet ( transaction , 'comment.waypoints' , { } ) ;
66
+ const validatedWaypoints = TransactionUtils . getValidWaypoints ( waypoints ) ;
67
+ const isInvalidWaypoint = lodashSize ( validatedWaypoints ) < 2 ;
58
68
useEffect ( ( ) => {
59
69
if ( isDistanceRequest ) {
60
70
setHeaderTitle ( translate ( 'common.distance' ) ) ;
@@ -85,10 +95,12 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
85
95
} , [ ] ) ;
86
96
87
97
useEffect ( ( ) => {
98
+ const isInvalidDistanceRequest = ! isDistanceRequest || isInvalidWaypoint ;
99
+
88
100
// ID in Onyx could change by initiating a new request in a separate browser tab or completing a request
89
101
if ( prevMoneyRequestId . current !== iou . id ) {
90
102
// The ID is cleared on completing a request. In that case, we will do nothing
91
- if ( iou . id && ! isDistanceRequest && ! isSplitRequest ) {
103
+ if ( iou . id && isInvalidDistanceRequest && ! isSplitRequest ) {
92
104
navigateBack ( true ) ;
93
105
}
94
106
return ;
@@ -100,14 +112,14 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
100
112
if ( shouldReset ) {
101
113
IOU . resetMoneyRequestInfo ( moneyRequestId ) ;
102
114
}
103
- if ( ! isDistanceRequest && ( ( iou . amount === 0 && ! iou . receiptPath ) || shouldReset ) ) {
115
+ if ( isInvalidDistanceRequest && ( ( iou . amount === 0 && ! iou . receiptPath ) || shouldReset ) ) {
104
116
navigateBack ( true ) ;
105
117
}
106
118
107
119
return ( ) => {
108
120
prevMoneyRequestId . current = iou . id ;
109
121
} ;
110
- } , [ iou . amount , iou . id , iou . receiptPath , isDistanceRequest , isSplitRequest , iouType , reportID , navigateBack ] ) ;
122
+ } , [ iou . amount , iou . id , iou . receiptPath , isDistanceRequest , isSplitRequest , iouType , reportID , navigateBack , isInvalidWaypoint ] ) ;
111
123
112
124
return (
113
125
< ScreenWrapper
@@ -143,11 +155,19 @@ MoneyRequestParticipantsPage.displayName = 'MoneyRequestParticipantsPage';
143
155
MoneyRequestParticipantsPage . propTypes = propTypes ;
144
156
MoneyRequestParticipantsPage . defaultProps = defaultProps ;
145
157
146
- export default withOnyx ( {
147
- iou : {
148
- key : ONYXKEYS . IOU ,
149
- } ,
150
- selectedTab : {
151
- key : `${ ONYXKEYS . COLLECTION . SELECTED_TAB } ${ CONST . TAB . RECEIPT_TAB_ID } ` ,
152
- } ,
153
- } ) ( MoneyRequestParticipantsPage ) ;
158
+ export default compose (
159
+ withOnyx ( {
160
+ iou : {
161
+ key : ONYXKEYS . IOU ,
162
+ } ,
163
+ selectedTab : {
164
+ key : `${ ONYXKEYS . COLLECTION . SELECTED_TAB } ${ CONST . TAB . RECEIPT_TAB_ID } ` ,
165
+ } ,
166
+ } ) ,
167
+ // eslint-disable-next-line rulesdir/no-multiple-onyx-in-file
168
+ withOnyx ( {
169
+ transaction : {
170
+ key : ( { iou} ) => `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ iou . transactionID } ` ,
171
+ } ,
172
+ } ) ,
173
+ ) ( MoneyRequestParticipantsPage ) ;
0 commit comments