forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSageIntacctAdvancedPage.tsx
148 lines (139 loc) · 7.25 KB
/
SageIntacctAdvancedPage.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
139
140
141
142
143
144
145
146
147
148
import React, {useMemo} from 'react';
import ConnectionLayout from '@components/ConnectionLayout';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ErrorUtils from '@libs/ErrorUtils';
import {getCurrentSageIntacctEntityName} from '@libs/PolicyUtils';
import Navigation from '@navigation/Navigation';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicy from '@pages/workspace/withPolicy';
import ToggleSettingOptionRow from '@pages/workspace/workflows/ToggleSettingsOptionRow';
import {
updateSageIntacctApprovalMode,
updateSageIntacctAutoSync,
updateSageIntacctImportEmployees,
updateSageIntacctSyncReimbursedReports,
updateSageIntacctSyncReimbursementAccountID,
} from '@userActions/connections/SageIntacct';
import * as Policy from '@userActions/Policy/Policy';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type {SageIntacctDataElement} from '@src/types/onyx/Policy';
function getReimbursedAccountName(bankAccounts: SageIntacctDataElement[], reimbursementAccountID?: string): string | undefined {
return bankAccounts.find((bankAccount) => bankAccount.id === reimbursementAccountID)?.name ?? reimbursementAccountID;
}
function SageIntacctAdvancedPage({policy}: WithPolicyProps) {
const {translate} = useLocalize();
const policyID = policy?.id ?? '-1';
const styles = useThemeStyles();
const {importEmployees, autoSync, sync, pendingFields, errorFields} = policy?.connections?.intacct?.config ?? {};
const {data, config} = policy?.connections?.intacct ?? {};
const toggleSections = useMemo(
() => [
{
label: translate('workspace.sageIntacct.autoSync'),
description: translate('workspace.sageIntacct.autoSyncDescription'),
isActive: !!autoSync?.enabled,
onToggle: (enabled: boolean) => updateSageIntacctAutoSync(policyID, enabled),
pendingAction: pendingFields?.enabled,
error: ErrorUtils.getLatestErrorField(config, CONST.SAGE_INTACCT_CONFIG.AUTO_SYNC_ENABLED),
onCloseError: () => Policy.clearSageIntacctErrorField(policyID, CONST.SAGE_INTACCT_CONFIG.AUTO_SYNC_ENABLED),
},
{
label: translate('workspace.sageIntacct.inviteEmployees'),
description: translate('workspace.sageIntacct.inviteEmployeesDescription'),
isActive: !!importEmployees,
onToggle: (enabled: boolean) => {
updateSageIntacctImportEmployees(policyID, enabled);
updateSageIntacctApprovalMode(policyID, enabled);
},
pendingAction: pendingFields?.importEmployees,
error:
ErrorUtils.getLatestErrorField(config ?? {}, CONST.SAGE_INTACCT_CONFIG.IMPORT_EMPLOYEES) ??
ErrorUtils.getLatestErrorField(config ?? {}, CONST.SAGE_INTACCT_CONFIG.APPROVAL_MODE),
onCloseError: () => {
Policy.clearSageIntacctErrorField(policyID, CONST.SAGE_INTACCT_CONFIG.IMPORT_EMPLOYEES);
Policy.clearSageIntacctErrorField(policyID, CONST.SAGE_INTACCT_CONFIG.APPROVAL_MODE);
},
},
{
label: translate('workspace.sageIntacct.syncReimbursedReports'),
description: translate('workspace.sageIntacct.syncReimbursedReportsDescription'),
isActive: !!sync?.syncReimbursedReports,
onToggle: (enabled: boolean) => {
updateSageIntacctSyncReimbursedReports(policyID, enabled);
if (enabled && !sync?.reimbursementAccountID) {
const reimbursementAccountID = data?.bankAccounts[0]?.id ?? '';
updateSageIntacctSyncReimbursementAccountID(policyID, reimbursementAccountID);
}
},
pendingAction: pendingFields?.syncReimbursedReports,
error: ErrorUtils.getLatestErrorField(config ?? {}, CONST.SAGE_INTACCT_CONFIG.SYNC_REIMBURSED_REPORTS),
onCloseError: () => {
Policy.clearSageIntacctErrorField(policyID, CONST.SAGE_INTACCT_CONFIG.SYNC_REIMBURSED_REPORTS);
},
},
],
[
translate,
autoSync?.enabled,
pendingFields?.enabled,
pendingFields?.importEmployees,
pendingFields?.syncReimbursedReports,
config,
importEmployees,
sync?.syncReimbursedReports,
sync?.reimbursementAccountID,
policyID,
data?.bankAccounts,
],
);
return (
<ConnectionLayout
displayName={SageIntacctAdvancedPage.displayName}
headerTitle="workspace.accounting.advanced"
headerSubtitle={getCurrentSageIntacctEntityName(policy)}
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED}
contentContainerStyle={styles.pb2}
titleStyle={styles.ph5}
connectionName={CONST.POLICY.CONNECTIONS.NAME.SAGE_INTACCT}
onBackButtonPress={() => Navigation.goBack(ROUTES.POLICY_ACCOUNTING.getRoute(policyID))}
>
{toggleSections.map((section) => (
<ToggleSettingOptionRow
key={section.label}
title={section.label}
subtitle={section.description}
shouldPlaceSubtitleBelowSwitch
switchAccessibilityLabel={section.label}
isActive={section.isActive}
onToggle={section.onToggle}
wrapperStyle={[styles.ph5, styles.pv3]}
pendingAction={section.pendingAction}
errors={section.error}
onCloseError={section.onCloseError}
/>
))}
{!!sync?.syncReimbursedReports && (
<OfflineWithFeedback
key={translate('workspace.sageIntacct.paymentAccount')}
pendingAction={pendingFields?.reimbursementAccountID}
>
<MenuItemWithTopDescription
title={getReimbursedAccountName(data?.bankAccounts ?? [], sync?.reimbursementAccountID) ?? translate('workspace.sageIntacct.notConfigured')}
description={translate('workspace.sageIntacct.paymentAccount')}
shouldShowRightIcon
onPress={() => Navigation.navigate(ROUTES.POLICY_ACCOUNTING_SAGE_INTACCT_PAYMENT_ACCOUNT.getRoute(policyID))}
brickRoadIndicator={errorFields?.reimbursementAccountID ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
/>
</OfflineWithFeedback>
)}
</ConnectionLayout>
);
}
SageIntacctAdvancedPage.displayName = 'SageIntacctAdvancedPage';
export default withPolicy(SageIntacctAdvancedPage);