From 61574b778857903ddb6e528962d4a7a802383fc5 Mon Sep 17 00:00:00 2001 From: Ali Toshmatov Date: Wed, 18 Oct 2023 02:41:44 +0500 Subject: [PATCH] Updated waypoint removing logic and types --- src/libs/TransactionUtils.ts | 4 ++-- src/libs/actions/Transaction.ts | 13 +++++++------ src/types/onyx/Transaction.ts | 3 ++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index 77fc4f04f99d..da74003765a9 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -399,7 +399,7 @@ function getAllReportTransactions(reportID?: string): Transaction[] { /** * Checks if a waypoint has a valid address */ -function waypointHasValidAddress(waypoint: RecentWaypoint | null): boolean { +function waypointHasValidAddress(waypoint: RecentWaypoint | Record): boolean { return !!waypoint?.address?.trim(); } @@ -423,7 +423,7 @@ function getValidWaypoints(waypoints: WaypointCollection, reArrangeIndexes = fal let lastWaypointIndex = -1; - return waypointValues.reduce((acc, currentWaypoint, index) => { + return waypointValues.reduce((acc, currentWaypoint, index) => { const previousWaypoint = waypointValues[lastWaypointIndex]; // Check if the waypoint has a valid address diff --git a/src/libs/actions/Transaction.ts b/src/libs/actions/Transaction.ts index c3b1d982c445..40866f3e9326 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -107,15 +107,16 @@ function removeWaypoint(transactionID: string, currentIndex: string) { const transaction = allTransactions?.[transactionID] ?? {}; const existingWaypoints = transaction?.comment?.waypoints ?? {}; const totalWaypoints = Object.keys(existingWaypoints).length; - // Prevents removing the starting or ending waypoint but clear the stored address only if there are only two waypoints - if (totalWaypoints === 2 && (index === 0 || index === totalWaypoints - 1)) { - saveWaypoint(transactionID, index.toString(), null); - return; - } const waypointValues = Object.values(existingWaypoints); const removed = waypointValues.splice(index, 1); - const isRemovedWaypointEmpty = removed.length > 0 && !TransactionUtils.waypointHasValidAddress(removed[0] ?? null); + + // When there are only two waypoints we are adding empty waypoint back + if (totalWaypoints === 2 && (index === 0 || index === totalWaypoints - 1)) { + waypointValues.splice(index, 0, {}); + } + + const isRemovedWaypointEmpty = removed.length > 0 && !TransactionUtils.waypointHasValidAddress(removed[0] ?? {}); const reIndexedWaypoints: WaypointCollection = {}; waypointValues.forEach((waypoint, idx) => { diff --git a/src/types/onyx/Transaction.ts b/src/types/onyx/Transaction.ts index 292addbb142e..34bc64075188 100644 --- a/src/types/onyx/Transaction.ts +++ b/src/types/onyx/Transaction.ts @@ -3,7 +3,8 @@ import * as OnyxCommon from './OnyxCommon'; import CONST from '../../CONST'; import RecentWaypoint from './RecentWaypoint'; -type WaypointCollection = Record; +// Default waypoint will be `{}`(empty object), that is why we are using Record +type WaypointCollection = Record>; type Comment = { comment?: string; waypoints?: WaypointCollection;