Skip to content

Commit

Permalink
Updated waypoint removing logic and types
Browse files Browse the repository at this point in the history
  • Loading branch information
alitoshmatov committed Oct 17, 2023
1 parent 19c8e25 commit 61574b7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, never>): boolean {
return !!waypoint?.address?.trim();
}

Expand All @@ -423,7 +423,7 @@ function getValidWaypoints(waypoints: WaypointCollection, reArrangeIndexes = fal

let lastWaypointIndex = -1;

return waypointValues.reduce((acc, currentWaypoint, index) => {
return waypointValues.reduce<WaypointCollection>((acc, currentWaypoint, index) => {
const previousWaypoint = waypointValues[lastWaypointIndex];

// Check if the waypoint has a valid address
Expand Down
13 changes: 7 additions & 6 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion src/types/onyx/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as OnyxCommon from './OnyxCommon';
import CONST from '../../CONST';
import RecentWaypoint from './RecentWaypoint';

type WaypointCollection = Record<string, RecentWaypoint | null>;
// Default waypoint will be `{}`(empty object), that is why we are using Record<string, never>
type WaypointCollection = Record<string, RecentWaypoint | Record<string, never>>;
type Comment = {
comment?: string;
waypoints?: WaypointCollection;
Expand Down

0 comments on commit 61574b7

Please sign in to comment.