Skip to content

Commit

Permalink
Merge pull request #37585 from tienifr/fix/33919-followup
Browse files Browse the repository at this point in the history
  • Loading branch information
francoisl authored Mar 4, 2024
2 parents 8b026d0 + d90f433 commit c932d31
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/components/DistanceRequest/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,22 @@ function DistanceRequest({transactionID = '', report, transaction, route, isEdit
}

const newWaypoints: WaypointCollection = {};
let emptyWaypointIndex = -1;
data.forEach((waypoint, index) => {
newWaypoints[`waypoint${index}`] = waypoints?.[waypoint] ?? {};
// Find waypoint that BECOMES empty after dragging
if (isEmptyObject(newWaypoints[`waypoint${index}`]) && !isEmptyObject(waypoints[`waypoint${index}`])) {
emptyWaypointIndex = index;
}
});

setOptimisticWaypoints(newWaypoints);
// eslint-disable-next-line rulesdir/no-thenable-actions-in-views
TransactionUserActions.updateWaypoints(transactionID, newWaypoints).then(() => {
Promise.all([TransactionUserActions.removeWaypoint(transaction, emptyWaypointIndex.toString()), TransactionUserActions.updateWaypoints(transactionID, newWaypoints)]).then(() => {
setOptimisticWaypoints(undefined);
});
},
[transactionID, waypoints, waypointsList],
[transactionID, transaction, waypoints, waypointsList],
);

const submitWaypoints = useCallback(() => {
Expand Down
9 changes: 4 additions & 5 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function saveWaypoint(transactionID: string, index: string, waypoint: RecentWayp
}
}

function removeWaypoint(transaction: OnyxEntry<Transaction>, currentIndex: string, isDraft?: boolean) {
function removeWaypoint(transaction: OnyxEntry<Transaction>, currentIndex: string, isDraft?: boolean): Promise<void> {
// Index comes from the route params and is a string
const index = Number(currentIndex);
const existingWaypoints = transaction?.comment?.waypoints ?? {};
Expand All @@ -116,7 +116,7 @@ function removeWaypoint(transaction: OnyxEntry<Transaction>, currentIndex: strin
const waypointValues = Object.values(existingWaypoints);
const removed = waypointValues.splice(index, 1);
if (removed.length === 0) {
return;
return Promise.resolve();
}

const isRemovedWaypointEmpty = removed.length > 0 && !TransactionUtils.waypointHasValidAddress(removed[0] ?? {});
Expand Down Expand Up @@ -166,10 +166,9 @@ function removeWaypoint(transaction: OnyxEntry<Transaction>, currentIndex: strin
};
}
if (isDraft) {
Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction?.transactionID}`, newTransaction);
return;
return Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction?.transactionID}`, newTransaction);
}
Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction?.transactionID}`, newTransaction);
return Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction?.transactionID}`, newTransaction);
}

function getOnyxDataForRouteRequest(transactionID: string, isDraft = false): OnyxData {
Expand Down
9 changes: 7 additions & 2 deletions src/pages/iou/request/step/IOURequestStepDistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,22 @@ function IOURequestStepDistance({
}

const newWaypoints = {};
let emptyWaypointIndex = -1;
_.each(data, (waypoint, index) => {
newWaypoints[`waypoint${index}`] = lodashGet(waypoints, waypoint, {});
// Find waypoint that BECOMES empty after dragging
if (_.isEmpty(newWaypoints[`waypoint${index}`]) && !_.isEmpty(lodashGet(waypoints, `waypoint${index}`, {}))) {
emptyWaypointIndex = index;
}
});

setOptimisticWaypoints(newWaypoints);
// eslint-disable-next-line rulesdir/no-thenable-actions-in-views
Transaction.updateWaypoints(transactionID, newWaypoints, true).then(() => {
Promise.all([Transaction.removeWaypoint(transaction, emptyWaypointIndex.toString(), true), Transaction.updateWaypoints(transactionID, newWaypoints, true)]).then(() => {
setOptimisticWaypoints(null);
});
},
[transactionID, waypoints, waypointsList],
[transactionID, transaction, waypoints, waypointsList],
);

const submitWaypoints = useCallback(() => {
Expand Down

0 comments on commit c932d31

Please sign in to comment.