Skip to content

Commit

Permalink
Merge pull request #29811 from alitoshmatov/29748/disapearing-finish-…
Browse files Browse the repository at this point in the history
…waypoint

Fix disappearing finish waypoint when start waypoint is selected
  • Loading branch information
neil-marcellini authored Oct 17, 2023
2 parents 0cfbec0 + 01016dc commit fc8ce73
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
4 changes: 1 addition & 3 deletions src/components/DistanceRequest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 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 | null): boolean {
function waypointHasValidAddress(waypoint: RecentWaypoint | Waypoint): 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
16 changes: 8 additions & 8 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function createInitialWaypoints(transactionID: string) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
comment: {
waypoints: {
waypoint0: null,
waypoint1: null,
waypoint0: {},
waypoint1: {},
},
},
});
Expand Down Expand Up @@ -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) => {
Expand Down
15 changes: 13 additions & 2 deletions src/types/onyx/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ import * as OnyxCommon from './OnyxCommon';
import CONST from '../../CONST';
import RecentWaypoint from './RecentWaypoint';

type WaypointCollection = Record<string, RecentWaypoint | null>;
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 +89,4 @@ type Transaction = {
};

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

0 comments on commit fc8ce73

Please sign in to comment.