Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/components/ActivePolicyProvider.tsx
Original file line number Diff line number Diff line change
@@ -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<Policy>;
};

const ActivePolicyContext = createContext<ActivePolicyContextValue>({activePolicyID: undefined, activePolicy: undefined});

const activePolicySelector = (policy: OnyxEntry<Policy>) => (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 <ActivePolicyContext.Provider value={{activePolicyID, activePolicy}}>{children}</ActivePolicyContext.Provider>;
}

function useActivePolicyContext() {
return useContext(ActivePolicyContext);
}

export default ActivePolicyProvider;
export {useActivePolicyContext};
2 changes: 2 additions & 0 deletions src/components/OnyxListItemProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -42,6 +43,7 @@ function OnyxListItemProvider(props: OnyxListItemProviderProps) {
CardListProvider,
WorkspaceCardListProvider,
OnboardingValuesProvider,
ActivePolicyProvider,
]}
>
{props.children}
Expand Down
8 changes: 2 additions & 6 deletions src/hooks/usePolicyForMovingExpenses.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {useActivePolicyContext} from '@components/ActivePolicyProvider';
import {useSession} from '@components/OnyxListItemProvider';

import {canSubmitPerDiemExpenseFromWorkspace, isGroupPolicy, isPolicyMemberWithoutPendingDelete, isTimeTrackingEnabled} from '@libs/PolicyUtils';
Expand All @@ -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
Expand Down Expand Up @@ -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 ?? '';
Expand Down
3 changes: 0 additions & 3 deletions src/selectors/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ type ReusablePolicyConnectionName =
| typeof CONST.POLICY.CONNECTIONS.NAME.RILLET
| typeof CONST.POLICY.CONNECTIONS.NAME.DUALENTRY;

const activePolicySelector = (policy: OnyxEntry<Policy>) => (policy?.type !== CONST.POLICY.TYPE.PERSONAL ? policy : undefined);

const ownerPoliciesSelector = (policies: OnyxCollection<Policy>, currentUserAccountID: number) => getOwnedPaidPolicies(policies, currentUserAccountID);

type OwnedPaidPoliciesCounts = {
Expand Down Expand Up @@ -391,7 +389,6 @@ const createAdminPoliciesSelector =

export type {PolicySelector};
export {
activePolicySelector,
createAllPolicyReportFieldsSelector,
ownerPoliciesSelector,
createOwnedPaidPoliciesCountsSelector,
Expand Down
Loading