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: prevent a route refresh if the list of valid waypoints have not changed #27496

Merged
merged 2 commits into from
Sep 18, 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
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from #30176
The route was rerendered if you add an empty stop, we resolved this by returning early if there are no removed waypoints, i.e.

    if (removed.length === 0) {
        return;
    }

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
Loading