-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportFieldsListValuesPage.tsx
356 lines (312 loc) · 16.1 KB
/
ReportFieldsListValuesPage.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useMemo, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import ButtonWithDropdownMenu from '@components/ButtonWithDropdownMenu';
import type {DropdownOption} from '@components/ButtonWithDropdownMenu/types';
import ConfirmModal from '@components/ConfirmModal';
import EmptyStateComponent from '@components/EmptyStateComponent';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import * as Illustrations from '@components/Icon/Illustrations';
import ScreenWrapper from '@components/ScreenWrapper';
import ListItemRightCaretWithLabel from '@components/SelectionList/ListItemRightCaretWithLabel';
import TableListItem from '@components/SelectionList/TableListItem';
import type {ListItem} from '@components/SelectionList/types';
import SelectionListWithModal from '@components/SelectionListWithModal';
import TableListItemSkeleton from '@components/Skeletons/TableRowSkeleton';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as ReportField from '@libs/actions/Policy/ReportField';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading';
import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullscreenLoading';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
type ValueListItem = ListItem & {
/** The value */
value: string;
/** Whether the value is enabled */
enabled: boolean;
/** The value order weight in the list */
orderWeight?: number;
};
type ReportFieldsListValuesPageProps = WithPolicyAndFullscreenLoadingProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.REPORT_FIELDS_LIST_VALUES>;
function ReportFieldsListValuesPage({
policy,
route: {
params: {policyID, reportFieldID},
},
}: ReportFieldsListValuesPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {isSmallScreenWidth} = useResponsiveLayout();
const [formDraft] = useOnyx(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT);
const [selectionMode] = useOnyx(ONYXKEYS.MOBILE_SELECTION_MODE);
const [selectedValues, setSelectedValues] = useState<Record<string, boolean>>({});
const [deleteValuesConfirmModalVisible, setDeleteValuesConfirmModalVisible] = useState(false);
const hasAccountingConnections = PolicyUtils.hasAccountingConnections(policy);
const canSelectMultiple = !hasAccountingConnections && (isSmallScreenWidth ? selectionMode?.isEnabled : true);
const [listValues, disabledListValues] = useMemo(() => {
let reportFieldValues: string[];
let reportFieldDisabledValues: boolean[];
if (reportFieldID) {
const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID);
reportFieldValues = Object.values(policy?.fieldList?.[reportFieldKey]?.values ?? {});
reportFieldDisabledValues = Object.values(policy?.fieldList?.[reportFieldKey]?.disabledOptions ?? {});
} else {
reportFieldValues = formDraft?.listValues ?? [];
reportFieldDisabledValues = formDraft?.disabledListValues ?? [];
}
return [reportFieldValues, reportFieldDisabledValues];
}, [formDraft?.disabledListValues, formDraft?.listValues, policy?.fieldList, reportFieldID]);
const listValuesSections = useMemo(() => {
const data = listValues
.map<ValueListItem>((value, index) => ({
value,
index,
text: value,
keyForList: value,
isSelected: selectedValues[value],
enabled: !disabledListValues[index] ?? true,
pendingAction: reportFieldID ? policy?.fieldList?.[ReportUtils.getReportFieldKey(reportFieldID)]?.pendingAction : null,
rightElement: (
<ListItemRightCaretWithLabel
shouldShowCaret={false}
labelText={disabledListValues[index] ? translate('workspace.common.disabled') : translate('workspace.common.enabled')}
/>
),
}))
.sort((a, b) => localeCompare(a.value, b.value));
return [{data, isDisabled: false}];
}, [disabledListValues, listValues, policy?.fieldList, reportFieldID, selectedValues, translate]);
const shouldShowEmptyState = Object.values(listValues ?? {}).length <= 0;
const selectedValuesArray = Object.keys(selectedValues).filter((key) => selectedValues[key]);
const toggleValue = (valueItem: ValueListItem) => {
setSelectedValues((prev) => ({
...prev,
[valueItem.value]: !prev[valueItem.value],
}));
};
const toggleAllValues = () => {
const isAllSelected = listValues.length === Object.keys(selectedValues).length;
setSelectedValues(isAllSelected ? {} : Object.fromEntries(listValues.map((value) => [value, true])));
};
const handleDeleteValues = () => {
setSelectedValues({});
const valuesToDelete = selectedValuesArray.reduce<number[]>((acc, valueName) => {
const index = listValues?.indexOf(valueName) ?? -1;
if (index !== -1) {
acc.push(index);
}
return acc;
}, []);
if (reportFieldID) {
ReportField.removeReportFieldListValue(policyID, reportFieldID, valuesToDelete);
} else {
ReportField.deleteReportFieldsListValue(valuesToDelete);
}
setDeleteValuesConfirmModalVisible(false);
};
const openListValuePage = (valueItem: ValueListItem) => {
if (valueItem.index === undefined || hasAccountingConnections) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_REPORT_FIELDS_VALUE_SETTINGS.getRoute(policyID, valueItem.index, reportFieldID));
setSelectedValues({});
};
const getCustomListHeader = () => {
const header = (
<View
style={[
styles.flex1,
styles.flexRow,
styles.justifyContentBetween,
// Required padding accounting for the checkbox and the right arrow in multi-select mode
canSelectMultiple && styles.pl3,
]}
>
<Text style={styles.searchInputStyle}>{translate('common.name')}</Text>
<Text style={[styles.searchInputStyle, styles.textAlignCenter]}>{translate('statusPage.status')}</Text>
</View>
);
if (canSelectMultiple) {
return header;
}
return <View style={[styles.flexRow, styles.ph9, styles.pv3, styles.pb5]}>{header}</View>;
};
const getHeaderButtons = () => {
const options: Array<DropdownOption<DeepValueOf<typeof CONST.POLICY.BULK_ACTION_TYPES>>> = [];
if ((isSmallScreenWidth && selectionMode?.isEnabled) ?? selectedValuesArray.length > 0) {
if (selectedValuesArray.length > 0) {
options.push({
icon: Expensicons.Trashcan,
text: translate(selectedValuesArray.length === 1 ? 'workspace.reportFields.deleteValue' : 'workspace.reportFields.deleteValues'),
value: CONST.POLICY.BULK_ACTION_TYPES.DELETE,
onSelected: () => setDeleteValuesConfirmModalVisible(true),
});
}
const enabledValues = selectedValuesArray.filter((valueName) => {
const index = listValues?.indexOf(valueName) ?? -1;
return !disabledListValues?.[index];
});
if (enabledValues.length > 0) {
const valuesToDisable = selectedValuesArray.reduce<number[]>((acc, valueName) => {
const index = listValues?.indexOf(valueName) ?? -1;
if (!disabledListValues?.[index] && index !== -1) {
acc.push(index);
}
return acc;
}, []);
options.push({
icon: Expensicons.DocumentSlash,
text: translate(enabledValues.length === 1 ? 'workspace.reportFields.disableValue' : 'workspace.reportFields.disableValues'),
value: CONST.POLICY.BULK_ACTION_TYPES.DISABLE,
onSelected: () => {
setSelectedValues({});
if (reportFieldID) {
ReportField.updateReportFieldListValueEnabled(policyID, reportFieldID, valuesToDisable, false);
return;
}
ReportField.setReportFieldsListValueEnabled(valuesToDisable, false);
},
});
}
const disabledValues = selectedValuesArray.filter((valueName) => {
const index = listValues?.indexOf(valueName) ?? -1;
return disabledListValues?.[index];
});
if (disabledValues.length > 0) {
const valuesToEnable = selectedValuesArray.reduce<number[]>((acc, valueName) => {
const index = listValues?.indexOf(valueName) ?? -1;
if (disabledListValues?.[index] && index !== -1) {
acc.push(index);
}
return acc;
}, []);
options.push({
icon: Expensicons.Document,
text: translate(disabledValues.length === 1 ? 'workspace.reportFields.enableValue' : 'workspace.reportFields.enableValues'),
value: CONST.POLICY.BULK_ACTION_TYPES.ENABLE,
onSelected: () => {
setSelectedValues({});
if (reportFieldID) {
ReportField.updateReportFieldListValueEnabled(policyID, reportFieldID, valuesToEnable, true);
return;
}
ReportField.setReportFieldsListValueEnabled(valuesToEnable, true);
},
});
}
return (
<ButtonWithDropdownMenu
onPress={() => null}
shouldAlwaysShowDropdownMenu
pressOnEnter
buttonSize={CONST.DROPDOWN_BUTTON_SIZE.MEDIUM}
customText={translate('workspace.common.selected', {selectedNumber: selectedValuesArray.length})}
options={options}
isSplitButton={false}
style={[isSmallScreenWidth && styles.flexGrow1, isSmallScreenWidth && styles.mb3]}
isDisabled={!selectedValuesArray.length}
/>
);
}
return (
<Button
style={[isSmallScreenWidth && styles.flexGrow1, isSmallScreenWidth && styles.mb3]}
medium
success
icon={Expensicons.Plus}
text={translate('workspace.reportFields.addValue')}
onPress={() => Navigation.navigate(ROUTES.WORKSPACE_REPORT_FIELDS_ADD_VALUE.getRoute(policyID, reportFieldID))}
/>
);
};
const selectionModeHeader = selectionMode?.isEnabled && isSmallScreenWidth;
return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_REPORT_FIELDS_ENABLED}
>
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
style={styles.defaultModalContainer}
testID={ReportFieldsListValuesPage.displayName}
shouldEnableMaxHeight
>
<HeaderWithBackButton
title={translate(selectionModeHeader ? 'common.selectMultiple' : 'workspace.reportFields.listValues')}
onBackButtonPress={() => {
if (selectionMode?.isEnabled) {
setSelectedValues({});
turnOffMobileSelectionMode();
return;
}
Navigation.goBack();
}}
>
{!isSmallScreenWidth && !hasAccountingConnections && getHeaderButtons()}
</HeaderWithBackButton>
{isSmallScreenWidth && <View style={[styles.pl5, styles.pr5]}>{!hasAccountingConnections && getHeaderButtons()}</View>}
<View style={[styles.ph5, styles.pv4]}>
<Text style={[styles.sidebarLinkText, styles.optionAlternateText]}>{translate('workspace.reportFields.listInputSubtitle')}</Text>
</View>
{shouldShowEmptyState && (
<EmptyStateComponent
title={translate('workspace.reportFields.emptyReportFieldsValues.title')}
subtitle={translate('workspace.reportFields.emptyReportFieldsValues.subtitle')}
SkeletonComponent={TableListItemSkeleton}
headerMediaType={CONST.EMPTY_STATE_MEDIA.ILLUSTRATION}
headerMedia={Illustrations.EmptyStateExpenses}
headerStyles={styles.emptyFolderBG}
headerContentStyles={styles.emptyStateFolderIconSize}
/>
)}
{!shouldShowEmptyState && (
<SelectionListWithModal
canSelectMultiple={canSelectMultiple}
turnOnSelectionModeOnLongPress
onTurnOnSelectionMode={(item) => item && toggleValue(item)}
sections={listValuesSections}
onCheckboxPress={toggleValue}
onSelectRow={openListValuePage}
shouldDebounceRowSelect={false}
onSelectAll={toggleAllValues}
ListItem={TableListItem}
customListHeader={getCustomListHeader()}
shouldPreventDefaultFocusOnSelectRow={!DeviceCapabilities.canUseTouchScreen()}
listHeaderWrapperStyle={[styles.ph9, styles.pv3, styles.pb5]}
showScrollIndicator={false}
/>
)}
<ConfirmModal
isVisible={deleteValuesConfirmModalVisible}
onConfirm={handleDeleteValues}
onCancel={() => setDeleteValuesConfirmModalVisible(false)}
title={translate(selectedValuesArray.length === 1 ? 'workspace.reportFields.deleteValue' : 'workspace.reportFields.deleteValues')}
prompt={translate(selectedValuesArray.length === 1 ? 'workspace.reportFields.deleteValuePrompt' : 'workspace.reportFields.deleteValuesPrompt')}
confirmText={translate('common.delete')}
cancelText={translate('common.cancel')}
danger
/>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
ReportFieldsListValuesPage.displayName = 'ReportFieldsListValuesPage';
export default withPolicyAndFullscreenLoading(ReportFieldsListValuesPage);