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
9 changes: 4 additions & 5 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.validateWaypoints(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
36 changes: 19 additions & 17 deletions src/libs/TransactionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,30 @@ function getAllReportTransactions(reportID) {
*/
function validateWaypoints(waypoints) {
const waypointValues = _.values(waypoints);

// Ensure the number of waypoints is between 2 and 25
if (waypointValues.length < 2 || waypointValues.length > 25) {
return false;
allroundexperts marked this conversation as resolved.
Show resolved Hide resolved
allroundexperts 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];

// Check if the waypoint has a valid address
if (!currentWaypoint || !currentWaypoint.address || typeof currentWaypoint.address !== 'string' || currentWaypoint.address.trim() === '') {
return false;
}

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

return true;
const validWaypoints = _.reduce(
waypointValues,
(acc, currentWaypoint, index) => {
const previousWaypoint = waypointValues[index - 1];
// 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 acc;
}
return {...acc, [`waypoint${index}`]: currentWaypoint};
},
{},
);

return validWaypoints;
}

/*
Expand Down
3 changes: 2 additions & 1 deletion src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ function createDistanceRequest(report, participant, comment, created, transactio
null,
transactionID,
);
const validatedWaypoints = TransactionUtils.validateWaypoints(transaction.comment.waypoints);
API.write(
'CreateDistanceRequest',
{
Expand All @@ -530,7 +531,7 @@ function createDistanceRequest(report, participant, comment, created, transactio
createdChatReportActionID,
createdIOUReportActionID,
reportPreviewReportActionID: reportPreviewAction.reportActionID,
waypoints: JSON.stringify(transaction.comment.waypoints),
waypoints: JSON.stringify(validatedWaypoints),
allroundexperts marked this conversation as resolved.
Show resolved Hide resolved
created,
},
onyxData,
Expand Down