-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWorkspaceMembersPage.js
517 lines (484 loc) · 22 KB
/
WorkspaceMembersPage.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import React, {useState, useEffect, useCallback} from 'react';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import styles from '../../styles/styles';
import ONYXKEYS from '../../ONYXKEYS';
import HeaderWithBackButton from '../../components/HeaderWithBackButton';
import Navigation from '../../libs/Navigation/Navigation';
import ScreenWrapper from '../../components/ScreenWrapper';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import * as Policy from '../../libs/actions/Policy';
import * as OptionsListUtils from '../../libs/OptionsListUtils';
import Button from '../../components/Button';
import Checkbox from '../../components/Checkbox';
import Text from '../../components/Text';
import ROUTES from '../../ROUTES';
import ConfirmModal from '../../components/ConfirmModal';
import personalDetailsPropType from '../personalDetailsPropType';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../components/withWindowDimensions';
import OptionRow from '../../components/OptionRow';
import {policyPropTypes, policyDefaultProps} from './withPolicy';
import withPolicyAndFullscreenLoading from './withPolicyAndFullscreenLoading';
import CONST from '../../CONST';
import OfflineWithFeedback from '../../components/OfflineWithFeedback';
import {withNetwork} from '../../components/OnyxProvider';
import FullPageNotFoundView from '../../components/BlockingViews/FullPageNotFoundView';
import networkPropTypes from '../../components/networkPropTypes';
import * as UserUtils from '../../libs/UserUtils';
import FormHelpMessage from '../../components/FormHelpMessage';
import TextInput from '../../components/TextInput';
import KeyboardDismissingFlatList from '../../components/KeyboardDismissingFlatList';
import withCurrentUserPersonalDetails from '../../components/withCurrentUserPersonalDetails';
import * as PolicyUtils from '../../libs/PolicyUtils';
import PressableWithFeedback from '../../components/Pressable/PressableWithFeedback';
import usePrevious from '../../hooks/usePrevious';
import Log from '../../libs/Log';
import * as PersonalDetailsUtils from '../../libs/PersonalDetailsUtils';
const propTypes = {
/** The personal details of the person who is logged in */
personalDetails: personalDetailsPropType,
/** URL Route params */
route: PropTypes.shape({
/** Params from the URL path */
params: PropTypes.shape({
/** policyID passed via route: /workspace/:policyID/members */
policyID: PropTypes.string,
}),
}).isRequired,
/** Session info for the currently logged in user. */
session: PropTypes.shape({
/** Currently logged in user accountID */
accountID: PropTypes.number,
}),
...policyPropTypes,
...withLocalizePropTypes,
...windowDimensionsPropTypes,
network: networkPropTypes.isRequired,
};
const defaultProps = {
personalDetails: {},
session: {
accountID: 0,
},
...policyDefaultProps,
};
function WorkspaceMembersPage(props) {
const [selectedEmployees, setSelectedEmployees] = useState([]);
const [removeMembersConfirmModalVisible, setRemoveMembersConfirmModalVisible] = useState(false);
const [errors, setErrors] = useState({});
const [searchValue, setSearchValue] = useState('');
const prevIsOffline = usePrevious(props.network.isOffline);
/**
* Get members for the current workspace
*/
const getWorkspaceMembers = useCallback(() => {
Policy.openWorkspaceMembersPage(props.route.params.policyID, _.keys(PolicyUtils.getClientPolicyMemberEmailsToAccountIDs(props.policyMembers, props.personalDetails)));
}, [props.route.params.policyID, props.policyMembers, props.personalDetails]);
/**
* Check if the current selection includes members that cannot be removed
*/
const validateSelection = useCallback(() => {
const newErrors = {};
const ownerAccountID = _.first(PersonalDetailsUtils.getAccountIDsByLogins([props.policy.owner]));
_.each(selectedEmployees, (member) => {
if (member !== ownerAccountID && member !== props.session.accountID) {
return;
}
newErrors[member] = props.translate('workspace.people.error.cannotRemove');
});
setErrors(newErrors);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedEmployees, props.policy.owner, props.session.accountID]);
useEffect(() => {
getWorkspaceMembers();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
validateSelection();
}, [props.preferredLocale, validateSelection]);
useEffect(() => {
setSelectedEmployees((prevSelected) =>
_.intersection(
prevSelected,
_.map(_.values(PolicyUtils.getClientPolicyMemberEmailsToAccountIDs(props.policyMembers, props.personalDetails)), (accountID) => Number(accountID)),
),
);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.policyMembers]);
useEffect(() => {
const isReconnecting = prevIsOffline && !props.network.isOffline;
if (!isReconnecting) {
return;
}
getWorkspaceMembers();
}, [props.network.isOffline, prevIsOffline, getWorkspaceMembers]);
/**
* This function will iterate through the details of each policy member to check if the
* search string matches with any detail and return that filter.
* @param {Array} policyMembersPersonalDetails - This is the list of policy members
* @param {*} search - This is the string that the user has entered
* @returns {Array} - The list of policy members that have anything similar to the searchValue
*/
const getMemberOptions = (policyMembersPersonalDetails, search) => {
// If no search value, we return all members.
if (_.isEmpty(search)) {
return policyMembersPersonalDetails;
}
// We will filter through each policy member details to determine if they should be shown
return _.filter(policyMembersPersonalDetails, (member) => {
let memberDetails = '';
if (member.login) {
memberDetails += ` ${member.login.toLowerCase()}`;
}
if (member.firstName) {
memberDetails += ` ${member.firstName.toLowerCase()}`;
}
if (member.lastName) {
memberDetails += ` ${member.lastName.toLowerCase()}`;
}
if (member.displayName) {
memberDetails += ` ${member.displayName.toLowerCase()}`;
}
if (member.phoneNumber) {
memberDetails += ` ${member.phoneNumber.toLowerCase()}`;
}
return OptionsListUtils.isSearchStringMatch(search, memberDetails);
});
};
/**
* Open the modal to invite a user
*/
const inviteUser = () => {
setSearchValue('');
Navigation.navigate(ROUTES.getWorkspaceInviteRoute(props.route.params.policyID));
};
/**
* Remove selected users from the workspace
*/
const removeUsers = () => {
if (!_.isEmpty(errors)) {
return;
}
// Remove the admin from the list
const accountIDsToRemove = _.without(selectedEmployees, props.session.accountID);
Policy.removeMembers(accountIDsToRemove, props.route.params.policyID);
setSelectedEmployees([]);
setRemoveMembersConfirmModalVisible(false);
};
/**
* Show the modal to confirm removal of the selected members
*/
const askForConfirmationToRemove = () => {
if (!_.isEmpty(errors)) {
return;
}
setRemoveMembersConfirmModalVisible(true);
};
/**
* Add or remove all users passed from the selectedEmployees list
* @param {Object} memberList
*/
const toggleAllUsers = (memberList) => {
const accountIDList = _.map(_.keys(memberList), (memberAccountID) => Number(memberAccountID));
setSelectedEmployees((prevSelected) => (!_.every(accountIDList, (memberAccountID) => _.contains(prevSelected, memberAccountID)) ? accountIDList : []));
validateSelection();
};
/**
* Add user from the selectedEmployees list
*
* @param {String} login
*/
const addUser = useCallback(
(accountID) => {
setSelectedEmployees((prevSelected) => [...prevSelected, accountID]);
validateSelection();
},
[validateSelection],
);
/**
* Remove user from the selectedEmployees list
*
* @param {String} login
*/
const removeUser = useCallback(
(accountID) => {
setSelectedEmployees((prevSelected) => _.without(prevSelected, accountID));
validateSelection();
},
[validateSelection],
);
/**
* Toggle user from the selectedEmployees list
*
* @param {String} accountID
* @param {String} pendingAction
*
*/
const toggleUser = useCallback(
(accountID, pendingAction) => {
if (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
return;
}
// Add or remove the user if the checkbox is enabled
if (_.contains(selectedEmployees, Number(accountID))) {
removeUser(accountID);
} else {
addUser(accountID);
}
},
[selectedEmployees, addUser, removeUser],
);
/**
* Dismisses the errors on one item
*
* @param {Object} item
*/
const dismissError = useCallback(
(item) => {
if (item.pendingAction === 'delete') {
Policy.clearDeleteMemberError(props.route.params.policyID, item.accountID);
} else {
Policy.clearAddMemberError(props.route.params.policyID, item.accountID);
}
},
[props.route.params.policyID],
);
/**
* Check if the policy member is deleted from the workspace
*
* @param {Object} policyMember
* @returns {Boolean}
*/
const isDeletedPolicyMember = (policyMember) => !props.network.isOffline && policyMember.pendingAction === 'delete' && _.isEmpty(policyMember.errors);
/**
* Render a workspace member component
*
* @param {Object} args
* @param {Object} args.item
* @param {Number} args.index
*
* @returns {React.Component}
*/
const renderItem = useCallback(
({item}) => {
const disabled = props.session.email === item.login || item.role === 'admin';
const hasError = !_.isEmpty(item.errors) || errors[item.accountID];
const isChecked = _.contains(selectedEmployees, Number(item.accountID));
return (
<OfflineWithFeedback
onClose={() => dismissError(item)}
pendingAction={item.pendingAction}
errors={item.errors}
>
<PressableWithFeedback
style={[styles.peopleRow, (_.isEmpty(item.errors) || errors[item.accountID]) && styles.peopleRowBorderBottom, hasError && styles.borderColorDanger]}
disabled={disabled}
onPress={() => toggleUser(item.accountID, item.pendingAction)}
accessibilityRole="checkbox"
accessibilityState={{
checked: isChecked,
}}
accessibilityLabel={props.formatPhoneNumber(item.displayName)}
hoverDimmingValue={1}
pressDimmingValue={0.7}
>
<Checkbox
disabled={disabled}
isChecked={isChecked}
onPress={() => toggleUser(item.accountID, item.pendingAction)}
accessibilityLabel={item.displayName}
/>
<View style={styles.flex1}>
<OptionRow
boldStyle
isDisabled={disabled}
option={{
text: props.formatPhoneNumber(item.displayName),
alternateText: props.formatPhoneNumber(item.login),
participantsList: [item],
icons: [
{
source: UserUtils.getAvatar(item.avatar, item.accountID),
name: item.login,
type: CONST.ICON_TYPE_AVATAR,
},
],
keyForList: item.accountID,
}}
onSelectRow={() => toggleUser(item.accountID, item.pendingAction)}
/>
</View>
{(props.session.accountID === item.accountID || item.role === 'admin') && (
<View style={[styles.badge, styles.peopleBadge]}>
<Text style={[styles.peopleBadgeText]}>{props.translate('common.admin')}</Text>
</View>
)}
</PressableWithFeedback>
{!_.isEmpty(errors[item.accountID]) && (
<FormHelpMessage
isError
message={errors[item.accountID]}
/>
)}
</OfflineWithFeedback>
);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[selectedEmployees, errors, props.session.accountID, dismissError, toggleUser],
);
const policyOwner = lodashGet(props.policy, 'owner');
const currentUserLogin = lodashGet(props.currentUserPersonalDetails, 'login');
const removableMembers = {};
let data = [];
_.each(props.policyMembers, (policyMember, accountID) => {
if (isDeletedPolicyMember(policyMember)) {
return;
}
const details = props.personalDetails[accountID];
if (!details) {
Log.hmmm(`[WorkspaceMembersPage] no personal details found for policy member with accountID: ${accountID}`);
return;
}
data.push({
...policyMember,
...details,
});
});
data = _.sortBy(data, (value) => value.displayName.toLowerCase());
data = getMemberOptions(data, searchValue.trim().toLowerCase());
// If this policy is owned by Expensify then show all support (expensify.com or team.expensify.com) emails
// We don't want to show guides as policy members unless the user is a guide. Some customers get confused when they
// see random people added to their policy, but guides having access to the policies help set them up.
if (policyOwner && currentUserLogin && !PolicyUtils.isExpensifyTeam(policyOwner) && !PolicyUtils.isExpensifyTeam(currentUserLogin)) {
data = _.reject(data, (member) => PolicyUtils.isExpensifyTeam(member.login || member.displayName));
}
_.each(data, (member) => {
if (member.accountID === props.session.accountID || member.login === props.policy.owner || member.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
return;
}
removableMembers[member.accountID] = member;
});
const policyID = lodashGet(props.route, 'params.policyID');
const policyName = lodashGet(props.policy, 'name');
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
style={[styles.defaultModalContainer]}
>
{({safeAreaPaddingBottomStyle}) => (
<FullPageNotFoundView
shouldShow={_.isEmpty(props.policy)}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WORKSPACES)}
>
<HeaderWithBackButton
title={props.translate('workspace.common.members')}
subtitle={policyName}
onBackButtonPress={() => {
setSearchValue('');
Navigation.goBack(ROUTES.getWorkspaceInitialRoute(policyID));
}}
shouldShowGetAssistanceButton
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_MEMBERS}
/>
<ConfirmModal
danger
title={props.translate('workspace.people.removeMembersTitle')}
isVisible={removeMembersConfirmModalVisible}
onConfirm={removeUsers}
onCancel={() => setRemoveMembersConfirmModalVisible(false)}
prompt={props.translate('workspace.people.removeMembersPrompt')}
confirmText={props.translate('common.remove')}
cancelText={props.translate('common.cancel')}
/>
<View style={[styles.w100, styles.flex1]}>
<View style={[styles.w100, styles.flexRow, styles.pt3, styles.ph5]}>
<Button
medium
success
text={props.translate('common.invite')}
onPress={inviteUser}
/>
<Button
medium
danger
style={[styles.ml2]}
isDisabled={selectedEmployees.length === 0}
text={props.translate('common.remove')}
onPress={askForConfirmationToRemove}
/>
</View>
<View style={[styles.w100, styles.pv3, styles.ph5]}>
<TextInput
value={searchValue}
onChangeText={setSearchValue}
label={props.translate('optionsSelector.findMember')}
/>
</View>
{data.length > 0 ? (
<View style={[styles.w100, styles.mt4, styles.flex1]}>
<View style={[styles.peopleRow, styles.ph5, styles.pb3]}>
<PressableWithFeedback
disabled={_.isEmpty(removableMembers)}
onPress={() => toggleAllUsers(removableMembers)}
accessibilityRole="checkbox"
accessibilityState={{
checked: !_.isEmpty(removableMembers) && _.every(_.keys(removableMembers), (accountID) => _.contains(selectedEmployees, Number(accountID))),
}}
accessibilityLabel={props.translate('workspace.people.selectAll')}
hoverDimmingValue={1}
pressDimmingValue={0.7}
>
<Checkbox
disabled={_.isEmpty(removableMembers)}
isChecked={!_.isEmpty(removableMembers) && _.every(_.keys(removableMembers), (accountID) => _.contains(selectedEmployees, Number(accountID)))}
onPress={() => toggleAllUsers(removableMembers)}
accessibilityLabel={props.translate('workspace.people.selectAll')}
/>
</PressableWithFeedback>
<View style={[styles.flex1]}>
<Text style={[styles.textStrong, styles.ph5]}>{props.translate('workspace.people.selectAll')}</Text>
</View>
</View>
<KeyboardDismissingFlatList
renderItem={renderItem}
data={data}
keyExtractor={(item) => item.login}
showsVerticalScrollIndicator
style={[styles.ph5, styles.pb5]}
contentContainerStyle={safeAreaPaddingBottomStyle}
keyboardShouldPersistTaps="handled"
/>
</View>
) : (
<View style={[styles.ph5]}>
<Text style={[styles.textLabel, styles.colorMuted]}>{props.translate('workspace.common.memberNotFound')}</Text>
</View>
)}
</View>
</FullPageNotFoundView>
)}
</ScreenWrapper>
);
}
WorkspaceMembersPage.propTypes = propTypes;
WorkspaceMembersPage.defaultProps = defaultProps;
WorkspaceMembersPage.displayName = 'WorkspaceMembersPage';
export default compose(
withLocalize,
withWindowDimensions,
withPolicyAndFullscreenLoading,
withNetwork(),
withOnyx({
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
}),
withCurrentUserPersonalDetails,
)(WorkspaceMembersPage);