forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditRequestPage.js
341 lines (306 loc) · 13.4 KB
/
EditRequestPage.js
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
import React, {useEffect, useMemo} from 'react';
import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import lodashValues from 'lodash/values';
import {withOnyx} from 'react-native-onyx';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';
import ROUTES from '../ROUTES';
import compose from '../libs/compose';
import Navigation from '../libs/Navigation/Navigation';
import * as ReportActionsUtils from '../libs/ReportActionsUtils';
import * as ReportUtils from '../libs/ReportUtils';
import * as PolicyUtils from '../libs/PolicyUtils';
import * as TransactionUtils from '../libs/TransactionUtils';
import * as Policy from '../libs/actions/Policy';
import * as IOU from '../libs/actions/IOU';
import * as CurrencyUtils from '../libs/CurrencyUtils';
import * as OptionsListUtils from '../libs/OptionsListUtils';
import Permissions from '../libs/Permissions';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes} from '../components/withCurrentUserPersonalDetails';
import tagPropTypes from '../components/tagPropTypes';
import FullPageNotFoundView from '../components/BlockingViews/FullPageNotFoundView';
import EditRequestDescriptionPage from './EditRequestDescriptionPage';
import EditRequestMerchantPage from './EditRequestMerchantPage';
import EditRequestCreatedPage from './EditRequestCreatedPage';
import EditRequestAmountPage from './EditRequestAmountPage';
import EditRequestReceiptPage from './EditRequestReceiptPage';
import reportPropTypes from './reportPropTypes';
import EditRequestDistancePage from './EditRequestDistancePage';
import EditRequestCategoryPage from './EditRequestCategoryPage';
import EditRequestTagPage from './EditRequestTagPage';
import categoryPropTypes from '../components/categoryPropTypes';
import ScreenWrapper from '../components/ScreenWrapper';
import transactionPropTypes from '../components/transactionPropTypes';
const propTypes = {
/** Route from navigation */
route: PropTypes.shape({
/** Params from the route */
params: PropTypes.shape({
/** Which field we are editing */
field: PropTypes.string,
/** reportID for the "transaction thread" */
threadReportID: PropTypes.string,
}),
}).isRequired,
/** Onyx props */
/** List of betas available to current user */
betas: PropTypes.arrayOf(PropTypes.string),
/** The report object for the thread report */
report: reportPropTypes,
/** The parent report object for the thread report */
parentReport: reportPropTypes,
/** The policy object for the current route */
policy: PropTypes.shape({
/** The name of the policy */
name: PropTypes.string,
/** The URL for the policy avatar */
avatar: PropTypes.string,
}),
/** Session info for the currently logged in user. */
session: PropTypes.shape({
/** Currently logged in user email */
email: PropTypes.string,
}),
/** Collection of categories attached to a policy */
policyCategories: PropTypes.objectOf(categoryPropTypes),
/** Collection of tags attached to a policy */
policyTags: tagPropTypes,
/** The original transaction that is being edited */
transaction: transactionPropTypes,
...withCurrentUserPersonalDetailsPropTypes,
};
const defaultProps = {
betas: [],
report: {},
parentReport: {},
policy: null,
session: {
email: null,
},
policyCategories: {},
policyTags: {},
transaction: {},
};
function EditRequestPage({betas, report, route, parentReport, policy, session, policyCategories, policyTags, parentReportActions, transaction}) {
const parentReportActionID = lodashGet(report, 'parentReportActionID', '0');
const parentReportAction = lodashGet(parentReportActions, parentReportActionID);
const {
amount: transactionAmount,
currency: transactionCurrency,
comment: transactionDescription,
merchant: transactionMerchant,
category: transactionCategory,
tag: transactionTag,
} = ReportUtils.getTransactionDetails(transaction);
const defaultCurrency = lodashGet(route, 'params.currency', '') || transactionCurrency;
// Take only the YYYY-MM-DD value
const transactionCreated = TransactionUtils.getCreated(transaction);
const fieldToEdit = lodashGet(route, ['params', 'field'], '');
const isDeleted = ReportActionsUtils.isDeletedAction(parentReportAction);
const isSettled = ReportUtils.isSettled(parentReport.reportID);
const isAdmin = Policy.isAdminOfFreePolicy([policy]) && ReportUtils.isExpenseReport(parentReport);
const isRequestor = ReportUtils.isMoneyRequestReport(parentReport) && lodashGet(session, 'accountID', null) === parentReportAction.actorAccountID;
const canEdit = !isSettled && !isDeleted && (isAdmin || isRequestor);
// For now, it always defaults to the first tag of the policy
const policyTag = PolicyUtils.getTag(policyTags);
const policyTagList = lodashGet(policyTag, 'tags', {});
const tagListName = PolicyUtils.getTagListName(policyTags);
// A flag for verifying that the current report is a sub-report of a workspace chat
const isPolicyExpenseChat = useMemo(() => ReportUtils.isPolicyExpenseChat(ReportUtils.getRootParentReport(report)), [report]);
// A flag for showing the categories page
const shouldShowCategories = isPolicyExpenseChat && Permissions.canUseCategories(betas) && (transactionCategory || OptionsListUtils.hasEnabledOptions(lodashValues(policyCategories)));
// A flag for showing the tags page
const shouldShowTags = isPolicyExpenseChat && Permissions.canUseTags(betas) && (transactionTag || OptionsListUtils.hasEnabledOptions(lodashValues(policyTagList)));
// Dismiss the modal when the request is paid or deleted
useEffect(() => {
if (canEdit) {
return;
}
Navigation.isNavigationReady().then(() => {
Navigation.dismissModal();
});
}, [canEdit]);
// Update the transaction object and close the modal
function editMoneyRequest(transactionChanges) {
if (TransactionUtils.isDistanceRequest(transaction)) {
IOU.updateDistanceRequest(transaction.transactionID, report.reportID, transactionChanges);
} else {
IOU.editMoneyRequest(transaction.transactionID, report.reportID, transactionChanges);
}
Navigation.dismissModal(report.reportID);
}
if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.DESCRIPTION) {
return (
<EditRequestDescriptionPage
defaultDescription={transactionDescription}
onSubmit={(transactionChanges) => {
// In case the comment hasn't been changed, do not make the API request.
if (transactionChanges.comment.trim() === transactionDescription) {
Navigation.dismissModal();
return;
}
editMoneyRequest({comment: transactionChanges.comment.trim()});
}}
/>
);
}
if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.DATE) {
return (
<EditRequestCreatedPage
defaultCreated={transactionCreated}
onSubmit={(transactionChanges) => {
// In case the date hasn't been changed, do not make the API request.
if (transactionChanges.created === transactionCreated) {
Navigation.dismissModal();
return;
}
editMoneyRequest(transactionChanges);
}}
/>
);
}
if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.AMOUNT) {
return (
<EditRequestAmountPage
defaultAmount={transactionAmount}
defaultCurrency={defaultCurrency}
reportID={report.reportID}
onSubmit={(transactionChanges) => {
const amount = CurrencyUtils.convertToBackendAmount(Number.parseFloat(transactionChanges));
// In case the amount hasn't been changed, do not make the API request.
if (amount === transactionAmount && transactionCurrency === defaultCurrency) {
Navigation.dismissModal();
return;
}
// Temporarily disabling currency editing and it will be enabled as a quick follow up
editMoneyRequest({
amount,
currency: defaultCurrency,
});
}}
onNavigateToCurrency={() => {
const activeRoute = encodeURIComponent(Navigation.getActiveRoute().replace(/\?.*/, ''));
Navigation.navigate(ROUTES.EDIT_CURRENCY_REQUEST.getRoute(report.reportID, defaultCurrency, activeRoute));
}}
/>
);
}
if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.MERCHANT) {
return (
<EditRequestMerchantPage
defaultMerchant={transactionMerchant}
onSubmit={(transactionChanges) => {
// In case the merchant hasn't been changed, do not make the API request.
if (transactionChanges.merchant.trim() === transactionMerchant) {
Navigation.dismissModal();
return;
}
editMoneyRequest({merchant: transactionChanges.merchant.trim()});
}}
/>
);
}
if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.CATEGORY && shouldShowCategories) {
return (
<EditRequestCategoryPage
defaultCategory={transactionCategory}
policyID={lodashGet(report, 'policyID', '')}
onSubmit={(transactionChanges) => {
let updatedCategory = transactionChanges.category;
// In case the same category has been selected, do reset of the category.
if (transactionCategory === updatedCategory) {
updatedCategory = '';
}
editMoneyRequest({category: updatedCategory});
}}
/>
);
}
if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.TAG && shouldShowTags) {
return (
<EditRequestTagPage
defaultTag={transactionTag}
tagName={tagListName}
policyID={lodashGet(report, 'policyID', '')}
onSubmit={(transactionChanges) => {
let updatedTag = transactionChanges.tag;
// In case the same tag has been selected, reset the tag.
if (transactionTag === updatedTag) {
updatedTag = '';
}
editMoneyRequest({tag: updatedTag, tagListName});
}}
/>
);
}
if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) {
return (
<EditRequestReceiptPage
route={route}
transactionID={transaction.transactionID}
/>
);
}
if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.DISTANCE) {
return (
<EditRequestDistancePage
report={report}
transactionID={transaction.transactionID}
route={route}
/>
);
}
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={EditRequestPage.displayName}
>
<FullPageNotFoundView shouldShow />
</ScreenWrapper>
);
}
EditRequestPage.displayName = 'EditRequestPage';
EditRequestPage.propTypes = propTypes;
EditRequestPage.defaultProps = defaultProps;
export default compose(
withCurrentUserPersonalDetails,
withOnyx({
betas: {
key: ONYXKEYS.BETAS,
},
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${route.params.threadReportID}`,
},
}),
// eslint-disable-next-line rulesdir/no-multiple-onyx-in-file
withOnyx({
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '0'}`,
},
policyCategories: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${report ? report.policyID : '0'}`,
},
policyTags: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${report ? report.policyID : '0'}`,
},
parentReport: {
key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT}${report ? report.parentReportID : '0'}`,
},
parentReportActions: {
key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : '0'}`,
canEvict: false,
},
}),
// eslint-disable-next-line rulesdir/no-multiple-onyx-in-file
withOnyx({
transaction: {
key: ({report, parentReportActions}) => {
const parentReportActionID = lodashGet(report, 'parentReportActionID', '0');
const parentReportAction = lodashGet(parentReportActions, parentReportActionID);
return `${ONYXKEYS.COLLECTION.TRANSACTION}${lodashGet(parentReportAction, 'originalMessage.IOUTransactionID', 0)}`;
},
},
}),
)(EditRequestPage);