diff --git a/src/components/DistanceRequest/index.js b/src/components/DistanceRequest/index.js index 416fefc5af89..db0571cdcdaf 100644 --- a/src/components/DistanceRequest/index.js +++ b/src/components/DistanceRequest/index.js @@ -2,7 +2,6 @@ import React, {useCallback, useEffect, useMemo, useState, useRef} from 'react'; import {View} from 'react-native'; import {withOnyx} from 'react-native-onyx'; import lodashGet from 'lodash/get'; -import lodashIsEmpty from 'lodash/isEmpty'; import PropTypes from 'prop-types'; import _ from 'underscore'; import ROUTES from '../../ROUTES'; @@ -169,8 +168,7 @@ function DistanceRequest({transactionID, report, transaction, route, isEditingRe const newWaypoints = {}; _.each(data, (waypoint, index) => { - const newWaypoint = lodashGet(waypoints, waypoint, {}); - newWaypoints[`waypoint${index}`] = lodashIsEmpty(newWaypoint) ? null : newWaypoint; + newWaypoints[`waypoint${index}`] = lodashGet(waypoints, waypoint, {}); }); setOptimisticWaypoints(newWaypoints); diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index 6a45bef5780b..31cad217666c 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 | null): boolean { +function waypointHasValidAddress(waypoint: RecentWaypoint | Waypoint): 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 8653b038e381..8a7f0f7bd533 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -32,8 +32,8 @@ function createInitialWaypoints(transactionID: string) { Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, { comment: { waypoints: { - waypoint0: null, - waypoint1: null, + waypoint0: {}, + waypoint1: {}, }, }, }); @@ -107,15 +107,15 @@ 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); + 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 reIndexedWaypoints: WaypointCollection = {}; waypointValues.forEach((waypoint, idx) => { diff --git a/src/types/onyx/Transaction.ts b/src/types/onyx/Transaction.ts index 9da3ef610571..21be3c49497e 100644 --- a/src/types/onyx/Transaction.ts +++ b/src/types/onyx/Transaction.ts @@ -3,7 +3,18 @@ import * as OnyxCommon from './OnyxCommon'; import CONST from '../../CONST'; import RecentWaypoint from './RecentWaypoint'; -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 +89,4 @@ type Transaction = { }; export default Transaction; -export type {WaypointCollection, Comment, Receipt}; +export type {WaypointCollection, Comment, Receipt, Waypoint};