Skip to content

Commit

Permalink
Merge pull request #41278 from Expensify/hayata-add-custom-hook-to-ge…
Browse files Browse the repository at this point in the history
…t-connections-data

Add a custom hook to fetch the connections field of the policy object
  • Loading branch information
aldo-expensify authored Apr 30, 2024
2 parents e138a59 + 3f6427f commit 5a9228d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/hooks/usePolicyWithConnections.ts
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;
}

0 comments on commit 5a9228d

Please sign in to comment.