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: logic for waypoint validation #26591

Merged
merged 9 commits into from
Sep 4, 2023
11 changes: 5 additions & 6 deletions src/components/DistanceRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken})
const previousWaypoints = usePrevious(waypoints);
const haveWaypointsChanged = !_.isEqual(previousWaypoints, waypoints);
const doesRouteExist = lodashHas(transaction, 'routes.route0.geometry.coordinates');
const shouldFetchRoute = (!doesRouteExist || haveWaypointsChanged) && !isLoadingRoute && TransactionUtils.validateWaypoints(waypoints);

const validatedWaypoints = TransactionUtils.getValidWaypoints(waypoints);
const shouldFetchRoute = (!doesRouteExist || haveWaypointsChanged) && !isLoadingRoute && _.keys(validatedWaypoints).length > 1;
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
const waypointMarkers = useMemo(
() =>
_.filter(
Expand Down Expand Up @@ -149,14 +149,13 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken})
const visibleAreaEnd = lodashGet(event, 'nativeEvent.contentOffset.y', 0) + scrollContainerHeight;
setShouldShowGradient(visibleAreaEnd < scrollContentHeight);
};

useEffect(() => {
if (isOffline || !shouldFetchRoute) {
return;
}

Transaction.getRoute(iou.transactionID, waypoints);
}, [shouldFetchRoute, iou.transactionID, waypoints, isOffline]);
Transaction.getRoute(iou.transactionID, validatedWaypoints);
}, [shouldFetchRoute, iou.transactionID, validatedWaypoints, isOffline]);

useEffect(updateGradientVisibility, [scrollContainerHeight, scrollContentHeight]);

Expand Down Expand Up @@ -256,7 +255,7 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken})
style={[styles.w100, styles.mb4, styles.ph4, styles.flexShrink0]}
onPress={() => IOU.navigateToNextPage(iou, iouType, reportID, report)}
pressOnEnter
isDisabled={waypointMarkers.length < 2}
isDisabled={_.keys(validatedWaypoints).length < 2}
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
text={translate('common.next')}
/>
</>
Expand Down
48 changes: 29 additions & 19 deletions src/libs/TransactionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,34 +268,44 @@ function getAllReportTransactions(reportID) {
}

/**
* Verifies that the provided waypoints are valid
* Filters the waypoints which are valid and returns those
* @param {Object} waypoints
* @returns {Boolean}
* @param {Boolean} reArrangeIndexes
* @returns {Object} validated waypoints
*/
function validateWaypoints(waypoints) {
function getValidWaypoints(waypoints, reArrangeIndexes = false) {
const waypointValues = _.values(waypoints);

// Ensure the number of waypoints is between 2 and 25
if (waypointValues.length < 2 || waypointValues.length > 25) {
return false;
return {};
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
}

for (let i = 0; i < waypointValues.length; i++) {
const currentWaypoint = waypointValues[i];
const previousWaypoint = waypointValues[i - 1];
let lastWaypointIndex = -1;

// Check if the waypoint has a valid address
if (!currentWaypoint || !currentWaypoint.address || typeof currentWaypoint.address !== 'string' || currentWaypoint.address.trim() === '') {
return false;
}
const validWaypoints = _.reduce(
waypointValues,
(acc, currentWaypoint, index) => {
const previousWaypoint = waypointValues[lastWaypointIndex];
// Check if the waypoint has a valid address
if (!currentWaypoint || !currentWaypoint.address || typeof currentWaypoint.address !== 'string' || currentWaypoint.address.trim() === '') {
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
return acc;
}

// Check for adjacent waypoints with the same address
if (previousWaypoint && currentWaypoint.address === previousWaypoint.address) {
return false;
}
}
// Check for adjacent waypoints with the same address
if (previousWaypoint && currentWaypoint.address === previousWaypoint.address) {
return acc;
}

const validatedWaypoints = {...acc, [`waypoint${reArrangeIndexes ? lastWaypointIndex + 1 : index}`]: currentWaypoint};
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved

lastWaypointIndex += 1;

return validatedWaypoints;
},
{},
);

return true;
return validWaypoints;
}

/*
Expand Down Expand Up @@ -325,7 +335,7 @@ export {
getAllReportTransactions,
hasReceipt,
isReceiptBeingScanned,
validateWaypoints,
getValidWaypoints,
isDistanceRequest,
hasMissingSmartscanFields,
};
2 changes: 1 addition & 1 deletion src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ function createDistanceRequest(report, participant, comment, created, transactio
createdChatReportActionID,
createdIOUReportActionID,
reportPreviewReportActionID: reportPreviewAction.reportActionID,
waypoints: JSON.stringify(transaction.comment.waypoints),
waypoints: JSON.stringify(TransactionUtils.getValidWaypoints(transaction.comment.waypoints, true)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think 2nd param is needed.
This will also be used in GetRoute api. And doesn't affect displaying routes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replied here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need this logic to reorganize indices to be consecutive (i.e. we need to pass true)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I meant to reorganize always without needing to pass 2nd param but later I realized it should be conditional

created,
},
onyxData,
Expand Down