Skip to content

Commit

Permalink
Merge pull request #25980 from Expensify/jack-betterWaypointHandling
Browse files Browse the repository at this point in the history
  • Loading branch information
thienlnam authored Sep 1, 2023
2 parents 2137b05 + cab7be1 commit 3c73b7e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/components/DistanceRequest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useEffect, useState} from 'react';
import {ScrollView, View} from 'react-native';
import lodashGet from 'lodash/get';
import lodashHas from 'lodash/has';
import _ from 'underscore';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
Expand Down Expand Up @@ -68,14 +69,15 @@ function DistanceRequest({transactionID, transaction, mapboxAccessToken}) {

const lastWaypointIndex = numberOfWaypoints - 1;
const isLoadingRoute = lodashGet(transaction, 'comment.isLoading', false);
const hasRouteError = Boolean(lodashGet(transaction, 'errorFields.route'));
const hasRouteError = lodashHas(transaction, 'errorFields.route');
const previousWaypoints = usePrevious(waypoints);
const haveWaypointsChanged = !_.isEqual(previousWaypoints, waypoints);
const shouldFetchRoute = haveWaypointsChanged && !isOffline && !isLoadingRoute && TransactionUtils.validateWaypoints(waypoints);
const doesRouteExist = lodashHas(transaction, 'routes.route0.geometry.coordinates');
const shouldFetchRoute = (!doesRouteExist || haveWaypointsChanged) && !isLoadingRoute && TransactionUtils.validateWaypoints(waypoints);

const waypointMarkers = _.filter(
_.map(waypoints, (waypoint, key) => {
if (!waypoint || waypoint.lng === undefined || waypoint.lat === undefined) {
if (!waypoint || !lodashHas(waypoint, 'lat') || !lodashHas(waypoint, 'lng')) {
return;
}

Expand Down Expand Up @@ -126,14 +128,13 @@ function DistanceRequest({transactionID, transaction, mapboxAccessToken}) {
setShouldShowGradient(visibleAreaEnd < scrollContentHeight);
};

// Handle fetching the route when there are at least 2 waypoints
useEffect(() => {
if (!shouldFetchRoute) {
if (isOffline || !shouldFetchRoute) {
return;
}

Transaction.getRoute(transactionID, waypoints);
}, [shouldFetchRoute, transactionID, waypoints]);
}, [shouldFetchRoute, transactionID, waypoints, isOffline]);

useEffect(updateGradientVisibility, [scrollContainerHeight, scrollContentHeight]);

Expand Down
9 changes: 9 additions & 0 deletions src/libs/actions/Transaction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import lodashGet from 'lodash/get';
import lodashHas from 'lodash/has';
import ONYXKEYS from '../../ONYXKEYS';
import * as CollectionUtils from '../CollectionUtils';
import * as API from '../API';
Expand Down Expand Up @@ -93,6 +94,14 @@ function saveWaypoint(transactionID, index, waypoint) {
},
},
});

// You can save offline waypoints without verifying the address (we will geocode it on the backend)
// We're going to prevent saving those addresses in the recent waypoints though since they could be invalid addresses
// However, in the backend once we verify the address, we will save the waypoint in the recent waypoints NVP
if (!lodashHas(waypoint, 'lat') || !lodashHas(waypoint, 'lng')) {
return;
}

const recentWaypointAlreadyExists = _.find(recentWaypoints, (recentWaypoint) => recentWaypoint.address === waypoint.address);
if (!recentWaypointAlreadyExists) {
const clonedWaypoints = _.clone(recentWaypoints);
Expand Down
2 changes: 2 additions & 0 deletions src/pages/iou/WaypointEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ function WaypointEditor({transactionID, route: {params: {iouType = '', waypointI
// Therefore, we're going to save the waypoint as just the address, and the lat/long will be filled in on the backend
if (isOffline && waypointValue) {
const waypoint = {
lat: null,
lng: null,
address: waypointValue,
};

Expand Down

0 comments on commit 3c73b7e

Please sign in to comment.