-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathEditReportFieldPage.tsx
170 lines (147 loc) · 6.56 KB
/
EditReportFieldPage.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
import Str from 'expensify-common/lib/str';
import React, {useState} from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import ConfirmModal from '@components/ConfirmModal';
import type {FormOnyxValues} from '@components/Form/types';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import type {ThreeDotsMenuItem} from '@components/HeaderWithBackButton/types';
import * as Expensicons from '@components/Icon/Expensicons';
import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import * as ReportActions from '@src/libs/actions/Report';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, Report} from '@src/types/onyx';
import EditReportFieldDate from './EditReportFieldDate';
import EditReportFieldDropdown from './EditReportFieldDropdown';
import EditReportFieldText from './EditReportFieldText';
type EditReportFieldPageOnyxProps = {
/** The report object for the expense report */
report: OnyxEntry<Report>;
/** Policy to which the report belongs to */
policy: OnyxEntry<Policy>;
};
type EditReportFieldPageProps = EditReportFieldPageOnyxProps & {
/** Route from navigation */
route: {
/** Params from the route */
params: {
/** Which field we are editing */
fieldID: string;
/** reportID for the expense report */
reportID: string;
/** policyID for the expense report */
policyID: string;
};
};
};
function EditReportFieldPage({route, policy, report}: EditReportFieldPageProps) {
const {windowWidth} = useWindowDimensions();
const styles = useThemeStyles();
const fieldKey = ReportUtils.getReportFieldKey(route.params.fieldID);
const reportField = report?.fieldList?.[fieldKey] ?? policy?.fieldList?.[fieldKey];
const isDisabled = ReportUtils.isReportFieldDisabled(report, reportField ?? null, policy);
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
const {translate} = useLocalize();
if (!reportField || !report || isDisabled) {
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={EditReportFieldPage.displayName}
>
<FullPageNotFoundView
shouldShow
onBackButtonPress={() => {}}
onLinkPress={() => {}}
/>
</ScreenWrapper>
);
}
const isReportFieldTitle = ReportUtils.isReportFieldOfTypeTitle(reportField);
const handleReportFieldChange = (form: FormOnyxValues<typeof ONYXKEYS.FORMS.REPORT_FIELD_EDIT_FORM>) => {
const value = form[fieldKey];
if (isReportFieldTitle) {
ReportActions.updateReportName(report.reportID, value, report.reportName ?? '');
} else {
ReportActions.updateReportField(report.reportID, {...reportField, value: value === '' ? null : value}, reportField);
}
Navigation.dismissModal(report?.reportID);
};
const handleReportFieldDelete = () => {
ReportActions.deleteReportField(report.reportID, reportField);
Navigation.dismissModal(report?.reportID);
};
const fieldValue = isReportFieldTitle ? report.reportName ?? '' : reportField.value ?? reportField.defaultValue;
const menuItems: ThreeDotsMenuItem[] = [];
const isReportFieldDeletable = reportField.deletable && !isReportFieldTitle;
if (isReportFieldDeletable) {
menuItems.push({icon: Expensicons.Trashcan, text: translate('common.delete'), onSelected: () => setIsDeleteModalVisible(true)});
}
const fieldName = Str.UCFirst(reportField.name);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={EditReportFieldPage.displayName}
>
<HeaderWithBackButton
title={fieldName}
threeDotsMenuItems={menuItems}
shouldShowThreeDotsButton={!!menuItems?.length}
threeDotsAnchorPosition={styles.threeDotsPopoverOffsetNoCloseButton(windowWidth)}
/>
<ConfirmModal
title={translate('workspace.reportFields.delete')}
isVisible={isDeleteModalVisible}
onConfirm={handleReportFieldDelete}
onCancel={() => setIsDeleteModalVisible(false)}
prompt={translate('workspace.reportFields.deleteConfirmation')}
confirmText={translate('common.delete')}
cancelText={translate('common.cancel')}
danger
/>
{(reportField.type === 'text' || isReportFieldTitle) && (
<EditReportFieldText
fieldName={Str.UCFirst(reportField.name)}
fieldKey={fieldKey}
fieldValue={fieldValue}
isRequired={!reportField.deletable}
onSubmit={handleReportFieldChange}
/>
)}
{reportField.type === 'date' && (
<EditReportFieldDate
fieldName={Str.UCFirst(reportField.name)}
fieldKey={fieldKey}
fieldValue={fieldValue}
isRequired={!reportField.deletable}
onSubmit={handleReportFieldChange}
/>
)}
{reportField.type === 'dropdown' && (
<EditReportFieldDropdown
policyID={report.policyID ?? ''}
fieldKey={fieldKey}
fieldValue={fieldValue}
fieldOptions={reportField.values.filter((_value: string, index: number) => !reportField.disabledOptions[index])}
onSubmit={handleReportFieldChange}
/>
)}
</ScreenWrapper>
);
}
EditReportFieldPage.displayName = 'EditReportFieldPage';
export default withOnyx<EditReportFieldPageProps, EditReportFieldPageOnyxProps>({
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${route.params.reportID}`,
},
policy: {
key: ({route}) => `${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`,
},
})(EditReportFieldPage);