Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Distance - Unable to save distance when Start and Stop waypoints are valid address #52736

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/hooks/useFetchRoute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isEqual from 'lodash/isEqual';
import {useEffect} from 'react';
import {useEffect, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import * as TransactionUtils from '@libs/TransactionUtils';
import * as TransactionAction from '@userActions/Transaction';
Expand All @@ -18,6 +18,7 @@ export default function useFetchRoute(
transactionState: TransactionState = CONST.TRANSACTION.STATE.CURRENT,
) {
const {isOffline} = useNetwork();
const [isInitialMount, setIsInitialMount] = useState(true);
const hasRouteError = !!transaction?.errorFields?.route;
const hasRoute = TransactionUtils.hasRoute(transaction);
const isRouteAbsentWithoutErrors = !hasRoute && !hasRouteError;
Expand All @@ -26,14 +27,19 @@ export default function useFetchRoute(
const previousValidatedWaypoints = usePrevious(validatedWaypoints);
const haveValidatedWaypointsChanged = !isEqual(previousValidatedWaypoints, validatedWaypoints);
const isDistanceRequest = TransactionUtils.isDistanceRequest(transaction);
const shouldFetchRoute = isDistanceRequest && (isRouteAbsentWithoutErrors || haveValidatedWaypointsChanged) && !isLoadingRoute && Object.keys(validatedWaypoints).length > 1;
const shouldFetchRoute =
isDistanceRequest &&
(isRouteAbsentWithoutErrors || haveValidatedWaypointsChanged || (isInitialMount && hasRouteError)) &&
!isLoadingRoute &&
Object.keys(validatedWaypoints).length > 1;

useEffect(() => {
if (isOffline || !shouldFetchRoute || !transaction?.transactionID) {
return;
}

TransactionAction.getRoute(transaction.transactionID, validatedWaypoints, transactionState);
setIsInitialMount(false);
}, [shouldFetchRoute, transaction?.transactionID, validatedWaypoints, isOffline, action, transactionState]);

return {shouldFetchRoute, validatedWaypoints};
Expand Down
5 changes: 3 additions & 2 deletions src/pages/iou/request/step/IOURequestStepDistance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function IOURequestStepDistance({
[optimisticWaypoints, transaction],
);

const backupWaypoints = transactionBackup?.pendingFields?.waypoints ? transactionBackup?.comment?.waypoints : undefined;
const backupWaypoints = transactionBackup?.comment?.waypoints;
// When online, fetch the backup route to ensure the map is populated even if the user does not save the transaction.
// Fetch the backup route first to ensure the backup transaction map is updated before the main transaction map.
// This prevents a scenario where the main map loads, the user dismisses the map editor, and the backup map has not yet loaded due to delay.
Expand Down Expand Up @@ -441,7 +441,7 @@ function IOURequestStepDistance({

const submitWaypoints = useCallback(() => {
// If there is any error or loading state, don't let user go to next page.
if (duplicateWaypointsError || atLeastTwoDifferentWaypointsError || hasRouteError || isLoadingRoute || (!isEditing && isLoading)) {
if (duplicateWaypointsError || atLeastTwoDifferentWaypointsError || (hasRouteError && !isOffline) || isLoadingRoute || (!isEditing && isLoading)) {
setShouldShowAtLeastTwoDifferentWaypointsError(true);
return;
}
Expand Down Expand Up @@ -487,6 +487,7 @@ function IOURequestStepDistance({
transaction?.routes,
report?.reportID,
policy,
isOffline,
]);

const renderItem = useCallback(
Expand Down
Loading