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: sort valid waypoints #26705

Merged
merged 6 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/ConfirmedRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import _ from 'underscore';
import ONYXKEYS from '../ONYXKEYS';
import CONST from '../CONST';
import * as MapboxToken from '../libs/actions/MapboxToken';
import * as TransactionUtils from '../libs/TransactionUtils';
import * as Expensicons from './Icon/Expensicons';
import theme from '../styles/themes/default';
import styles from '../styles/styles';
Expand Down Expand Up @@ -47,7 +48,7 @@ const getWaypointMarkers = (waypoints) => {
return;
}

const index = Number(key.replace('waypoint', ''));
const index = TransactionUtils.getWaypointIndex(key);
let MarkerComponent;
if (index === 0) {
MarkerComponent = Expensicons.DotIndicatorUnfilled;
Expand Down
8 changes: 4 additions & 4 deletions src/components/DistanceRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken})

const lastWaypointIndex = numberOfWaypoints - 1;
const isLoadingRoute = lodashGet(transaction, 'comment.isLoading', false);
const hasRouteError = lodashHas(transaction, 'errorFields.route');
const hasRouteError = !!lodashGet(transaction, 'errorFields.route');
const haveWaypointsChanged = !_.isEqual(previousWaypoints, waypoints);
const doesRouteExist = lodashHas(transaction, 'routes.route0.geometry.coordinates');
const validatedWaypoints = TransactionUtils.getValidWaypoints(waypoints);
Expand All @@ -103,7 +103,7 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken})
return;
}

const index = Number(key.replace('waypoint', ''));
const index = TransactionUtils.getWaypointIndex(key);
let MarkerComponent;
if (index === 0) {
MarkerComponent = Expensicons.DotIndicatorUnfilled;
Expand Down Expand Up @@ -181,7 +181,7 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken})
>
{_.map(waypoints, (waypoint, key) => {
// key is of the form waypoint0, waypoint1, ...
const index = Number(key.replace('waypoint', ''));
const index = TransactionUtils.getWaypointIndex(key);
let descriptionKey = 'distance.waypointDescription.';
let waypointIcon;
if (index === 0) {
Expand Down Expand Up @@ -263,7 +263,7 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken})
success
style={[styles.w100, styles.mb4, styles.ph4, styles.flexShrink0]}
onPress={() => IOU.navigateToNextPage(iou, iouType, reportID, report)}
isDisabled={_.size(validatedWaypoints) < 2}
isDisabled={_.size(validatedWaypoints) < 2 || hasRouteError || isOffline}
text={translate('common.next')}
/>
</ScrollView>
Expand Down
14 changes: 12 additions & 2 deletions src/libs/TransactionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,24 @@ function waypointHasValidAddress(waypoint) {
return true;
}

/**
* Converts the key of a waypoint to its index
* @param {String} key
* @returns {Number} waypoint index
*/
function getWaypointIndex(key) {
return Number(key.replace('waypoint', ''));
}

/**
* Filters the waypoints which are valid and returns those
* @param {Object} waypoints
* @param {Boolean} reArrangeIndexes
* @returns {Object} validated waypoints
*/
function getValidWaypoints(waypoints, reArrangeIndexes = false) {
const waypointValues = _.values(waypoints);
const sortedIndexes = _.map(_.keys(waypoints), (key) => getWaypointIndex(key)).sort();
const waypointValues = _.map(sortedIndexes, (index) => waypoints[`waypoint${index}`]);
// Ensure the number of waypoints is between 2 and 25
if (waypointValues.length < 2 || waypointValues.length > 25) {
return {};
Expand Down Expand Up @@ -339,7 +349,6 @@ function getValidWaypoints(waypoints, reArrangeIndexes = false) {
},
{},
);

return validWaypoints;
}

Expand All @@ -359,4 +368,5 @@ export {
getValidWaypoints,
isDistanceRequest,
hasMissingSmartscanFields,
getWaypointIndex,
};
Loading