-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use useFetchRoute and use it in confirm page too
- Loading branch information
1 parent
42bed64
commit 962e999
Showing
3 changed files
with
39 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {isEqual} from 'lodash'; | ||
import {useEffect} from 'react'; | ||
import type {OnyxEntry} from 'react-native-onyx'; | ||
import * as IOUUtils from '@libs/IOUUtils'; | ||
import * as TransactionUtils from '@libs/TransactionUtils'; | ||
import * as TransactionAction from '@userActions/Transaction'; | ||
import type {IOUAction} from '@src/CONST'; | ||
import type {Transaction} from '@src/types/onyx'; | ||
import type {WaypointCollection} from '@src/types/onyx/Transaction'; | ||
import useNetwork from './useNetwork'; | ||
import usePrevious from './usePrevious'; | ||
|
||
export default function useFetchRoute(transaction: OnyxEntry<Transaction>, waypoints: WaypointCollection | undefined, action: IOUAction) { | ||
const {isOffline} = useNetwork(); | ||
const hasRouteError = !!transaction?.errorFields?.route; | ||
const hasRoute = TransactionUtils.hasRoute(transaction); | ||
const isRouteAbsentWithoutErrors = !hasRoute && !hasRouteError; | ||
const isLoadingRoute = transaction?.comment?.isLoading ?? false; | ||
const validatedWaypoints = TransactionUtils.getValidWaypoints(waypoints); | ||
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; | ||
|
||
useEffect(() => { | ||
if (isOffline || !shouldFetchRoute || !transaction?.transactionID) { | ||
return; | ||
} | ||
|
||
TransactionAction.getRoute(transaction.transactionID, validatedWaypoints, IOUUtils.shouldUseTransactionDraft(action)); | ||
}, [shouldFetchRoute, transaction?.transactionID, validatedWaypoints, isOffline, action]); | ||
|
||
return {shouldFetchRoute, validatedWaypoints}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters