From 725ef04450703947a76fd76babded19a15cd3e91 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Fri, 15 Sep 2023 07:05:32 +0100 Subject: [PATCH 1/2] prevent a route refresh when adding stops or deleting empty waypoints --- src/libs/TransactionUtils.js | 1 + src/libs/actions/Transaction.js | 35 ++++++++++++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/libs/TransactionUtils.js b/src/libs/TransactionUtils.js index 4ca8b48d284e..dccabd74772b 100644 --- a/src/libs/TransactionUtils.js +++ b/src/libs/TransactionUtils.js @@ -384,4 +384,5 @@ export { isDistanceRequest, hasMissingSmartscanFields, getWaypointIndex, + waypointHasValidAddress, }; diff --git a/src/libs/actions/Transaction.js b/src/libs/actions/Transaction.js index b16e8c8753ac..663dfd1c8564 100644 --- a/src/libs/actions/Transaction.js +++ b/src/libs/actions/Transaction.js @@ -5,6 +5,7 @@ import lodashHas from 'lodash/has'; import ONYXKEYS from '../../ONYXKEYS'; import * as CollectionUtils from '../CollectionUtils'; import * as API from '../API'; +import * as TransactionUtils from '../TransactionUtils'; let recentWaypoints = []; Onyx.connect({ @@ -55,15 +56,6 @@ function addStop(transactionID) { [`waypoint${newLastIndex}`]: {}, }, }, - - // Clear the existing route so that we don't show an old route - routes: { - route0: { - geometry: { - coordinates: null, - }, - }, - }, }); } @@ -124,7 +116,8 @@ function removeWaypoint(transactionID, currentIndex) { } const waypointValues = _.values(existingWaypoints); - waypointValues.splice(index, 1); + const removed = waypointValues.splice(index, 1); + const isRemovedWaypointEmpty = removed.length > 0 && !TransactionUtils.waypointHasValidAddress(removed[0]); const reIndexedWaypoints = {}; waypointValues.forEach((waypoint, idx) => { @@ -134,21 +127,27 @@ function removeWaypoint(transactionID, currentIndex) { // Onyx.merge won't remove the null nested object values, this is a workaround // to remove nested keys while also preserving other object keys // Doing a deep clone of the transaction to avoid mutating the original object and running into a cache issue when using Onyx.set - const newTransaction = { + let newTransaction = { ...transaction, comment: { ...transaction.comment, waypoints: reIndexedWaypoints, }, - // Clear the existing route so that we don't show an old route - routes: { - route0: { - geometry: { - coordinates: null, + }; + + if (!isRemovedWaypointEmpty) { + newTransaction = { + ...newTransaction, + // Clear the existing route so that we don't show an old route + routes: { + route0: { + geometry: { + coordinates: null, + }, }, }, - }, - }; + }; + } Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, newTransaction); } From d23d1e18884b2783f44482a9916793d2fc0e8e5b Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Sun, 17 Sep 2023 17:09:02 +0100 Subject: [PATCH 2/2] fetch the route only if the validated waypoints have changed --- src/components/DistanceRequest.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/DistanceRequest.js b/src/components/DistanceRequest.js index bf5a4cb9548b..74868cf6520b 100644 --- a/src/components/DistanceRequest.js +++ b/src/components/DistanceRequest.js @@ -109,10 +109,11 @@ function DistanceRequest({iou, iouType, report, transaction, mapboxAccessToken, const lastWaypointIndex = numberOfWaypoints - 1; const isLoadingRoute = lodashGet(transaction, 'comment.isLoading', false); const hasRouteError = !!lodashGet(transaction, 'errorFields.route'); - const haveWaypointsChanged = !_.isEqual(previousWaypoints, waypoints); const doesRouteExist = lodashHas(transaction, 'routes.route0.geometry.coordinates'); const validatedWaypoints = TransactionUtils.getValidWaypoints(waypoints); - const shouldFetchRoute = (!doesRouteExist || haveWaypointsChanged) && !isLoadingRoute && _.size(validatedWaypoints) > 1; + const previousValidatedWaypoints = usePrevious(validatedWaypoints); + const haveValidatedWaypointsChanged = !_.isEqual(previousValidatedWaypoints, validatedWaypoints); + const shouldFetchRoute = (!doesRouteExist || haveValidatedWaypointsChanged) && !isLoadingRoute && _.size(validatedWaypoints) > 1; const waypointMarkers = useMemo( () => _.filter(