Skip to content

Commit

Permalink
Merge pull request #27496 from akinwale/task-26838
Browse files Browse the repository at this point in the history
fix: prevent a route refresh if the list of valid waypoints have not changed
  • Loading branch information
danieldoglas authored Sep 18, 2023
2 parents 2792030 + d23d1e1 commit 502ceb2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/components/DistanceRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/libs/TransactionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,5 @@ export {
isDistanceRequest,
hasMissingSmartscanFields,
getWaypointIndex,
waypointHasValidAddress,
};
35 changes: 17 additions & 18 deletions src/libs/actions/Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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,
},
},
},
});
}

Expand Down Expand Up @@ -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) => {
Expand All @@ -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);
}

Expand Down

0 comments on commit 502ceb2

Please sign in to comment.