forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMoneyRequestConfirmPage.js
298 lines (272 loc) · 12.7 KB
/
MoneyRequestConfirmPage.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
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import {useIsFocused} from '@react-navigation/native';
import MoneyRequestConfirmationList from '../../../components/MoneyRequestConfirmationList';
import CONST from '../../../CONST';
import ScreenWrapper from '../../../components/ScreenWrapper';
import styles from '../../../styles/styles';
import Navigation from '../../../libs/Navigation/Navigation';
import ROUTES from '../../../ROUTES';
import * as IOU from '../../../libs/actions/IOU';
import compose from '../../../libs/compose';
import * as ReportUtils from '../../../libs/ReportUtils';
import * as OptionsListUtils from '../../../libs/OptionsListUtils';
import withLocalize from '../../../components/withLocalize';
import HeaderWithBackButton from '../../../components/HeaderWithBackButton';
import ONYXKEYS from '../../../ONYXKEYS';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsDefaultProps, withCurrentUserPersonalDetailsPropTypes} from '../../../components/withCurrentUserPersonalDetails';
import reportPropTypes from '../../reportPropTypes';
import personalDetailsPropType from '../../personalDetailsPropType';
import * as FileUtils from '../../../libs/fileDownload/FileUtils';
import * as Policy from '../../../libs/actions/Policy';
const propTypes = {
report: reportPropTypes,
/** Holds data related to Money Request view state, rather than the underlying Money Request data. */
iou: PropTypes.shape({
id: PropTypes.string,
amount: PropTypes.number,
comment: PropTypes.string,
created: PropTypes.string,
currency: PropTypes.string,
merchant: PropTypes.string,
participants: PropTypes.arrayOf(
PropTypes.shape({
accountID: PropTypes.number,
login: PropTypes.string,
isPolicyExpenseChat: PropTypes.bool,
isOwnPolicyExpenseChat: PropTypes.bool,
selected: PropTypes.bool,
}),
),
receiptPath: PropTypes.string,
}),
/** Personal details of all users */
personalDetails: personalDetailsPropType,
...withCurrentUserPersonalDetailsPropTypes,
};
const defaultProps = {
report: {},
personalDetails: {},
iou: {
id: '',
amount: 0,
currency: CONST.CURRENCY.USD,
comment: '',
merchant: '',
created: '',
participants: [],
},
...withCurrentUserPersonalDetailsDefaultProps,
};
function MoneyRequestConfirmPage(props) {
const prevMoneyRequestId = useRef(props.iou.id);
const iouType = useRef(lodashGet(props.route, 'params.iouType', ''));
const reportID = useRef(lodashGet(props.route, 'params.reportID', ''));
const participants = useMemo(
() =>
lodashGet(props.iou.participants, [0, 'isPolicyExpenseChat'], false)
? OptionsListUtils.getPolicyExpenseReportOptions(props.iou.participants[0])
: OptionsListUtils.getParticipantsOptions(props.iou.participants, props.personalDetails),
[props.iou.participants, props.personalDetails],
);
const isFocused = useIsFocused();
useEffect(() => {
const policyExpenseChat = _.find(participants, (participant) => participant.isPolicyExpenseChat);
if (policyExpenseChat) {
Policy.openDraftWorkspaceRequest(policyExpenseChat.policyID);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
// ID in Onyx could change by initiating a new request in a separate browser tab or completing a request
if (prevMoneyRequestId.current !== props.iou.id) {
// The ID is cleared on completing a request. In that case, we will do nothing.
if (props.iou.id) {
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType.current, reportID.current), true);
}
return;
}
// Reset the money request Onyx if the ID in Onyx does not match the ID from params
const moneyRequestId = `${iouType.current}${reportID.current}`;
const shouldReset = props.iou.id !== moneyRequestId;
if (shouldReset) {
IOU.resetMoneyRequestInfo(moneyRequestId);
}
if (_.isEmpty(props.iou.participants) || (props.iou.amount === 0 && !props.iou.receiptPath) || shouldReset) {
Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType.current, reportID.current), true);
}
return () => {
prevMoneyRequestId.current = props.iou.id;
};
}, [props.iou.participants, props.iou.amount, props.iou.id, props.iou.receiptPath]);
const navigateBack = () => {
let fallback;
if (reportID.current) {
fallback = ROUTES.getMoneyRequestRoute(iouType.current, reportID.current);
} else {
fallback = ROUTES.getMoneyRequestParticipantsRoute(iouType.current);
}
Navigation.goBack(fallback);
};
/**
* @param {Array} selectedParticipants
* @param {String} trimmedComment
* @param {File} [receipt]
*/
const requestMoney = useCallback(
(selectedParticipants, trimmedComment, receipt) => {
IOU.requestMoney(
props.report,
props.iou.amount,
props.iou.currency,
props.iou.created,
props.iou.merchant,
props.currentUserPersonalDetails.login,
props.currentUserPersonalDetails.accountID,
selectedParticipants[0],
trimmedComment,
receipt,
);
},
[props.report, props.iou.amount, props.iou.currency, props.iou.created, props.iou.merchant, props.currentUserPersonalDetails.login, props.currentUserPersonalDetails.accountID],
);
const createTransaction = useCallback(
(selectedParticipants) => {
const trimmedComment = props.iou.comment.trim();
// IOUs created from a group report will have a reportID param in the route.
// Since the user is already viewing the report, we don't need to navigate them to the report
if (iouType.current === CONST.IOU.MONEY_REQUEST_TYPE.SPLIT && CONST.REGEX.NUMBER.test(reportID.current)) {
IOU.splitBill(
selectedParticipants,
props.currentUserPersonalDetails.login,
props.currentUserPersonalDetails.accountID,
props.iou.amount,
trimmedComment,
props.iou.currency,
reportID.current,
);
return;
}
// If the request is created from the global create menu, we also navigate the user to the group report
if (iouType.current === CONST.IOU.MONEY_REQUEST_TYPE.SPLIT) {
IOU.splitBillAndOpenReport(
selectedParticipants,
props.currentUserPersonalDetails.login,
props.currentUserPersonalDetails.accountID,
props.iou.amount,
trimmedComment,
props.iou.currency,
);
return;
}
if (props.iou.receiptPath && props.iou.receiptSource) {
FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptSource).then((receipt) => {
requestMoney(selectedParticipants, trimmedComment, receipt);
});
return;
}
requestMoney(selectedParticipants, trimmedComment);
},
[
props.iou.amount,
props.iou.comment,
props.currentUserPersonalDetails.login,
props.currentUserPersonalDetails.accountID,
props.iou.currency,
props.iou.receiptPath,
props.iou.receiptSource,
requestMoney,
],
);
/**
* Checks if user has a GOLD wallet then creates a paid IOU report on the fly
*
* @param {String} paymentMethodType
*/
const sendMoney = useCallback(
(paymentMethodType) => {
const currency = props.iou.currency;
const trimmedComment = props.iou.comment.trim();
const participant = participants[0];
if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.ELSEWHERE) {
IOU.sendMoneyElsewhere(props.report, props.iou.amount, currency, trimmedComment, props.currentUserPersonalDetails.accountID, participant);
return;
}
if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.PAYPAL_ME) {
IOU.sendMoneyViaPaypal(props.report, props.iou.amount, currency, trimmedComment, props.currentUserPersonalDetails.accountID, participant);
return;
}
if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.EXPENSIFY) {
IOU.sendMoneyWithWallet(props.report, props.iou.amount, currency, trimmedComment, props.currentUserPersonalDetails.accountID, participant);
}
},
[props.iou.amount, props.iou.comment, participants, props.iou.currency, props.currentUserPersonalDetails.accountID, props.report],
);
return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
{({safeAreaPaddingBottomStyle}) => (
<View style={[styles.flex1, safeAreaPaddingBottomStyle]}>
<HeaderWithBackButton
title={props.translate('iou.cash')}
onBackButtonPress={navigateBack}
/>
<MoneyRequestConfirmationList
hasMultipleParticipants={iouType.current === CONST.IOU.MONEY_REQUEST_TYPE.SPLIT}
selectedParticipants={participants}
iouAmount={props.iou.amount}
iouComment={props.iou.comment}
iouCurrencyCode={props.iou.currency}
onConfirm={createTransaction}
onSendMoney={sendMoney}
onSelectParticipant={(option) => {
const newParticipants = _.map(props.iou.participants, (participant) => {
if (participant.accountID === option.accountID) {
return {...participant, selected: !participant.selected};
}
return participant;
});
IOU.setMoneyRequestParticipants(newParticipants);
}}
receiptPath={props.iou.receiptPath}
receiptSource={props.iou.receiptSource}
iouType={iouType.current}
reportID={reportID.current}
// The participants can only be modified when the action is initiated from directly within a group chat and not the floating-action-button.
// This is because when there is a group of people, say they are on a trip, and you have some shared expenses with some of the people,
// but not all of them (maybe someone skipped out on dinner). Then it's nice to be able to select/deselect people from the group chat bill
// split rather than forcing the user to create a new group, just for that expense. The reportID is empty, when the action was initiated from
// the floating-action-button (since it is something that exists outside the context of a report).
canModifyParticipants={!_.isEmpty(reportID.current)}
policyID={props.report.policyID}
bankAccountRoute={ReportUtils.getBankAccountRoute(props.report)}
iouMerchant={props.iou.merchant}
iouCreated={props.iou.created}
disableArrowKeysActions={!isFocused}
/>
</View>
)}
</ScreenWrapper>
);
}
MoneyRequestConfirmPage.displayName = 'MoneyRequestConfirmPage';
MoneyRequestConfirmPage.propTypes = propTypes;
MoneyRequestConfirmPage.defaultProps = defaultProps;
export default compose(
withCurrentUserPersonalDetails,
withLocalize,
withOnyx({
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${lodashGet(route, 'params.reportID', '')}`,
},
iou: {
key: ONYXKEYS.IOU,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
}),
)(MoneyRequestConfirmPage);