-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathExpenseReportRulesSection.tsx
224 lines (216 loc) · 12.8 KB
/
ExpenseReportRulesSection.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import React from 'react';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import Section from '@components/Section';
import Text from '@components/Text';
import TextLink from '@components/TextLink';
import useLocalize from '@hooks/useLocalize';
import usePolicy from '@hooks/usePolicy';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import ToggleSettingOptionRow from '@pages/workspace/workflows/ToggleSettingsOptionRow';
import * as PolicyActions from '@userActions/Policy/Policy';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
type ExpenseReportRulesSectionProps = {
policyID: string;
};
function ExpenseReportRulesSection({policyID}: ExpenseReportRulesSectionProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();
const policy = usePolicy(policyID);
const customReportNamesUnavailable = !policy?.areReportFieldsEnabled;
// Auto-approvals and self-approvals are unavailable due to the policy workflows settings
const workflowApprovalsUnavailable = PolicyUtils.getWorkflowApprovalsUnavailable(policy);
const autoPayApprovedReportsUnavailable = policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO;
const renderFallbackSubtitle = ({featureName, variant = 'unlock'}: {featureName: string; variant?: 'unlock' | 'enable'}) => {
return (
<Text style={[styles.flexRow, styles.alignItemsCenter, styles.w100, styles.mt2]}>
<Text style={[styles.textNormal, styles.colorMuted]}>{translate('workspace.rules.expenseReportRules.unlockFeatureGoToSubtitle')}</Text>{' '}
<TextLink
style={styles.link}
onPress={() => Navigation.navigate(ROUTES.WORKSPACE_MORE_FEATURES.getRoute(policyID))}
>
{translate('workspace.common.moreFeatures').toLowerCase()}
</TextLink>{' '}
{variant === 'unlock' ? (
<Text style={[styles.textNormal, styles.colorMuted]}>{translate('workspace.rules.expenseReportRules.unlockFeatureEnableWorkflowsSubtitle', featureName)}</Text>
) : (
<Text style={[styles.textNormal, styles.colorMuted]}>{translate('workspace.rules.expenseReportRules.enableFeatureSubtitle', featureName)}</Text>
)}
</Text>
);
};
const optionItems = [
{
title: translate('workspace.rules.expenseReportRules.customReportNamesTitle'),
subtitle: customReportNamesUnavailable
? renderFallbackSubtitle({featureName: translate('workspace.common.reportFields').toLowerCase(), variant: 'enable'})
: translate('workspace.rules.expenseReportRules.customReportNamesSubtitle'),
switchAccessibilityLabel: translate('workspace.rules.expenseReportRules.customReportNamesTitle'),
isActive: policy?.shouldShowCustomReportTitleOption,
disabled: customReportNamesUnavailable,
showLockIcon: customReportNamesUnavailable,
pendingAction: policy?.pendingFields?.shouldShowCustomReportTitleOption,
onToggle: (isEnabled: boolean) => PolicyActions.enablePolicyDefaultReportTitle(policyID, isEnabled),
subMenuItems: [
<OfflineWithFeedback
pendingAction={
!policy?.pendingFields?.shouldShowCustomReportTitleOption && policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].pendingFields?.defaultValue
? policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].pendingFields?.defaultValue
: null
}
key="customName"
>
<MenuItemWithTopDescription
description={translate('workspace.rules.expenseReportRules.customNameTitle')}
title={policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].defaultValue}
shouldShowRightIcon
style={[styles.sectionMenuItemTopDescription, styles.mt6, styles.mbn3]}
onPress={() => Navigation.navigate(ROUTES.RULES_CUSTOM_NAME.getRoute(policyID))}
/>
</OfflineWithFeedback>,
<ToggleSettingOptionRow
pendingAction={
!policy?.pendingFields?.shouldShowCustomReportTitleOption && policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].pendingFields?.deletable
? policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].pendingFields?.deletable
: null
}
key="preventMembersFromChangingCustomNames"
title={translate('workspace.rules.expenseReportRules.preventMembersFromChangingCustomNamesTitle')}
switchAccessibilityLabel={translate('workspace.rules.expenseReportRules.preventMembersFromChangingCustomNamesTitle')}
wrapperStyle={[styles.sectionMenuItemTopDescription, styles.mt6]}
titleStyle={styles.pv2}
isActive={!policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE].deletable}
onToggle={(isEnabled) => PolicyActions.setPolicyPreventMemberCreatedTitle(policyID, isEnabled)}
/>,
],
},
{
title: translate('workspace.rules.expenseReportRules.preventSelfApprovalsTitle'),
subtitle: workflowApprovalsUnavailable
? renderFallbackSubtitle({featureName: translate('common.approvals').toLowerCase()})
: translate('workspace.rules.expenseReportRules.preventSelfApprovalsSubtitle'),
switchAccessibilityLabel: translate('workspace.rules.expenseReportRules.preventSelfApprovalsTitle'),
isActive: policy?.preventSelfApproval && !workflowApprovalsUnavailable,
disabled: workflowApprovalsUnavailable,
showLockIcon: workflowApprovalsUnavailable,
pendingAction: policy?.pendingFields?.preventSelfApproval,
onToggle: (isEnabled: boolean) => PolicyActions.setPolicyPreventSelfApproval(policyID, isEnabled),
},
{
title: translate('workspace.rules.expenseReportRules.autoApproveCompliantReportsTitle'),
subtitle: workflowApprovalsUnavailable
? renderFallbackSubtitle({featureName: translate('common.approvals').toLowerCase()})
: translate('workspace.rules.expenseReportRules.autoApproveCompliantReportsSubtitle'),
switchAccessibilityLabel: translate('workspace.rules.expenseReportRules.autoApproveCompliantReportsTitle'),
isActive: policy?.shouldShowAutoApprovalOptions && !workflowApprovalsUnavailable,
disabled: workflowApprovalsUnavailable,
showLockIcon: workflowApprovalsUnavailable,
pendingAction: policy?.pendingFields?.shouldShowAutoApprovalOptions,
onToggle: (isEnabled: boolean) => {
PolicyActions.enableAutoApprovalOptions(policyID, isEnabled);
},
subMenuItems: [
<OfflineWithFeedback
pendingAction={!policy?.pendingFields?.shouldShowAutoApprovalOptions && policy?.autoApproval?.pendingFields?.limit ? policy?.autoApproval?.pendingFields?.limit : null}
key="autoApproveReportsUnder"
>
<MenuItemWithTopDescription
description={translate('workspace.rules.expenseReportRules.autoApproveReportsUnderTitle')}
title={CurrencyUtils.convertToDisplayString(
policy?.autoApproval?.limit ?? CONST.POLICY.AUTO_APPROVE_REPORTS_UNDER_DEFAULT_CENTS,
policy?.outputCurrency ?? CONST.CURRENCY.USD,
)}
shouldShowRightIcon
style={[styles.sectionMenuItemTopDescription, styles.mt6, styles.mbn3]}
onPress={() => Navigation.navigate(ROUTES.RULES_AUTO_APPROVE_REPORTS_UNDER.getRoute(policyID))}
/>
</OfflineWithFeedback>,
<OfflineWithFeedback
pendingAction={
!policy?.pendingFields?.shouldShowAutoApprovalOptions && policy?.autoApproval?.pendingFields?.auditRate ? policy?.autoApproval?.pendingFields?.auditRate : null
}
key="randomReportAuditTitle"
>
<MenuItemWithTopDescription
description={translate('workspace.rules.expenseReportRules.randomReportAuditTitle')}
title={`${policy?.autoApproval?.auditRate ?? CONST.POLICY.RANDOM_AUDIT_DEFAULT_PERCENTAGE}%`}
shouldShowRightIcon
style={[styles.sectionMenuItemTopDescription, styles.mt6, styles.mbn3]}
onPress={() => Navigation.navigate(ROUTES.RULES_RANDOM_REPORT_AUDIT.getRoute(policyID))}
/>
</OfflineWithFeedback>,
],
},
{
title: translate('workspace.rules.expenseReportRules.autoPayApprovedReportsTitle'),
subtitle: autoPayApprovedReportsUnavailable
? renderFallbackSubtitle({featureName: translate('common.payments').toLowerCase()})
: translate('workspace.rules.expenseReportRules.autoPayApprovedReportsSubtitle'),
switchAccessibilityLabel: translate('workspace.rules.expenseReportRules.autoPayApprovedReportsTitle'),
onToggle: (isEnabled: boolean) => {
PolicyActions.enablePolicyAutoReimbursementLimit(policyID, isEnabled);
},
disabled: autoPayApprovedReportsUnavailable,
showLockIcon: autoPayApprovedReportsUnavailable,
isActive: policy?.shouldShowAutoReimbursementLimitOption && !autoPayApprovedReportsUnavailable,
pendingAction: policy?.pendingFields?.shouldShowAutoReimbursementLimitOption,
subMenuItems: [
<OfflineWithFeedback
pendingAction={
!policy?.pendingFields?.shouldShowAutoReimbursementLimitOption && policy?.autoReimbursement?.pendingFields?.limit
? policy?.autoReimbursement?.pendingFields?.limit
: null
}
key="autoPayReportsUnder"
>
<MenuItemWithTopDescription
description={translate('workspace.rules.expenseReportRules.autoPayReportsUnderTitle')}
title={CurrencyUtils.convertToDisplayString(
policy?.autoReimbursement?.limit ?? CONST.POLICY.AUTO_REIMBURSEMENT_DEFAULT_LIMIT_CENTS,
policy?.outputCurrency ?? CONST.CURRENCY.USD,
)}
shouldShowRightIcon
style={[styles.sectionMenuItemTopDescription, styles.mt6, styles.mbn3]}
onPress={() => Navigation.navigate(ROUTES.RULES_AUTO_PAY_REPORTS_UNDER.getRoute(policyID))}
/>
</OfflineWithFeedback>,
],
},
];
return (
<Section
isCentralPane
title={translate('workspace.rules.expenseReportRules.title')}
subtitle={translate('workspace.rules.expenseReportRules.subtitle')}
titleStyles={styles.accountSettingsSectionTitle}
subtitleMuted
>
{optionItems.map(({title, subtitle, isActive, subMenuItems, showLockIcon, disabled, onToggle, pendingAction}, index) => {
const showBorderBottom = index !== optionItems.length - 1;
return (
<ToggleSettingOptionRow
key={title}
title={title}
subtitle={subtitle}
switchAccessibilityLabel={title}
wrapperStyle={[styles.pv6, showBorderBottom && styles.borderBottom]}
shouldPlaceSubtitleBelowSwitch
titleStyle={styles.pv2}
subtitleStyle={styles.pt1}
isActive={!!isActive}
showLockIcon={showLockIcon}
disabled={disabled}
subMenuItems={subMenuItems}
onToggle={onToggle}
pendingAction={pendingAction}
/>
);
})}
</Section>
);
}
export default ExpenseReportRulesSection;