Skip to content

Commit

Permalink
Merge pull request #26705 from allroundexperts/fix-followup-26583
Browse files Browse the repository at this point in the history
fix: sort valid waypoints
  • Loading branch information
neil-marcellini authored Sep 11, 2023
2 parents b8dc1c3 + 9cedf17 commit c95998d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
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 @@ -91,7 +91,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 @@ -104,7 +104,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 @@ -182,7 +182,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 @@ -264,7 +264,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 @@ -303,14 +303,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 @@ -340,7 +350,6 @@ function getValidWaypoints(waypoints, reArrangeIndexes = false) {
},
{},
);

return validWaypoints;
}

Expand All @@ -360,4 +369,5 @@ export {
getValidWaypoints,
isDistanceRequest,
hasMissingSmartscanFields,
getWaypointIndex,
};

0 comments on commit c95998d

Please sign in to comment.