From 01016dcaf4da744fe554cc839b47b5780f01c637 Mon Sep 17 00:00:00 2001 From: Ali Toshmatov Date: Wed, 18 Oct 2023 02:54:30 +0500 Subject: [PATCH] Updated types, introduces Waypoints type --- src/libs/TransactionUtils.ts | 4 ++-- src/libs/actions/Transaction.ts | 3 +-- src/types/onyx/Transaction.ts | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index da74003765a9..b1b35a97c959 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -6,7 +6,7 @@ import DateUtils from './DateUtils'; import {isExpensifyCard} from './CardUtils'; import * as NumberUtils from './NumberUtils'; import {RecentWaypoint, ReportAction, Transaction} from '../types/onyx'; -import {Receipt, Comment, WaypointCollection} from '../types/onyx/Transaction'; +import {Receipt, Comment, WaypointCollection, Waypoint} from '../types/onyx/Transaction'; type AdditionalTransactionChanges = {comment?: string; waypoints?: WaypointCollection}; @@ -399,7 +399,7 @@ function getAllReportTransactions(reportID?: string): Transaction[] { /** * Checks if a waypoint has a valid address */ -function waypointHasValidAddress(waypoint: RecentWaypoint | Record): boolean { +function waypointHasValidAddress(waypoint: RecentWaypoint | Waypoint): boolean { return !!waypoint?.address?.trim(); } diff --git a/src/libs/actions/Transaction.ts b/src/libs/actions/Transaction.ts index 40866f3e9326..8a7f0f7bd533 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -110,14 +110,13 @@ function removeWaypoint(transactionID: string, currentIndex: string) { const waypointValues = Object.values(existingWaypoints); const removed = waypointValues.splice(index, 1); + const isRemovedWaypointEmpty = removed.length > 0 && !TransactionUtils.waypointHasValidAddress(removed[0] ?? {}); // 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) => { reIndexedWaypoints[`waypoint${idx}`] = waypoint; diff --git a/src/types/onyx/Transaction.ts b/src/types/onyx/Transaction.ts index 34bc64075188..9636ac72a99b 100644 --- a/src/types/onyx/Transaction.ts +++ b/src/types/onyx/Transaction.ts @@ -3,8 +3,18 @@ import * as OnyxCommon from './OnyxCommon'; import CONST from '../../CONST'; import RecentWaypoint from './RecentWaypoint'; -// Default waypoint will be `{}`(empty object), that is why we are using Record -type WaypointCollection = Record>; +type Waypoint = { + /** The full address of the waypoint */ + address?: string; + + /** The lattitude of the waypoint */ + lat?: number; + + /** The longitude of the waypoint */ + lng?: number; +}; + +type WaypointCollection = Record; type Comment = { comment?: string; waypoints?: WaypointCollection; @@ -78,4 +88,4 @@ type Transaction = { }; export default Transaction; -export type {WaypointCollection, Comment, Receipt}; +export type {WaypointCollection, Comment, Receipt, Waypoint};