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 error message for duplicate waypoint #30548

Merged
merged 12 commits into from
Nov 7, 2023
9 changes: 3 additions & 6 deletions src/components/DistanceRequest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function DistanceRequest({transactionID, report, transaction, route, isEditingRe
const haveValidatedWaypointsChanged = !_.isEqual(previousValidatedWaypoints, validatedWaypoints);
const isRouteAbsentWithoutErrors = !hasRoute && !hasRouteError;
const shouldFetchRoute = (isRouteAbsentWithoutErrors || haveValidatedWaypointsChanged) && !isLoadingRoute && _.size(validatedWaypoints) > 1;
const numberOfNoneEmptyWaypoints = _.filter(_.values(waypoints), (value) => !_.isEmpty(value) && TransactionUtils.waypointHasValidAddress(value)).length;

useEffect(() => {
MapboxToken.init();
Expand Down Expand Up @@ -150,12 +151,8 @@ function DistanceRequest({transactionID, report, transaction, route, isEditingRe
return ErrorUtils.getLatestErrorField(transaction, 'route');
}

// Initially, both waypoints will be null, and if we give fallback value as empty string that will result in true condition, that's why different default values.
if (_.keys(waypoints).length === 2 && lodashGet(waypoints, 'waypoint0.address', 'address1') === lodashGet(waypoints, 'waypoint1.address', 'address2')) {
return {0: translate('iou.error.duplicateWaypointsErrorMessage')};
}

if (_.size(validatedWaypoints) < 2) {
const isDuplicatedWaypoints = numberOfNoneEmptyWaypoints > _.size(validatedWaypoints) && _.size(validatedWaypoints) === 1;
if (_.size(validatedWaypoints) < 2 || isDuplicatedWaypoints) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I meant that we don't need isDuplicatedWaypoints and numberOfNoneEmptyWaypoints. Also, we can delete iou.error.duplicateWaypointsErrorMessage and its translation

return {0: translate('iou.error.emptyWaypointsErrorMessage')};
Copy link
Contributor

Choose a reason for hiding this comment

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

we can replace iou.error.emptyWaypointsErrorMessage with iou.error.atLeastTwoDifferentWaypoints, its content: Please enter at least two different waypoints

}
};
Expand Down
6 changes: 4 additions & 2 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ function getValidWaypoints(waypoints: WaypointCollection, reArrangeIndexes = fal
}

let lastWaypointIndex = -1;
let waypointIndex = -1;

return waypointValues.reduce<WaypointCollection>((acc, currentWaypoint, index) => {
const previousWaypoint = waypointValues[lastWaypointIndex];
Expand All @@ -431,9 +432,10 @@ function getValidWaypoints(waypoints: WaypointCollection, reArrangeIndexes = fal
return acc;
}

const validatedWaypoints: WaypointCollection = {...acc, [`waypoint${reArrangeIndexes ? lastWaypointIndex + 1 : index}`]: currentWaypoint};
const validatedWaypoints: WaypointCollection = {...acc, [`waypoint${reArrangeIndexes ? waypointIndex + 1 : index}`]: currentWaypoint};

lastWaypointIndex += 1;
lastWaypointIndex = index;
waypointIndex += 1;

return validatedWaypoints;
}, {});
Expand Down
Loading