-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
WorkspaceMembersPage.js
433 lines (392 loc) · 17.1 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
import React, {useCallback, useEffect, useState, useMemo} 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 ROUTES from '../../ROUTES';
import ConfirmModal from '../../components/ConfirmModal';
import personalDetailsPropType from '../personalDetailsPropType';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../components/withWindowDimensions';
import withPolicy, {policyDefaultProps, policyPropTypes} from './withPolicy';
import CONST from '../../CONST';
import {withNetwork} from '../../components/OnyxProvider';
import FullPageNotFoundView from '../../components/BlockingViews/FullPageNotFoundView';
import networkPropTypes from '../../components/networkPropTypes';
import * as UserUtils from '../../libs/UserUtils';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsDefaultProps, withCurrentUserPersonalDetailsPropTypes} from '../../components/withCurrentUserPersonalDetails';
import * as PolicyUtils from '../../libs/PolicyUtils';
import usePrevious from '../../hooks/usePrevious';
import Log from '../../libs/Log';
import * as PersonalDetailsUtils from '../../libs/PersonalDetailsUtils';
import SelectionList from '../../components/SelectionList';
import Text from '../../components/Text';
const propTypes = {
/** All personal details asssociated with user */
personalDetails: PropTypes.objectOf(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,
}),
isLoadingReportData: PropTypes.bool,
...policyPropTypes,
...withLocalizePropTypes,
...windowDimensionsPropTypes,
...withCurrentUserPersonalDetailsPropTypes,
network: networkPropTypes.isRequired,
};
const defaultProps = {
personalDetails: {},
session: {
accountID: 0,
},
isLoadingReportData: true,
...policyDefaultProps,
...withCurrentUserPersonalDetailsDefaultProps,
};
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);
const accountIDs = useMemo(() => _.keys(props.policyMembers), [props.policyMembers]);
const prevAccountIDs = usePrevious(accountIDs);
/**
* Get members for the current workspace
*/
const getWorkspaceMembers = useCallback(() => {
Policy.openWorkspaceMembersPage(props.route.params.policyID, _.keys(PolicyUtils.getMemberAccountIDsForWorkspace(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 ? [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(() => {
if (removeMembersConfirmModalVisible && !_.isEqual(accountIDs, prevAccountIDs)) {
setRemoveMembersConfirmModalVisible(false);
}
setSelectedEmployees((prevSelected) =>
_.intersection(
prevSelected,
_.map(_.values(PolicyUtils.getMemberAccountIDsForWorkspace(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]);
/**
* 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 enabledAccounts = _.filter(memberList, (member) => !member.isDisabled);
const everyoneSelected = _.every(enabledAccounts, (member) => _.contains(selectedEmployees, Number(member.keyForList)));
if (everyoneSelected) {
setSelectedEmployees([]);
} else {
const everyAccountId = _.map(enabledAccounts, (member) => Number(member.keyForList));
setSelectedEmployees(everyAccountId);
}
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(Number(accountID));
} else {
addUser(Number(accountID));
}
},
[selectedEmployees, addUser, removeUser],
);
/**
* Dismisses the errors on one item
*
* @param {Object} item
*/
const dismissError = useCallback(
(item) => {
if (item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.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 === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && _.isEmpty(policyMember.errors);
const policyOwner = lodashGet(props.policy, 'owner');
const currentUserLogin = lodashGet(props.currentUserPersonalDetails, 'login');
const policyID = lodashGet(props.route, 'params.policyID');
const policyName = lodashGet(props.policy, 'name');
const getMemberOptions = () => {
let result = [];
_.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;
}
// If search value is provided, filter out members that don't match the search value
if (searchValue.trim()) {
let memberDetails = '';
if (details.login) {
memberDetails += ` ${details.login.toLowerCase()}`;
}
if (details.firstName) {
memberDetails += ` ${details.firstName.toLowerCase()}`;
}
if (details.lastName) {
memberDetails += ` ${details.lastName.toLowerCase()}`;
}
if (details.displayName) {
memberDetails += ` ${details.displayName.toLowerCase()}`;
}
if (details.phoneNumber) {
memberDetails += ` ${details.phoneNumber.toLowerCase()}`;
}
if (!OptionsListUtils.isSearchStringMatch(searchValue.trim(), memberDetails)) {
return;
}
}
// 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 (PolicyUtils.isExpensifyTeam(details.login || details.displayName)) {
if (policyOwner && currentUserLogin && !PolicyUtils.isExpensifyTeam(policyOwner) && !PolicyUtils.isExpensifyTeam(currentUserLogin)) {
return;
}
}
const isAdmin = props.session.email === details.login || policyMember.role === CONST.POLICY.ROLE.ADMIN;
result.push({
keyForList: accountID,
accountID: Number(accountID),
isSelected: _.contains(selectedEmployees, Number(accountID)),
isDisabled: accountID === props.session.accountID || details.login === props.policy.owner || policyMember.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
text: props.formatPhoneNumber(details.displayName),
alternateText: props.formatPhoneNumber(details.login),
rightElement: isAdmin ? (
<View style={[styles.badge, styles.peopleBadge]}>
<Text style={styles.peopleBadgeText}>{props.translate('common.admin')}</Text>
</View>
) : null,
avatar: {
source: UserUtils.getAvatar(details.avatar, accountID),
name: details.login,
type: CONST.ICON_TYPE_AVATAR,
},
errors: policyMember.errors,
pendingAction: policyMember.pendingAction,
});
});
result = _.sortBy(result, (value) => value.text.toLowerCase());
return result;
};
const data = getMemberOptions();
const headerMessage = searchValue.trim() && !data.length ? props.translate('workspace.common.memberNotFound') : '';
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
style={[styles.defaultModalContainer]}
testID={WorkspaceMembersPage.displayName}
>
<FullPageNotFoundView
shouldShow={((_.isEmpty(props.policy) || !PolicyUtils.isPolicyAdmin(props.policy)) && !props.isLoadingReportData) || PolicyUtils.isPendingDeletePolicy(props.policy)}
subtitleKey={_.isEmpty(props.policy) ? undefined : 'workspace.common.notAuthorized'}
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.mt4, styles.flex1]}>
<SelectionList
canSelectMultiple
sections={[{data, indexOffset: 0, isDisabled: false}]}
textInputLabel={props.translate('optionsSelector.findMember')}
textInputValue={searchValue}
onChangeText={setSearchValue}
headerMessage={headerMessage}
onSelectRow={(item) => toggleUser(item.keyForList)}
onSelectAll={() => toggleAllUsers(data)}
onDismissError={dismissError}
showLoadingPlaceholder={!OptionsListUtils.isPersonalDetailsReady(props.personalDetails) || _.isEmpty(props.policyMembers)}
showScrollIndicator
/>
</View>
</View>
</FullPageNotFoundView>
</ScreenWrapper>
);
}
WorkspaceMembersPage.propTypes = propTypes;
WorkspaceMembersPage.defaultProps = defaultProps;
WorkspaceMembersPage.displayName = 'WorkspaceMembersPage';
export default compose(
withLocalize,
withWindowDimensions,
withPolicy,
withNetwork(),
withOnyx({
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
isLoadingReportData: {
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
},
}),
withCurrentUserPersonalDetails,
)(WorkspaceMembersPage);