Skip to content

Commit 846a4b7

Browse files
authored
Merge pull request #28308 from DylanDylann/fix/27165-after-revert
Fix/27165: don't allow go to next step if empty waypoint
2 parents 54b99d4 + 80d1013 commit 846a4b7

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

src/pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsPage.js

+33-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import lodashGet from 'lodash/get';
2+
import lodashSize from 'lodash/size';
23
import PropTypes from 'prop-types';
34
import React, {useCallback, useEffect, useRef, useState} from 'react';
45
import {View} from 'react-native';
56
import {withOnyx} from 'react-native-onyx';
67
import HeaderWithBackButton from '@components/HeaderWithBackButton';
78
import ScreenWrapper from '@components/ScreenWrapper';
9+
import transactionPropTypes from '@components/transactionPropTypes';
810
import useInitialValue from '@hooks/useInitialValue';
911
import useLocalize from '@hooks/useLocalize';
12+
import compose from '@libs/compose';
1013
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
1114
import * as MoneyRequestUtils from '@libs/MoneyRequestUtils';
1215
import Navigation from '@libs/Navigation/Navigation';
16+
import * as TransactionUtils from '@libs/TransactionUtils';
1317
import {iouDefaultProps, iouPropTypes} from '@pages/iou/propTypes';
1418
import styles from '@styles/styles';
1519
import * as IOU from '@userActions/IOU';
@@ -36,14 +40,18 @@ const propTypes = {
3640

3741
/** The current tab we have navigated to in the request modal. String that corresponds to the request type. */
3842
selectedTab: PropTypes.oneOf([CONST.TAB.DISTANCE, CONST.TAB.MANUAL, CONST.TAB.SCAN]),
43+
44+
/** Transaction that stores the distance request data */
45+
transaction: transactionPropTypes,
3946
};
4047

4148
const defaultProps = {
4249
iou: iouDefaultProps,
50+
transaction: {},
4351
selectedTab: undefined,
4452
};
4553

46-
function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
54+
function MoneyRequestParticipantsPage({iou, selectedTab, route, transaction}) {
4755
const {translate} = useLocalize();
4856
const prevMoneyRequestId = useRef(iou.id);
4957
const optionsSelectorRef = useRef();
@@ -54,7 +62,9 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
5462
const isScanRequest = MoneyRequestUtils.isScanRequest(selectedTab);
5563
const isSplitRequest = iou.id === CONST.IOU.TYPE.SPLIT;
5664
const [headerTitle, setHeaderTitle] = useState();
57-
65+
const waypoints = lodashGet(transaction, 'comment.waypoints', {});
66+
const validatedWaypoints = TransactionUtils.getValidWaypoints(waypoints);
67+
const isInvalidWaypoint = lodashSize(validatedWaypoints) < 2;
5868
useEffect(() => {
5969
if (isDistanceRequest) {
6070
setHeaderTitle(translate('common.distance'));
@@ -85,10 +95,12 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
8595
}, []);
8696

8797
useEffect(() => {
98+
const isInvalidDistanceRequest = !isDistanceRequest || isInvalidWaypoint;
99+
88100
// ID in Onyx could change by initiating a new request in a separate browser tab or completing a request
89101
if (prevMoneyRequestId.current !== iou.id) {
90102
// 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) {
92104
navigateBack(true);
93105
}
94106
return;
@@ -100,14 +112,14 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
100112
if (shouldReset) {
101113
IOU.resetMoneyRequestInfo(moneyRequestId);
102114
}
103-
if (!isDistanceRequest && ((iou.amount === 0 && !iou.receiptPath) || shouldReset)) {
115+
if (isInvalidDistanceRequest && ((iou.amount === 0 && !iou.receiptPath) || shouldReset)) {
104116
navigateBack(true);
105117
}
106118

107119
return () => {
108120
prevMoneyRequestId.current = iou.id;
109121
};
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]);
111123

112124
return (
113125
<ScreenWrapper
@@ -143,11 +155,19 @@ MoneyRequestParticipantsPage.displayName = 'MoneyRequestParticipantsPage';
143155
MoneyRequestParticipantsPage.propTypes = propTypes;
144156
MoneyRequestParticipantsPage.defaultProps = defaultProps;
145157

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

Comments
 (0)