-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
MoneyRequestParticipantsSelector.js
executable file
·379 lines (336 loc) · 14.3 KB
/
MoneyRequestParticipantsSelector.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useCallback, useMemo, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import Button from '@components/Button';
import FormHelpMessage from '@components/FormHelpMessage';
import {usePersonalDetails} from '@components/OnyxProvider';
import {PressableWithFeedback} from '@components/Pressable';
import ReferralProgramCTA from '@components/ReferralProgramCTA';
import SelectCircle from '@components/SelectCircle';
import SelectionList from '@components/SelectionList';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Report from '@libs/actions/Report';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import reportPropTypes from '@pages/reportPropTypes';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
const propTypes = {
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),
/** Callback to request parent modal to go to next step, which should be request */
navigateToRequest: PropTypes.func.isRequired,
/** Callback to request parent modal to go to next step, which should be split */
navigateToSplit: PropTypes.func.isRequired,
/** Callback to add participants in MoneyRequestModal */
onAddParticipants: PropTypes.func.isRequired,
/** Selected participants from MoneyRequestModal with login */
participants: PropTypes.arrayOf(
PropTypes.shape({
accountID: PropTypes.number,
login: PropTypes.string,
isPolicyExpenseChat: PropTypes.bool,
isOwnPolicyExpenseChat: PropTypes.bool,
selected: PropTypes.bool,
}),
),
/** All reports shared with the user */
reports: PropTypes.objectOf(reportPropTypes),
/** padding bottom style of safe area */
safeAreaPaddingBottomStyle: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.object]),
/** The type of IOU report, i.e. bill, request, send */
iouType: PropTypes.string.isRequired,
/** Whether the money request is a distance request or not */
isDistanceRequest: PropTypes.bool,
/** Whether we are searching for reports in the server */
isSearchingForReports: PropTypes.bool,
};
const defaultProps = {
participants: [],
safeAreaPaddingBottomStyle: {},
reports: {},
betas: [],
isDistanceRequest: false,
isSearchingForReports: false,
};
function MoneyRequestParticipantsSelector({
betas,
participants,
reports,
navigateToRequest,
navigateToSplit,
onAddParticipants,
safeAreaPaddingBottomStyle,
iouType,
isDistanceRequest,
isSearchingForReports,
}) {
const {translate} = useLocalize();
const styles = useThemeStyles();
const [searchTerm, setSearchTerm] = useState('');
const {isOffline} = useNetwork();
const personalDetails = usePersonalDetails();
const offlineMessage = isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : '';
const newChatOptions = useMemo(() => {
const chatOptions = OptionsListUtils.getFilteredOptions(
reports,
personalDetails,
betas,
searchTerm,
participants,
CONST.EXPENSIFY_EMAILS,
// If we are using this component in the "Request money" flow then we pass the includeOwnedWorkspaceChats argument so that the current user
// sees the option to request money from their admin on their own Workspace Chat.
iouType === CONST.IOU.TYPE.REQUEST,
// We don't want to include any P2P options like personal details or reports that are not workspace chats for certain features.
!isDistanceRequest,
false,
{},
[],
false,
{},
[],
// We don't want the user to be able to invite individuals when they are in the "Distance request" flow for now.
// This functionality is being built here: https://github.com/Expensify/App/issues/23291
!isDistanceRequest,
true,
);
return {
recentReports: chatOptions.recentReports,
personalDetails: chatOptions.personalDetails,
userToInvite: chatOptions.userToInvite,
};
}, [betas, reports, participants, personalDetails, searchTerm, iouType, isDistanceRequest]);
const maxParticipantsReached = participants.length === CONST.REPORT.MAXIMUM_PARTICIPANTS;
/**
* Returns the sections needed for the OptionsSelector
*
* @returns {Array}
*/
const sections = useMemo(() => {
const newSections = [];
let indexOffset = 0;
const formatResults = OptionsListUtils.formatSectionsFromSearchTerm(
searchTerm,
participants,
newChatOptions.recentReports,
newChatOptions.personalDetails,
personalDetails,
true,
indexOffset,
);
newSections.push(formatResults.section);
indexOffset = formatResults.newIndexOffset;
if (maxParticipantsReached) {
return newSections;
}
newSections.push({
title: translate('common.recents'),
data: newChatOptions.recentReports,
shouldShow: !_.isEmpty(newChatOptions.recentReports),
indexOffset,
});
indexOffset += newChatOptions.recentReports.length;
newSections.push({
title: translate('common.contacts'),
data: newChatOptions.personalDetails,
shouldShow: !_.isEmpty(newChatOptions.personalDetails),
indexOffset,
});
indexOffset += newChatOptions.personalDetails.length;
if (newChatOptions.userToInvite && !OptionsListUtils.isCurrentUser(newChatOptions.userToInvite)) {
newSections.push({
title: undefined,
data: _.map([newChatOptions.userToInvite], (participant) => {
const isPolicyExpenseChat = lodashGet(participant, 'isPolicyExpenseChat', false);
return isPolicyExpenseChat ? OptionsListUtils.getPolicyExpenseReportOption(participant) : OptionsListUtils.getParticipantsOption(participant, personalDetails);
}),
shouldShow: true,
indexOffset,
});
}
return newSections;
}, [maxParticipantsReached, newChatOptions.personalDetails, newChatOptions.recentReports, newChatOptions.userToInvite, participants, personalDetails, searchTerm, translate]);
/**
* Adds a single participant to the request
*
* @param {Object} option
*/
const addSingleParticipant = (option) => {
if (participants.length) {
return;
}
onAddParticipants(
[
{
accountID: option.accountID,
login: option.login,
isPolicyExpenseChat: option.isPolicyExpenseChat,
reportID: option.reportID,
selected: true,
searchText: option.searchText,
},
],
false,
);
navigateToRequest();
};
/**
* Removes a selected option from list if already selected. If not already selected add this option to the list.
* @param {Object} option
*/
const addParticipantToSelection = useCallback(
(option) => {
const isOptionSelected = (selectedOption) => {
if (selectedOption.accountID && selectedOption.accountID === option.accountID) {
return true;
}
if (selectedOption.reportID && selectedOption.reportID === option.reportID) {
return true;
}
return false;
};
const isOptionInList = _.some(participants, isOptionSelected);
let newSelectedOptions;
if (isOptionInList) {
newSelectedOptions = _.reject(participants, isOptionSelected);
} else {
newSelectedOptions = [
...participants,
{
accountID: option.accountID,
login: option.login,
isPolicyExpenseChat: option.isPolicyExpenseChat,
reportID: option.reportID,
selected: true,
searchText: option.searchText,
},
];
}
onAddParticipants(newSelectedOptions, newSelectedOptions.length !== 0);
},
[participants, onAddParticipants],
);
const headerMessage = useMemo(
() =>
OptionsListUtils.getHeaderMessage(
newChatOptions.personalDetails.length + newChatOptions.recentReports.length !== 0,
Boolean(newChatOptions.userToInvite),
searchTerm.trim(),
maxParticipantsReached,
_.some(participants, (participant) => participant.searchText.toLowerCase().includes(searchTerm.trim().toLowerCase())),
),
[maxParticipantsReached, newChatOptions.personalDetails.length, newChatOptions.recentReports.length, newChatOptions.userToInvite, participants, searchTerm],
);
// When search term updates we will fetch any reports
const setSearchTermAndSearchInServer = useCallback((text = '') => {
Report.searchInServer(text);
setSearchTerm(text);
}, []);
// Right now you can't split a request with a workspace and other additional participants
// This is getting properly fixed in https://github.com/Expensify/App/issues/27508, but as a stop-gap to prevent
// the app from crashing on native when you try to do this, we'll going to show error message if you have a workspace and other participants
const hasPolicyExpenseChatParticipant = _.some(participants, (participant) => participant.isPolicyExpenseChat);
const shouldShowSplitBillErrorMessage = participants.length > 1 && hasPolicyExpenseChatParticipant;
const isAllowedToSplit = !isDistanceRequest && iouType !== CONST.IOU.TYPE.SEND;
const handleConfirmSelection = useCallback(() => {
if (shouldShowSplitBillErrorMessage) {
return;
}
navigateToSplit();
}, [shouldShowSplitBillErrorMessage, navigateToSplit]);
const referralContentType = iouType === CONST.IOU.TYPE.SEND ? CONST.REFERRAL_PROGRAM.CONTENT_TYPES.SEND_MONEY : CONST.REFERRAL_PROGRAM.CONTENT_TYPES.MONEY_REQUEST;
const footerContent = useMemo(
() => (
<View>
<View style={[styles.flexShrink0, !!participants.length && !shouldShowSplitBillErrorMessage && styles.pb5]}>
<ReferralProgramCTA referralContentType={referralContentType} />
</View>
{shouldShowSplitBillErrorMessage && (
<FormHelpMessage
style={[styles.ph1, styles.mb2]}
isError
message="iou.error.splitBillMultipleParticipantsErrorMessage"
/>
)}
{!!participants.length && (
<Button
success
text={translate('iou.addToSplit')}
onPress={handleConfirmSelection}
pressOnEnter
isDisabled={shouldShowSplitBillErrorMessage}
/>
)}
</View>
),
[handleConfirmSelection, participants.length, referralContentType, shouldShowSplitBillErrorMessage, styles, translate],
);
const itemRightSideComponent = useCallback(
(item) => {
if (!isAllowedToSplit) {
return null;
}
if (item.isSelected) {
return (
<PressableWithFeedback
onPress={() => addParticipantToSelection(item)}
disabled={item.isDisabled}
role={CONST.ACCESSIBILITY_ROLE.CHECKBOX}
accessibilityLabel={CONST.ACCESSIBILITY_ROLE.CHECKBOX}
style={[styles.flexRow, styles.alignItemsCenter, styles.ml3]}
>
<SelectCircle isChecked={item.isSelected} />
</PressableWithFeedback>
);
}
return (
<Button
onPress={() => addParticipantToSelection(item)}
style={[styles.pl2]}
text={translate('iou.split')}
small
/>
);
},
[addParticipantToSelection, isAllowedToSplit, styles, translate],
);
return (
<View style={[styles.flex1, styles.w100, participants.length > 0 ? safeAreaPaddingBottomStyle : {}]}>
<SelectionList
onConfirm={handleConfirmSelection}
sections={sections}
textInputValue={searchTerm}
textInputLabel={translate('optionsSelector.nameEmailOrPhoneNumber')}
textInputHint={offlineMessage}
onChangeText={setSearchTermAndSearchInServer}
shouldPreventDefaultFocusOnSelectRow={!DeviceCapabilities.canUseTouchScreen()}
onSelectRow={addSingleParticipant}
footerContent={footerContent}
headerMessage={headerMessage}
showLoadingPlaceholder={isSearchingForReports}
rightHandSideComponent={itemRightSideComponent}
/>
</View>
);
}
MoneyRequestParticipantsSelector.propTypes = propTypes;
MoneyRequestParticipantsSelector.displayName = 'MoneyRequestParticipantsSelector';
MoneyRequestParticipantsSelector.defaultProps = defaultProps;
export default withOnyx({
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
betas: {
key: ONYXKEYS.BETAS,
},
isSearchingForReports: {
key: ONYXKEYS.IS_SEARCHING_FOR_REPORTS,
initWithStoredValues: false,
},
})(MoneyRequestParticipantsSelector);