-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportFieldsSettingsPage.tsx
138 lines (129 loc) · 6.74 KB
/
ReportFieldsSettingsPage.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 {StackScreenProps} from '@react-navigation/stack';
import {Str} from 'expensify-common';
import React, {useState} from 'react';
import {View} from 'react-native';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItem from '@components/MenuItem';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as WorkspaceReportFieldUtils from '@libs/WorkspaceReportFieldUtils';
import type {SettingsNavigatorParamList} from '@navigation/types';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading';
import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullscreenLoading';
import * as ReportField from '@userActions/Policy/ReportField';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
type ReportFieldsSettingsPageProps = WithPolicyAndFullscreenLoadingProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.REPORT_FIELDS_SETTINGS>;
function ReportFieldsSettingsPage({
policy,
route: {
params: {policyID, reportFieldID},
},
}: ReportFieldsSettingsPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
const hasAccountingConnections = PolicyUtils.hasAccountingConnections(policy);
const reportFieldKey = ReportUtils.getReportFieldKey(reportFieldID);
const reportField = policy?.fieldList?.[reportFieldKey] ?? null;
if (!reportField) {
return <NotFoundPage />;
}
const isDateFieldType = reportField.type === CONST.REPORT_FIELD_TYPES.DATE;
const isListFieldType = reportField.type === CONST.REPORT_FIELD_TYPES.LIST;
const isListFieldEmpty = isListFieldType && reportField.values.length <= 0;
const listValues = Object.values(policy?.fieldList?.[reportFieldKey]?.values ?? {});
const deleteReportFieldAndHideModal = () => {
ReportField.deleteReportFields(policyID, [reportFieldKey]);
setIsDeleteModalVisible(false);
Navigation.goBack();
};
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={ReportFieldsSettingsPage.displayName}
>
<HeaderWithBackButton
title={reportField.name}
shouldSetModalVisibility={false}
/>
<ConfirmModal
title={translate('workspace.reportFields.delete')}
isVisible={isDeleteModalVisible && !hasAccountingConnections}
onConfirm={deleteReportFieldAndHideModal}
onCancel={() => setIsDeleteModalVisible(false)}
shouldSetModalVisibility={false}
prompt={translate('workspace.reportFields.deleteConfirmation')}
confirmText={translate('common.delete')}
cancelText={translate('common.cancel')}
danger
/>
<View style={styles.flexGrow1}>
<MenuItemWithTopDescription
style={[styles.moneyRequestMenuItem]}
titleStyle={styles.flex1}
title={reportField.name}
description={translate('common.name')}
interactive={false}
/>
<MenuItemWithTopDescription
style={[styles.moneyRequestMenuItem]}
titleStyle={styles.flex1}
title={Str.recapitalize(translate(WorkspaceReportFieldUtils.getReportFieldTypeTranslationKey(reportField.type)))}
description={translate('common.type')}
interactive={false}
/>
{isListFieldType && (
<MenuItemWithTopDescription
style={[styles.moneyRequestMenuItem]}
titleStyle={styles.flex1}
description={translate('workspace.reportFields.listValues')}
shouldShowRightIcon
onPress={() => Navigation.navigate(ROUTES.WORKSPACE_REPORT_FIELDS_LIST_VALUES.getRoute(policyID, reportFieldID))}
title={listValues.join(', ')}
numberOfLinesTitle={5}
/>
)}
{!isListFieldEmpty && (
<MenuItemWithTopDescription
style={[styles.moneyRequestMenuItem]}
titleStyle={styles.flex1}
title={WorkspaceReportFieldUtils.getReportFieldInitialValue(reportField)}
description={translate('common.initialValue')}
shouldShowRightIcon={!isDateFieldType && !hasAccountingConnections}
interactive={!isDateFieldType && !hasAccountingConnections}
onPress={() => Navigation.navigate(ROUTES.WORKSPACE_EDIT_REPORT_FIELDS_INITIAL_VALUE.getRoute(policyID, reportFieldID))}
/>
)}
{!hasAccountingConnections && (
<View style={styles.flexGrow1}>
<MenuItem
icon={Expensicons.Trashcan}
title={translate('common.delete')}
onPress={() => setIsDeleteModalVisible(true)}
/>
</View>
)}
</View>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
ReportFieldsSettingsPage.displayName = 'ReportFieldsSettingsPage';
export default withPolicyAndFullscreenLoading(ReportFieldsSettingsPage);