-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
MoneyRequestConfirmationList.js
executable file
·352 lines (307 loc) · 13.2 KB
/
MoneyRequestConfirmationList.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
342
343
344
345
346
347
348
349
350
351
352
import React, {useState, useCallback, useMemo} from 'react';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import styles from '../styles/styles';
import * as OptionsListUtils from '../libs/OptionsListUtils';
import OptionsSelector from './OptionsSelector';
import ONYXKEYS from '../ONYXKEYS';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import withWindowDimensions, {windowDimensionsPropTypes} from './withWindowDimensions';
import compose from '../libs/compose';
import CONST from '../CONST';
import ButtonWithDropdownMenu from './ButtonWithDropdownMenu';
import Log from '../libs/Log';
import SettlementButton from './SettlementButton';
import ROUTES from '../ROUTES';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps} from './withCurrentUserPersonalDetails';
import * as IOUUtils from '../libs/IOUUtils';
import MenuItemWithTopDescription from './MenuItemWithTopDescription';
import Navigation from '../libs/Navigation/Navigation';
import optionPropTypes from './optionPropTypes';
import * as CurrencyUtils from '../libs/CurrencyUtils';
import * as ReportUtils from '../libs/ReportUtils';
const propTypes = {
/** Callback to inform parent modal of success */
onConfirm: PropTypes.func,
/** Callback to parent modal to send money */
onSendMoney: PropTypes.func,
/** Should we request a single or multiple participant selection from user */
hasMultipleParticipants: PropTypes.bool.isRequired,
/** IOU amount */
iouAmount: PropTypes.number.isRequired,
/** IOU comment */
iouComment: PropTypes.string,
/** IOU currency */
iouCurrencyCode: PropTypes.string,
/** IOU type */
iouType: PropTypes.string,
/** Selected participants from MoneyRequestModal with login */
participants: PropTypes.arrayOf(optionPropTypes).isRequired,
/** Payee of the money request with login */
payeePersonalDetails: optionPropTypes,
/** Can the participants be modified or not */
canModifyParticipants: PropTypes.bool,
/** Should the list be read only, and not editable? */
isReadOnly: PropTypes.bool,
/** Depending on expense report or personal IOU report, respective bank account route */
bankAccountRoute: PropTypes.string,
...windowDimensionsPropTypes,
...withLocalizePropTypes,
...withCurrentUserPersonalDetailsPropTypes,
/* Onyx Props */
/** Holds data related to IOU view state, rather than the underlying IOU data. */
iou: PropTypes.shape({
/** Whether or not the IOU step is loading (creating the IOU Report) */
loading: PropTypes.bool,
}),
/** Current user session */
session: PropTypes.shape({
email: PropTypes.string.isRequired,
}),
/** Callback function to navigate to a provided step in the MoneyRequestModal flow */
navigateToStep: PropTypes.func,
/** The policyID of the request */
policyID: PropTypes.string,
};
const defaultProps = {
onConfirm: () => {},
onSendMoney: () => {},
navigateToStep: () => {},
iou: {
loading: false,
},
iouType: CONST.IOU.MONEY_REQUEST_TYPE.REQUEST,
payeePersonalDetails: null,
canModifyParticipants: false,
isReadOnly: false,
bankAccountRoute: '',
session: {
email: null,
},
policyID: '',
...withCurrentUserPersonalDetailsDefaultProps,
};
function MoneyRequestConfirmationList(props) {
// Destructure functions from props to pass it as a dependecy to useCallback/useMemo hooks.
// Prop functions pass props itself as a "this" value to the function which means they change every time props change.
const {translate, onSendMoney, onConfirm} = props;
/**
* Returns the participants with amount
* @param {Array} participants
* @returns {Array}
*/
const getParticipantsWithAmount = useCallback(
(participantsList) => {
const iouAmount = IOUUtils.calculateAmount(participantsList.length, props.iouAmount);
return OptionsListUtils.getIOUConfirmationOptionsFromParticipants(participantsList, CurrencyUtils.convertToDisplayString(iouAmount, props.iouCurrencyCode));
},
[props.iouAmount, props.iouCurrencyCode],
);
const getFormattedParticipants = () =>
_.map(getParticipantsWithAmount(props.participants), (participant) => ({
...participant,
selected: true,
}));
const [participants, setParticipants] = useState(getFormattedParticipants);
const [didConfirm, setDidConfirm] = useState(false);
const splitOrRequestOptions = useMemo(() => {
const text = translate(props.hasMultipleParticipants ? 'iou.splitAmount' : 'iou.requestAmount', {
amount: CurrencyUtils.convertToDisplayString(props.iouAmount, props.iouCurrencyCode),
});
return [
{
text: text[0].toUpperCase() + text.slice(1),
value: props.hasMultipleParticipants ? CONST.IOU.MONEY_REQUEST_TYPE.SPLIT : CONST.IOU.MONEY_REQUEST_TYPE.REQUEST,
},
];
}, [props.hasMultipleParticipants, props.iouAmount, props.iouCurrencyCode, translate]);
const selectedParticipants = useMemo(() => _.filter(participants, (participant) => participant.selected), [participants]);
const getParticipantsWithoutAmount = useCallback((participantsList) => _.map(participantsList, (option) => _.omit(option, 'descriptiveText')), []);
const payeePersonalDetails = useMemo(() => props.payeePersonalDetails || props.currentUserPersonalDetails, [props.payeePersonalDetails, props.currentUserPersonalDetails]);
const optionSelectorSections = useMemo(() => {
const sections = [];
const unselectedParticipants = _.filter(participants, (participant) => !participant.selected);
if (props.hasMultipleParticipants) {
const formattedSelectedParticipants = getParticipantsWithAmount(selectedParticipants);
const formattedUnselectedParticipants = getParticipantsWithoutAmount(unselectedParticipants);
const formattedParticipantsList = _.union(formattedSelectedParticipants, formattedUnselectedParticipants);
const myIOUAmount = IOUUtils.calculateAmount(selectedParticipants.length, props.iouAmount, true);
const formattedPayeePersonalDetails = OptionsListUtils.getIOUConfirmationOptionsFromPayeePersonalDetail(
payeePersonalDetails,
CurrencyUtils.convertToDisplayString(myIOUAmount, props.iouCurrencyCode),
);
sections.push(
{
title: translate('moneyRequestConfirmationList.whoPaid'),
data: [formattedPayeePersonalDetails],
shouldShow: true,
indexOffset: 0,
},
{
title: translate('moneyRequestConfirmationList.whoWasThere'),
data: formattedParticipantsList,
shouldShow: true,
indexOffset: 1,
},
);
} else {
const formattedParticipantsList = getParticipantsWithoutAmount(props.participants);
sections.push({
title: translate('common.to'),
data: formattedParticipantsList,
shouldShow: true,
indexOffset: 0,
});
}
return sections;
}, [
selectedParticipants,
getParticipantsWithAmount,
getParticipantsWithoutAmount,
props.hasMultipleParticipants,
props.iouAmount,
props.iouCurrencyCode,
props.participants,
participants,
translate,
payeePersonalDetails,
]);
const selectedOptions = useMemo(() => {
if (!props.hasMultipleParticipants) {
return [];
}
return [...selectedParticipants, OptionsListUtils.getIOUConfirmationOptionsFromPayeePersonalDetail(payeePersonalDetails)];
}, [selectedParticipants, props.hasMultipleParticipants, payeePersonalDetails]);
/**
* Toggle selected option's selected prop.
* @param {Object} option
*/
const toggleOption = useCallback(
(option) => {
// Return early if selected option is currently logged in user.
if (option.login === props.session.email) {
return;
}
setParticipants((prevParticipants) => {
const newParticipants = _.map(prevParticipants, (participant) => {
if (participant.login === option.login) {
return {...participant, selected: !participant.selected};
}
return participant;
});
return newParticipants;
});
},
[props.session.email],
);
/**
* Navigate to profile of selected user
* @param {Object} option
*/
const navigateToUserDetail = (option) => {
if (!option.login) {
return;
}
Navigation.navigate(ROUTES.getProfileRoute(ReportUtils.getAccountIDForLogin(option.login)));
};
/**
* @param {String} paymentMethod
*/
const confirm = useCallback(
(paymentMethod) => {
setDidConfirm(true);
if (_.isEmpty(selectedParticipants)) {
return;
}
if (props.iouType === CONST.IOU.MONEY_REQUEST_TYPE.SEND) {
if (!paymentMethod) {
return;
}
Log.info(`[IOU] Sending money via: ${paymentMethod}`);
onSendMoney(paymentMethod);
} else {
onConfirm(selectedParticipants);
}
},
[selectedParticipants, onSendMoney, onConfirm, props.iouType],
);
const canModifyParticipants = !props.isReadOnly && props.canModifyParticipants && props.hasMultipleParticipants;
const formattedAmount = CurrencyUtils.convertToDisplayString(props.iouAmount, props.iouCurrencyCode);
const footerContent = useMemo(() => {
if (props.isReadOnly) {
return;
}
const shouldShowSettlementButton = props.iouType === CONST.IOU.MONEY_REQUEST_TYPE.SEND;
const shouldDisableButton = selectedParticipants.length === 0;
const recipient = participants[0];
return shouldShowSettlementButton ? (
<SettlementButton
isDisabled={shouldDisableButton}
onPress={confirm}
shouldShowPaypal={Boolean(recipient && recipient.payPalMeAddress)}
enablePaymentsRoute={ROUTES.IOU_SEND_ENABLE_PAYMENTS}
addBankAccountRoute={props.bankAccountRoute}
addDebitCardRoute={ROUTES.IOU_SEND_ADD_DEBIT_CARD}
currency={props.iouCurrencyCode}
policyID={props.policyID}
shouldShowPaymentOptions
/>
) : (
<ButtonWithDropdownMenu
isDisabled={shouldDisableButton}
onPress={(_event, value) => confirm(value)}
options={splitOrRequestOptions}
/>
);
}, [confirm, participants, props.bankAccountRoute, props.iouCurrencyCode, props.iouType, props.isReadOnly, props.policyID, selectedParticipants, splitOrRequestOptions]);
return (
<OptionsSelector
sections={optionSelectorSections}
value=""
onSelectRow={canModifyParticipants ? toggleOption : navigateToUserDetail}
onConfirmSelection={confirm}
selectedOptions={selectedOptions}
canSelectMultipleOptions={canModifyParticipants}
disableArrowKeysActions={!canModifyParticipants}
boldStyle
showTitleTooltip
shouldTextInputAppearBelowOptions
shouldShowTextInput={false}
shouldUseStyleForChildren={false}
optionHoveredStyle={canModifyParticipants ? styles.hoveredComponentBG : {}}
footerContent={footerContent}
>
<MenuItemWithTopDescription
shouldShowRightIcon={!props.isReadOnly}
title={formattedAmount}
description={translate('iou.amount')}
onPress={() => props.navigateToStep(0)}
style={[styles.moneyRequestMenuItem, styles.mt2]}
titleStyle={styles.moneyRequestConfirmationAmount}
disabled={didConfirm || props.isReadOnly}
/>
<MenuItemWithTopDescription
shouldShowRightIcon={!props.isReadOnly}
title={props.iouComment}
description={translate('common.description')}
onPress={() => Navigation.navigate(ROUTES.MONEY_REQUEST_DESCRIPTION)}
style={[styles.moneyRequestMenuItem, styles.mb2]}
disabled={didConfirm || props.isReadOnly}
/>
</OptionsSelector>
);
}
MoneyRequestConfirmationList.propTypes = propTypes;
MoneyRequestConfirmationList.defaultProps = defaultProps;
export default compose(
withLocalize,
withWindowDimensions,
withCurrentUserPersonalDetails,
withOnyx({
iou: {key: ONYXKEYS.IOU},
session: {
key: ONYXKEYS.SESSION,
},
}),
)(MoneyRequestConfirmationList);