From 10a7e6ca983f0d227fc4a1e7056178483291482d Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Mon, 4 Sep 2023 23:41:27 +0500 Subject: [PATCH 1/5] fix: sort valid waypoints --- src/libs/TransactionUtils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index b99c44abad90..7c288144aa0b 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -309,7 +309,8 @@ function waypointHasValidAddress(waypoint) { * @returns {Object} validated waypoints */ function getValidWaypoints(waypoints, reArrangeIndexes = false) { - const waypointValues = _.values(waypoints); + const sortedIndexes = _.map(waypoints, (_, key) => parseInt(key.replace('waypoint', ''), 10)).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 {}; @@ -339,7 +340,6 @@ function getValidWaypoints(waypoints, reArrangeIndexes = false) { }, {}, ); - return validWaypoints; } From 1fb4677c3f90451e29e83b574fc21bc42ac18051 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Mon, 4 Sep 2023 23:55:04 +0500 Subject: [PATCH 2/5] lint errors --- src/libs/TransactionUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index 7c288144aa0b..4a686733925e 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -309,7 +309,7 @@ function waypointHasValidAddress(waypoint) { * @returns {Object} validated waypoints */ function getValidWaypoints(waypoints, reArrangeIndexes = false) { - const sortedIndexes = _.map(waypoints, (_, key) => parseInt(key.replace('waypoint', ''), 10)).sort(); + const sortedIndexes = _.map(_.keys(waypoints), (key) => parseInt(key.replace('waypoint', ''), 10)).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) { From 26ca7fa21c7dc18ea28bc3e04830f63899470a0f Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Tue, 5 Sep 2023 00:29:39 +0500 Subject: [PATCH 3/5] fix: disable button on error --- src/components/DistanceRequest.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/DistanceRequest.js b/src/components/DistanceRequest.js index f3b1dcd94cf9..a1e84fbefead 100644 --- a/src/components/DistanceRequest.js +++ b/src/components/DistanceRequest.js @@ -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); @@ -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} text={translate('common.next')} /> From ace6a400132d776fcac3398ab1e1d279d41e1302 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Tue, 5 Sep 2023 14:57:57 +0500 Subject: [PATCH 4/5] fix: disable button when offline --- src/components/DistanceRequest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/DistanceRequest.js b/src/components/DistanceRequest.js index 1e83cd272a01..c76a48dc187f 100644 --- a/src/components/DistanceRequest.js +++ b/src/components/DistanceRequest.js @@ -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 || hasRouteError} + isDisabled={_.size(validatedWaypoints) < 2 || hasRouteError || isOffline} text={translate('common.next')} /> From 9cedf1771c00409b117b0cc0103c49d46b872391 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Tue, 5 Sep 2023 22:54:07 +0500 Subject: [PATCH 5/5] fix: create a utility function for extracting waypoint index --- src/components/ConfirmedRoute.js | 3 ++- src/components/DistanceRequest.js | 4 ++-- src/libs/TransactionUtils.js | 12 +++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/components/ConfirmedRoute.js b/src/components/ConfirmedRoute.js index aa7c38e0c535..6790e8ae4d65 100644 --- a/src/components/ConfirmedRoute.js +++ b/src/components/ConfirmedRoute.js @@ -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'; @@ -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; diff --git a/src/components/DistanceRequest.js b/src/components/DistanceRequest.js index c76a48dc187f..bb544ff13d30 100644 --- a/src/components/DistanceRequest.js +++ b/src/components/DistanceRequest.js @@ -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; @@ -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) { diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index 4a686733925e..f5b59edd9248 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -302,6 +302,15 @@ 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 @@ -309,7 +318,7 @@ function waypointHasValidAddress(waypoint) { * @returns {Object} validated waypoints */ function getValidWaypoints(waypoints, reArrangeIndexes = false) { - const sortedIndexes = _.map(_.keys(waypoints), (key) => parseInt(key.replace('waypoint', ''), 10)).sort(); + 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) { @@ -359,4 +368,5 @@ export { getValidWaypoints, isDistanceRequest, hasMissingSmartscanFields, + getWaypointIndex, };