Skip to content

Commit ac9b9ed

Browse files
committed
Extract waypoint address comparison into reusable utility function
1 parent 26f1fda commit ac9b9ed

4 files changed

Lines changed: 19 additions & 24 deletions

File tree

src/libs/TransactionUtils/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ function hasGPSWaypoints(transaction: OnyxEntry<Transaction>) {
189189
return !!waypoint?.keyForList?.startsWith('gps');
190190
}
191191

192+
/**
193+
* Compare two waypoint collections by their addresses only (ignoring coordinates/names), which is
194+
* the meaningful signal for "did the user change the route?". Numeric fields like lat/lng can drift
195+
* due to rounding in transaction backups, so they're excluded.
196+
*/
197+
function haveWaypointAddressesChanged(oldWaypoints: WaypointCollection | undefined, newWaypoints: WaypointCollection | undefined): boolean {
198+
const toAddresses = (collection: WaypointCollection | undefined) =>
199+
Object.fromEntries(Object.entries(collection ?? {}).map(([key, waypoint]) => [key, waypoint && 'address' in waypoint ? waypoint.address : undefined]));
200+
return !deepEqual(toAddresses(oldWaypoints), toAddresses(newWaypoints));
201+
}
202+
192203
function isMapDistanceRequest(transaction: OnyxEntry<Transaction>): boolean {
193204
// This is used during the expense creation flow before the transaction has been saved to the server
194205
if (transaction && Object.hasOwn(transaction, 'iouRequestType')) {
@@ -2883,6 +2894,7 @@ export {
28832894
isReceiptBeingScanned,
28842895
didReceiptScanSucceed,
28852896
getValidWaypoints,
2897+
haveWaypointAddressesChanged,
28862898
isDistanceRequest,
28872899
isMapDistanceRequest,
28882900
isGPSDistanceRequest,

src/libs/actions/IOU/UpdateMoneyRequest.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {deepEqual} from 'fast-equals';
21
// eslint-disable-next-line you-dont-need-lodash-underscore/union-by
32
import lodashUnionBy from 'lodash/unionBy';
43
import type {NullishDeep, OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
@@ -29,6 +28,7 @@ import {
2928
getClearedPendingFields,
3029
getMerchant,
3130
getUpdatedTransaction,
31+
haveWaypointAddressesChanged,
3232
isDistanceRequest as isDistanceRequestTransactionUtils,
3333
isFetchingWaypointsFromServer,
3434
isOnHold,
@@ -952,15 +952,7 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U
952952
// only edited the distance number. Detect whether the addresses actually changed so we can skip the
953953
// optimistic side effects (pending field, route clearing, render-path swap to interactive map) that
954954
// would otherwise make the parent map briefly disappear on a pure distance edit.
955-
const haveWaypointsActuallyChanged =
956-
'waypoints' in transactionChanges &&
957-
(() => {
958-
const oldWaypoints = transaction?.comment?.waypoints ?? {};
959-
const newWaypoints = transactionChanges.waypoints ?? {};
960-
const getAddresses = (collection: WaypointCollection) =>
961-
Object.fromEntries(Object.entries(collection).map(([key, waypoint]) => [key, waypoint && 'address' in waypoint ? waypoint.address : undefined]));
962-
return !deepEqual(getAddresses(oldWaypoints), getAddresses(newWaypoints));
963-
})();
955+
const haveWaypointsActuallyChanged = 'waypoints' in transactionChanges && haveWaypointAddressesChanged(transaction?.comment?.waypoints, transactionChanges.waypoints);
964956
const shouldSuppressWaypointsAsPending = 'waypoints' in transactionChanges && !haveWaypointsActuallyChanged;
965957

966958
// Step 1: Set any "pending fields" (ones updated while the user was offline) to have error messages in the failureData

src/pages/iou/request/step/IOURequestStepDistance.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import Navigation from '@libs/Navigation/Navigation';
4343
import OnyxTabNavigator, {TabScreenWithFocusTrapWrapper, TopTab} from '@libs/Navigation/OnyxTabNavigator';
4444
import {roundToTwoDecimalPlaces} from '@libs/NumberUtils';
4545
import {isPolicyExpenseChat as isPolicyExpenseChatUtil} from '@libs/ReportUtils';
46-
import {getDistanceInMeters, getRateID, getRequestType, hasRoute, isCustomUnitRateIDForP2P, isWaypointNullIsland} from '@libs/TransactionUtils';
46+
import {getDistanceInMeters, getRateID, getRequestType, hasRoute, haveWaypointAddressesChanged, isCustomUnitRateIDForP2P, isWaypointNullIsland} from '@libs/TransactionUtils';
4747
import CONST from '@src/CONST';
4848
import type {IOUType} from '@src/CONST';
4949
import ONYXKEYS from '@src/ONYXKEYS';
@@ -509,11 +509,8 @@ function IOURequestStepDistance({
509509

510510
// If nothing was changed, simply go to transaction thread
511511
// We compare only addresses because numbers are rounded while backup
512-
const oldWaypoints = transactionBackup?.comment?.waypoints ?? {};
513-
const oldAddresses = Object.fromEntries(Object.entries(oldWaypoints).map(([key, waypoint]) => [key, 'address' in waypoint ? waypoint.address : {}]));
514-
const addresses = Object.fromEntries(Object.entries(waypoints).map(([key, waypoint]) => [key, 'address' in waypoint ? waypoint.address : {}]));
515512
const hasRouteChanged = !deepEqual(transactionBackup?.routes, transaction?.routes);
516-
if (deepEqual(oldAddresses, addresses)) {
513+
if (!haveWaypointAddressesChanged(transactionBackup?.comment?.waypoints, waypoints)) {
517514
transactionWasSaved.current = true;
518515
navigateBackAfterSave();
519516
return;
@@ -601,10 +598,7 @@ function IOURequestStepDistance({
601598

602599
// Check if waypoints were edited on the map tab before the user switched to manual.
603600
// If so, we must still send the update even if the distance value itself didn't change.
604-
const oldWaypoints = transactionBackup?.comment?.waypoints ?? {};
605-
const oldAddresses = Object.fromEntries(Object.entries(oldWaypoints).map(([key, waypoint]) => [key, 'address' in waypoint ? waypoint.address : {}]));
606-
const currentAddresses = Object.fromEntries(Object.entries(waypoints).map(([key, waypoint]) => [key, 'address' in waypoint ? waypoint.address : {}]));
607-
const haveWaypointsChanged = !deepEqual(oldAddresses, currentAddresses);
601+
const haveWaypointsChanged = haveWaypointAddressesChanged(transactionBackup?.comment?.waypoints, waypoints);
608602

609603
if (!isDistanceChanged && !isDistanceUnitChanged && !haveWaypointsChanged) {
610604
transactionWasSaved.current = true;

src/pages/iou/request/step/IOURequestStepDistanceMap.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
3838
import {shouldUseTransactionDraft} from '@libs/IOUUtils';
3939
import Navigation from '@libs/Navigation/Navigation';
4040
import {isPolicyExpenseChat as isPolicyExpenseChatUtil} from '@libs/ReportUtils';
41-
import {getDistanceInMeters, getRateID, getRequestType, hasRoute, isCustomUnitRateIDForP2P, isWaypointNullIsland} from '@libs/TransactionUtils';
41+
import {getDistanceInMeters, getRateID, getRequestType, hasRoute, haveWaypointAddressesChanged, isCustomUnitRateIDForP2P, isWaypointNullIsland} from '@libs/TransactionUtils';
4242
import CONST from '@src/CONST';
4343
import ONYXKEYS from '@src/ONYXKEYS';
4444
import ROUTES from '@src/ROUTES';
@@ -441,11 +441,8 @@ function IOURequestStepDistanceMap({
441441

442442
// If nothing was changed, simply go to transaction thread
443443
// We compare only addresses because numbers are rounded while backup
444-
const oldWaypoints = transactionBackup?.comment?.waypoints ?? {};
445-
const oldAddresses = Object.fromEntries(Object.entries(oldWaypoints).map(([key, waypoint]) => [key, 'address' in waypoint ? waypoint.address : {}]));
446-
const addresses = Object.fromEntries(Object.entries(waypoints).map(([key, waypoint]) => [key, 'address' in waypoint ? waypoint.address : {}]));
447444
const hasRouteChanged = !deepEqual(transactionBackup?.routes, transaction?.routes);
448-
if (deepEqual(oldAddresses, addresses)) {
445+
if (!haveWaypointAddressesChanged(transactionBackup?.comment?.waypoints, waypoints)) {
449446
navigateBack();
450447
return;
451448
}

0 commit comments

Comments
 (0)