-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
AccountingContext.tsx
138 lines (120 loc) · 5.27 KB
/
AccountingContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import type {MutableRefObject, RefObject} from 'react';
import React, {useContext, useMemo, useRef, useState} from 'react';
import type {View} from 'react-native';
import AccountingConnectionConfirmationModal from '@components/AccountingConnectionConfirmationModal';
import useLocalize from '@hooks/useLocalize';
import {removePolicyConnection} from '@libs/actions/connections';
import Navigation from '@libs/Navigation/Navigation';
import {isControlPolicy} from '@libs/PolicyUtils';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type {ConnectionName} from '@src/types/onyx/Policy';
import type Policy from '@src/types/onyx/Policy';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import {getAccountingIntegrationData} from './utils';
type ActiveIntegration = {
name: ConnectionName;
shouldDisconnectIntegrationBeforeConnecting?: boolean;
integrationToDisconnect?: ConnectionName;
};
type ActiveIntegrationState = ActiveIntegration & {key: number};
type AccountingContextType = {
activeIntegration?: ActiveIntegration;
startIntegrationFlow: (activeIntegration: ActiveIntegration) => void;
/*
* This stores refs to integration buttons, so the PopoverMenu can be positioned correctly
*/
popoverAnchorRefs: RefObject<Record<string, MutableRefObject<View | null>>>;
};
const popoverAnchorRefsInitialValue = Object.values(CONST.POLICY.CONNECTIONS.NAME).reduce((acc, key) => {
acc[key] = {current: null};
return acc;
}, {} as Record<ConnectionName, MutableRefObject<View | null>>);
const defaultAccountingContext = {
activeIntegration: undefined,
startIntegrationFlow: () => {},
popoverAnchorRefs: {
current: popoverAnchorRefsInitialValue,
},
};
const AccountingContext = React.createContext<AccountingContextType>(defaultAccountingContext);
type AccountingContextProviderProps = ChildrenProps & {
policy: Policy;
};
function AccountingContextProvider({children, policy}: AccountingContextProviderProps) {
const popoverAnchorRefs = useRef<Record<string, MutableRefObject<View | null>>>(defaultAccountingContext.popoverAnchorRefs.current);
const [activeIntegration, setActiveIntegration] = useState<ActiveIntegrationState>();
const {translate} = useLocalize();
const policyID = policy.id;
const startIntegrationFlow = React.useCallback(
(newActiveIntegration: ActiveIntegration) => {
const accountingIntegrationData = getAccountingIntegrationData(newActiveIntegration.name, policyID, translate);
const workspaceUpgradeNavigationDetails = accountingIntegrationData?.workspaceUpgradeNavigationDetails;
if (workspaceUpgradeNavigationDetails && !isControlPolicy(policy)) {
Navigation.navigate(
ROUTES.WORKSPACE_UPGRADE.getRoute(policyID, workspaceUpgradeNavigationDetails.integrationAlias, workspaceUpgradeNavigationDetails.backToAfterWorkspaceUpgradeRoute),
);
return;
}
setActiveIntegration({
...newActiveIntegration,
key: Math.random(),
});
},
[policy, policyID, translate],
);
const closeConfirmationModal = () => {
setActiveIntegration((prev) => {
if (prev) {
return {
...prev,
shouldDisconnectIntegrationBeforeConnecting: false,
integrationToDisconnect: undefined,
};
}
return undefined;
});
};
const accountingContext = useMemo(
() => ({
activeIntegration,
startIntegrationFlow,
popoverAnchorRefs,
}),
[activeIntegration, startIntegrationFlow],
);
const renderActiveIntegration = () => {
if (!activeIntegration) {
return null;
}
return getAccountingIntegrationData(activeIntegration.name, policyID, translate, policy, activeIntegration.key)?.setupConnectionFlow;
};
const shouldShowConfirmationModal = activeIntegration?.shouldDisconnectIntegrationBeforeConnecting && activeIntegration?.integrationToDisconnect;
return (
<AccountingContext.Provider value={accountingContext}>
{children}
{!shouldShowConfirmationModal && renderActiveIntegration()}
{shouldShowConfirmationModal && activeIntegration?.integrationToDisconnect && (
<AccountingConnectionConfirmationModal
onConfirm={() => {
if (!activeIntegration?.integrationToDisconnect) {
return;
}
removePolicyConnection(policyID, activeIntegration?.integrationToDisconnect);
closeConfirmationModal();
}}
integrationToConnect={activeIntegration?.integrationToDisconnect}
onCancel={() => {
setActiveIntegration(undefined);
}}
/>
)}
</AccountingContext.Provider>
);
}
function useAccountingContext() {
return useContext(AccountingContext);
}
export default AccountingContext;
export {AccountingContextProvider, useAccountingContext};
export type {ActiveIntegrationState};