-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41278 from Expensify/hayata-add-custom-hook-to-ge…
…t-connections-data Add a custom hook to fetch the connections field of the policy object
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 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,25 @@ | ||
import {useEffect} from 'react'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import {openPolicyAccountingPage} from '@libs/actions/PolicyConnections'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import useNetwork from './useNetwork'; | ||
|
||
export default function usePolicyConnections(policyID: string) { | ||
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); | ||
const {isOffline} = useNetwork(); | ||
const [hasConnectionsDataBeenFetched] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_HAS_CONNECTIONS_DATA_BEEN_FETCHED}${policy?.id ?? '0'}`, { | ||
initWithStoredValues: false, | ||
}); | ||
|
||
useEffect(() => { | ||
// When the accounting feature is not enabled, or if the connections data already exists, | ||
// there is no need to fetch the connections data. | ||
if (isOffline || !policy || !policy.areConnectionsEnabled || !!hasConnectionsDataBeenFetched || !!policy.connections) { | ||
return; | ||
} | ||
|
||
openPolicyAccountingPage(policy.id); | ||
}, [hasConnectionsDataBeenFetched, policy, isOffline]); | ||
|
||
return policy; | ||
} |