Skip to content

Commit

Permalink
Updated types, introduces Waypoints type
Browse files Browse the repository at this point in the history
  • Loading branch information
alitoshmatov committed Oct 17, 2023
1 parent 61574b7 commit 01016dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -399,7 +399,7 @@ function getAllReportTransactions(reportID?: string): Transaction[] {
/**
* Checks if a waypoint has a valid address
*/
function waypointHasValidAddress(waypoint: RecentWaypoint | Record<string, never>): boolean {
function waypointHasValidAddress(waypoint: RecentWaypoint | Waypoint): boolean {
return !!waypoint?.address?.trim();
}

Expand Down
3 changes: 1 addition & 2 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions src/types/onyx/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, never>
type WaypointCollection = Record<string, RecentWaypoint | Record<string, never>>;
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<string, RecentWaypoint | Waypoint>;
type Comment = {
comment?: string;
waypoints?: WaypointCollection;
Expand Down Expand Up @@ -78,4 +88,4 @@ type Transaction = {
};

export default Transaction;
export type {WaypointCollection, Comment, Receipt};
export type {WaypointCollection, Comment, Receipt, Waypoint};

0 comments on commit 01016dc

Please sign in to comment.