1- import { useFocusEffect } from '@react-navigation/native' ;
2- import { validTransactionDraftIDsSelector } from '@selectors/TransactionDraft' ;
3- import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
4- import { Keyboard , View } from 'react-native' ;
1+ import React , { useEffect , useMemo , useState } from 'react' ;
2+ import { View } from 'react-native' ;
53import FocusTrapContainerElement from '@components/FocusTrap/FocusTrapContainerElement' ;
64import HeaderWithBackButton from '@components/HeaderWithBackButton' ;
75import ScreenWrapper from '@components/ScreenWrapper' ;
86import TabSelector from '@components/TabSelector/TabSelector' ;
9- import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails' ;
107import useLocalize from '@hooks/useLocalize' ;
118import useOnyx from '@hooks/useOnyx' ;
12- import usePersonalPolicy from '@hooks/usePersonalPolicy' ;
139import usePolicyForTransaction from '@hooks/usePolicyForTransaction' ;
14- import usePrevious from '@hooks/usePrevious ' ;
10+ import useResetIOUType from '@hooks/useResetIOUType ' ;
1511import useThemeStyles from '@hooks/useThemeStyles' ;
1612import { canUseTouchScreen } from '@libs/DeviceCapabilities' ;
1713import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID' ;
1814import Navigation from '@libs/Navigation/Navigation' ;
1915import OnyxTabNavigator , { TabScreenWithFocusTrapWrapper , TopTab } from '@libs/Navigation/OnyxTabNavigator' ;
20- import { hasOnlyPersonalPolicies as hasOnlyPersonalPoliciesUtil } from '@libs/PolicyUtils' ;
2116import { getPayeeName } from '@libs/ReportUtils' ;
2217import { endSpan } from '@libs/telemetry/activeSpans' ;
2318import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper' ;
24- import type { IOURequestType } from '@userActions/IOU' ;
25- import { initMoneyRequest } from '@userActions/IOU' ;
2619import CONST from '@src/CONST' ;
2720import ONYXKEYS from '@src/ONYXKEYS' ;
2821import type SCREENS from '@src/SCREENS' ;
2922import type { SelectedTabRequest } from '@src/types/onyx' ;
30- import { isEmptyObject } from '@src/types/utils/EmptyObject' ;
3123import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue' ;
3224import IOURequestStepDistanceGPS from './step/IOURequestStepDistanceGPS' ;
3325import IOURequestStepDistanceManual from './step/IOURequestStepDistanceManual' ;
@@ -51,22 +43,13 @@ function DistanceRequestStartPage({
5143 const styles = useThemeStyles ( ) ;
5244 const { translate} = useLocalize ( ) ;
5345 const [ report ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ) ;
54- const [ parentReport ] = useOnyx ( `${ ONYXKEYS . COLLECTION . REPORT } ${ report ?. parentReportID } ` ) ;
5546 const [ transaction ] = useOnyx ( `${ ONYXKEYS . COLLECTION . TRANSACTION_DRAFT } ${ getNonEmptyStringOnyxID ( route ?. params . transactionID ) } ` ) ;
5647 const { policy} = usePolicyForTransaction ( { transaction, reportPolicyID : report ?. policyID , action, iouType} ) ;
5748 const [ selectedTab , selectedTabResult ] = useOnyx ( `${ ONYXKEYS . COLLECTION . SELECTED_TAB } ${ CONST . TAB . DISTANCE_REQUEST_TYPE } ` ) ;
5849 const [ lastDistanceExpenseType ] = useOnyx ( ONYXKEYS . NVP_LAST_DISTANCE_EXPENSE_TYPE ) ;
5950 const isLoadingSelectedTab = isLoadingOnyxValue ( selectedTabResult ) ;
60- const [ allPolicies ] = useOnyx ( ONYXKEYS . COLLECTION . POLICY ) ;
61- const [ lastSelectedDistanceRates ] = useOnyx ( ONYXKEYS . NVP_LAST_SELECTED_DISTANCE_RATES ) ;
62- const [ currentDate ] = useOnyx ( ONYXKEYS . CURRENT_DATE ) ;
6351 const isTrackDistanceExpense = iouType === CONST . IOU . TYPE . TRACK ;
6452
65- const hasOnlyPersonalPolicies = useMemo ( ( ) => hasOnlyPersonalPoliciesUtil ( allPolicies ) , [ allPolicies ] ) ;
66- const currentUserPersonalDetails = useCurrentUserPersonalDetails ( ) ;
67- const personalPolicy = usePersonalPolicy ( ) ;
68- const [ draftTransactionIDs ] = useOnyx ( ONYXKEYS . COLLECTION . TRANSACTION_DRAFT , { selector : validTransactionDraftIDsSelector } ) ;
69-
7053 const tabTitles = {
7154 [ CONST . IOU . TYPE . REQUEST ] : translate ( 'iou.trackDistance' ) ,
7255 [ CONST . IOU . TYPE . SUBMIT ] : translate ( 'iou.trackDistance' ) ,
@@ -79,8 +62,6 @@ function DistanceRequestStartPage({
7962 [ CONST . IOU . TYPE . CREATE ] : translate ( 'iou.trackDistance' ) ,
8063 } ;
8164
82- const isFromGlobalCreate = isEmptyObject ( report ?. reportID ) ;
83-
8465 const transactionRequestType = useMemo ( ( ) => {
8566 if ( ! transaction ?. iouRequestType ) {
8667 return lastDistanceExpenseType ?? selectedTab ?? CONST . IOU . REQUEST_TYPE . DISTANCE_MAP ;
@@ -89,7 +70,15 @@ function DistanceRequestStartPage({
8970 return transaction . iouRequestType ;
9071 } , [ transaction ?. iouRequestType , selectedTab , lastDistanceExpenseType ] ) ;
9172
92- const prevTransactionReportID = usePrevious ( transaction ?. reportID ) ;
73+ const resetIOUTypeIfChanged = useResetIOUType ( {
74+ reportID,
75+ report,
76+ transaction,
77+ isLoadingSelectedTab,
78+ transactionRequestType,
79+ policy,
80+ isTrackDistanceExpense,
81+ } ) ;
9382
9483 useEffect ( ( ) => {
9584 endSpan ( CONST . TELEMETRY . SPAN_OPEN_CREATE_EXPENSE ) ;
@@ -99,60 +88,6 @@ function DistanceRequestStartPage({
9988 Navigation . closeRHPFlow ( ) ;
10089 } ;
10190
102- const resetIOUTypeIfChanged = useCallback (
103- ( newIOUType : IOURequestType ) => {
104- Keyboard . dismiss ( ) ;
105- if ( transaction ?. iouRequestType === newIOUType ) {
106- return ;
107- }
108- initMoneyRequest ( {
109- reportID,
110- policy,
111- personalPolicy,
112- isFromGlobalCreate,
113- isTrackDistanceExpense,
114- isFromFloatingActionButton : transaction ?. isFromFloatingActionButton ?? transaction ?. isFromGlobalCreate ?? isFromGlobalCreate ,
115- currentIouRequestType : transaction ?. iouRequestType ,
116- newIouRequestType : newIOUType ,
117- report,
118- parentReport,
119- currentDate,
120- lastSelectedDistanceRates,
121- currentUserPersonalDetails,
122- hasOnlyPersonalPolicies,
123- draftTransactionIDs,
124- } ) ;
125- } ,
126- [
127- transaction ?. iouRequestType ,
128- transaction ?. isFromGlobalCreate ,
129- transaction ?. isFromFloatingActionButton ,
130- reportID ,
131- policy ,
132- personalPolicy ,
133- isFromGlobalCreate ,
134- report ,
135- parentReport ,
136- currentDate ,
137- lastSelectedDistanceRates ,
138- currentUserPersonalDetails ,
139- isTrackDistanceExpense ,
140- hasOnlyPersonalPolicies ,
141- draftTransactionIDs ,
142- ] ,
143- ) ;
144-
145- // Clear out the temporary expense if the reportID in the URL has changed from the transaction's reportID.
146- useFocusEffect (
147- useCallback ( ( ) => {
148- // The test transaction can change the reportID of the transaction on the flow so we should prevent the reportID from being reverted again.
149- if ( transaction ?. reportID === reportID || isLoadingSelectedTab || ! transactionRequestType || prevTransactionReportID !== transaction ?. reportID ) {
150- return ;
151- }
152- resetIOUTypeIfChanged ( transactionRequestType ) ;
153- } , [ transaction ?. reportID , reportID , resetIOUTypeIfChanged , transactionRequestType , isLoadingSelectedTab , prevTransactionReportID ] ) ,
154- ) ;
155-
15691 const [ headerWithBackBtnContainerElement , setHeaderWithBackButtonContainerElement ] = useState < HTMLElement | null > ( null ) ;
15792 const [ tabBarContainerElement , setTabBarContainerElement ] = useState < HTMLElement | null > ( null ) ;
15893 const [ activeTabContainerElement , setActiveTabContainerElement ] = useState < HTMLElement | null > ( null ) ;
@@ -167,7 +102,6 @@ function DistanceRequestStartPage({
167102 iouType = { iouType }
168103 policyID = { policy ?. id }
169104 accessVariants = { [ CONST . IOU . ACCESS_VARIANTS . CREATE ] }
170- allPolicies = { allPolicies }
171105 >
172106 < ScreenWrapper
173107 shouldEnableKeyboardAvoidingView = { selectedTab === CONST . TAB_REQUEST . DISTANCE_ODOMETER }
0 commit comments