diff --git a/src/components/ActivePolicyProvider.tsx b/src/components/ActivePolicyProvider.tsx new file mode 100644 index 000000000000..8ea1f7bb0bf2 --- /dev/null +++ b/src/components/ActivePolicyProvider.tsx @@ -0,0 +1,35 @@ +import useOnyx from '@hooks/useOnyx'; + +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy} from '@src/types/onyx'; +import type ChildrenProps from '@src/types/utils/ChildrenProps'; + +import type {OnyxEntry} from 'react-native-onyx'; + +import React, {createContext, useContext} from 'react'; + +type ActivePolicyContextValue = { + activePolicyID: string | undefined; + activePolicy: OnyxEntry; +}; + +const ActivePolicyContext = createContext({activePolicyID: undefined, activePolicy: undefined}); + +const activePolicySelector = (policy: OnyxEntry) => (policy?.type !== CONST.POLICY.TYPE.PERSONAL ? policy : undefined); + +function ActivePolicyProvider({children}: ChildrenProps) { + const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); + const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, { + selector: activePolicySelector, + }); + + return {children}; +} + +function useActivePolicyContext() { + return useContext(ActivePolicyContext); +} + +export default ActivePolicyProvider; +export {useActivePolicyContext}; diff --git a/src/components/OnyxListItemProvider.tsx b/src/components/OnyxListItemProvider.tsx index d0e008bfa354..54f8e65c3620 100644 --- a/src/components/OnyxListItemProvider.tsx +++ b/src/components/OnyxListItemProvider.tsx @@ -2,6 +2,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import React from 'react'; +import ActivePolicyProvider from './ActivePolicyProvider'; import ComposeProviders from './ComposeProviders'; import createOnyxContext from './createOnyxContext'; @@ -42,6 +43,7 @@ function OnyxListItemProvider(props: OnyxListItemProviderProps) { CardListProvider, WorkspaceCardListProvider, OnboardingValuesProvider, + ActivePolicyProvider, ]} > {props.children} diff --git a/src/hooks/usePolicyForMovingExpenses.ts b/src/hooks/usePolicyForMovingExpenses.ts index 9bd26910b1d3..801bece7e8d6 100644 --- a/src/hooks/usePolicyForMovingExpenses.ts +++ b/src/hooks/usePolicyForMovingExpenses.ts @@ -1,3 +1,4 @@ +import {useActivePolicyContext} from '@components/ActivePolicyProvider'; import {useSession} from '@components/OnyxListItemProvider'; import {canSubmitPerDiemExpenseFromWorkspace, isGroupPolicy, isPolicyMemberWithoutPendingDelete, isTimeTrackingEnabled} from '@libs/PolicyUtils'; @@ -9,8 +10,6 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; -import {activePolicySelector} from '@selectors/Policy'; - import useOnyx from './useOnyx'; // TODO: temporary util - if we don't have employeeList object we don't check for the pending delete @@ -90,10 +89,7 @@ type PolicyForMovingExpenses = { }; function usePolicyForMovingExpenses(isPerDiemRequest?: boolean, isTimeRequest?: boolean, expensePolicyID?: string, isUnreportedManagedCardTransaction?: boolean): PolicyForMovingExpenses { - const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); - const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, { - selector: activePolicySelector, - }); + const {activePolicyID, activePolicy} = useActivePolicyContext(); const session = useSession(); const login = session?.email ?? ''; diff --git a/src/selectors/Policy.ts b/src/selectors/Policy.ts index e2d6483f4f9a..6972201628b4 100644 --- a/src/selectors/Policy.ts +++ b/src/selectors/Policy.ts @@ -30,8 +30,6 @@ type ReusablePolicyConnectionName = | typeof CONST.POLICY.CONNECTIONS.NAME.RILLET | typeof CONST.POLICY.CONNECTIONS.NAME.DUALENTRY; -const activePolicySelector = (policy: OnyxEntry) => (policy?.type !== CONST.POLICY.TYPE.PERSONAL ? policy : undefined); - const ownerPoliciesSelector = (policies: OnyxCollection, currentUserAccountID: number) => getOwnedPaidPolicies(policies, currentUserAccountID); type OwnedPaidPoliciesCounts = { @@ -391,7 +389,6 @@ const createAdminPoliciesSelector = export type {PolicySelector}; export { - activePolicySelector, createAllPolicyReportFieldsSelector, ownerPoliciesSelector, createOwnedPaidPoliciesCountsSelector,