Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/27165: don't allow go to next step if empty waypoint #28308

Merged
merged 15 commits into from
Nov 4, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import compose from '../../../../libs/compose';
import CONST from '../../../../CONST';
import ONYXKEYS from '../../../../ONYXKEYS';
import ROUTES from '../../../../ROUTES';
Expand All @@ -14,9 +15,11 @@ import Navigation from '../../../../libs/Navigation/Navigation';
import * as DeviceCapabilities from '../../../../libs/DeviceCapabilities';
import HeaderWithBackButton from '../../../../components/HeaderWithBackButton';
import * as IOU from '../../../../libs/actions/IOU';
import * as TransactionUtils from '../../../../libs/TransactionUtils';
import * as MoneyRequestUtils from '../../../../libs/MoneyRequestUtils';
import {iouPropTypes, iouDefaultProps} from '../../propTypes';
import useLocalize from '../../../../hooks/useLocalize';
import transactionPropTypes from '../../../../components/transactionPropTypes';

const propTypes = {
/** React Navigation route */
Expand All @@ -36,14 +39,18 @@ const propTypes = {

/** The current tab we have navigated to in the request modal. String that corresponds to the request type. */
selectedTab: PropTypes.oneOf([CONST.TAB.DISTANCE, CONST.TAB.MANUAL, CONST.TAB.SCAN]),

/** Transaction that stores the distance request data */
transaction: transactionPropTypes,
};

const defaultProps = {
iou: iouDefaultProps,
transaction: {},
selectedTab: undefined,
};

function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
function MoneyRequestParticipantsPage({iou, selectedTab, route, transaction}) {
const {translate} = useLocalize();
const prevMoneyRequestId = useRef(iou.id);
const optionsSelectorRef = useRef();
Expand All @@ -54,7 +61,9 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
const isScanRequest = MoneyRequestUtils.isScanRequest(selectedTab);
const isSplitRequest = iou.id === CONST.IOU.TYPE.SPLIT;
const [headerTitle, setHeaderTitle] = useState();

const waypoints = lodashGet(transaction, 'comment.waypoints', {});
const validatedWaypoints = TransactionUtils.getValidWaypoints(waypoints);
const isInvalidWaypoint = _.size(validatedWaypoints) < 2;
useEffect(() => {
if (isDistanceRequest) {
setHeaderTitle(translate('common.distance'));
Expand All @@ -79,10 +88,12 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
};

useEffect(() => {
const isInvalidDistanceRequest = !isDistanceRequest || isInvalidWaypoint;

// ID in Onyx could change by initiating a new request in a separate browser tab or completing a request
if (prevMoneyRequestId.current !== iou.id) {
// The ID is cleared on completing a request. In that case, we will do nothing
if (iou.id && !isDistanceRequest && !isSplitRequest) {
if (iou.id && isInvalidDistanceRequest && !isSplitRequest) {
navigateBack(true);
}
return;
Expand All @@ -94,14 +105,14 @@ function MoneyRequestParticipantsPage({iou, selectedTab, route}) {
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestId);
}
if (!isDistanceRequest && ((iou.amount === 0 && !iou.receiptPath) || shouldReset)) {
if (isInvalidDistanceRequest && ((iou.amount === 0 && !iou.receiptPath) || shouldReset)) {
navigateBack(true);
}

return () => {
prevMoneyRequestId.current = iou.id;
};
}, [iou.amount, iou.id, iou.receiptPath, isDistanceRequest, isSplitRequest]);
}, [iou.amount, iou.id, iou.receiptPath, isDistanceRequest, isSplitRequest, isInvalidWaypoint]);

return (
<ScreenWrapper
Expand Down Expand Up @@ -137,11 +148,19 @@ MoneyRequestParticipantsPage.displayName = 'MoneyRequestParticipantsPage';
MoneyRequestParticipantsPage.propTypes = propTypes;
MoneyRequestParticipantsPage.defaultProps = defaultProps;

export default withOnyx({
iou: {
key: ONYXKEYS.IOU,
},
selectedTab: {
key: `${ONYXKEYS.COLLECTION.SELECTED_TAB}${CONST.TAB.RECEIPT_TAB_ID}`,
},
})(MoneyRequestParticipantsPage);
export default compose(
withOnyx({
iou: {
key: ONYXKEYS.IOU,
},
selectedTab: {
key: `${ONYXKEYS.COLLECTION.SELECTED_TAB}${CONST.TAB.RECEIPT_TAB_ID}`,
},
}),
// eslint-disable-next-line rulesdir/no-multiple-onyx-in-file
withOnyx({
transaction: {
key: ({iou}) => `${ONYXKEYS.COLLECTION.TRANSACTION}${iou.transactionID}`,
},
}),
DylanDylann marked this conversation as resolved.
Show resolved Hide resolved
)(MoneyRequestParticipantsPage);
Loading