-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
RoomMembersPage.js
319 lines (290 loc) · 12.3 KB
/
RoomMembersPage.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
import PropTypes from 'prop-types';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import Button from '@components/Button';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import {usePersonalDetails} from '@components/OnyxProvider';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsDefaultProps, withCurrentUserPersonalDetailsPropTypes} from '@components/withCurrentUserPersonalDetails';
import withLocalize, {withLocalizePropTypes} from '@components/withLocalize';
import withWindowDimensions, {windowDimensionsPropTypes} from '@components/withWindowDimensions';
import useThemeStyles from '@hooks/useThemeStyles';
import compose from '@libs/compose';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as UserUtils from '@libs/UserUtils';
import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import withReportOrNotFound from './home/report/withReportOrNotFound';
import reportPropTypes from './reportPropTypes';
const propTypes = {
/** The report currently being looked at */
report: reportPropTypes.isRequired,
/** The policies which the user has access to and which the report could be tied to */
policies: PropTypes.shape({
/** ID of the policy */
id: PropTypes.string,
}),
/** URL Route params */
route: PropTypes.shape({
/** Params from the URL path */
params: PropTypes.shape({
/** reportID passed via route: /workspace/:reportID/members */
reportID: PropTypes.string,
}),
}).isRequired,
/** Session info for the currently logged in user. */
session: PropTypes.shape({
/** Currently logged in user accountID */
accountID: PropTypes.number,
}),
...withLocalizePropTypes,
...windowDimensionsPropTypes,
...withCurrentUserPersonalDetailsPropTypes,
};
const defaultProps = {
session: {
accountID: 0,
},
report: {},
policies: {},
...withCurrentUserPersonalDetailsDefaultProps,
};
function RoomMembersPage(props) {
const styles = useThemeStyles();
const [selectedMembers, setSelectedMembers] = useState([]);
const [removeMembersConfirmModalVisible, setRemoveMembersConfirmModalVisible] = useState(false);
const [searchValue, setSearchValue] = useState('');
const [didLoadRoomMembers, setDidLoadRoomMembers] = useState(false);
const personalDetails = usePersonalDetails() || CONST.EMPTY_OBJECT;
/**
* Get members for the current room
*/
const getRoomMembers = useCallback(() => {
Report.openRoomMembersPage(props.report.reportID);
setDidLoadRoomMembers(true);
}, [props.report.reportID]);
useEffect(() => {
getRoomMembers();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
/**
* Open the modal to invite a user
*/
const inviteUser = () => {
setSearchValue('');
Navigation.navigate(ROUTES.ROOM_INVITE.getRoute(props.report.reportID));
};
/**
* Remove selected users from the room
*/
const removeUsers = () => {
Report.removeFromRoom(props.report.reportID, selectedMembers);
setSelectedMembers([]);
setRemoveMembersConfirmModalVisible(false);
};
/**
* Add user from the selectedMembers list
*
* @param {String} login
*/
const addUser = useCallback((accountID) => {
setSelectedMembers((prevSelected) => [...prevSelected, accountID]);
}, []);
/**
* Remove user from the selectedEmployees list
*
* @param {String} login
*/
const removeUser = useCallback((accountID) => {
setSelectedMembers((prevSelected) => _.without(prevSelected, accountID));
}, []);
/**
* Toggle user from the selectedMembers 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(selectedMembers, Number(accountID))) {
removeUser(Number(accountID));
} else {
addUser(Number(accountID));
}
},
[selectedMembers, addUser, removeUser],
);
/**
* Add or remove all users passed from the selectedMembers list
* @param {Object} memberList
*/
const toggleAllUsers = (memberList) => {
const enabledAccounts = _.filter(memberList, (member) => !member.isDisabled);
const everyoneSelected = _.every(enabledAccounts, (member) => _.contains(selectedMembers, Number(member.keyForList)));
if (everyoneSelected) {
setSelectedMembers([]);
} else {
const everyAccountId = _.map(enabledAccounts, (member) => Number(member.keyForList));
setSelectedMembers(everyAccountId);
}
};
/**
* Show the modal to confirm removal of the selected members
*/
const askForConfirmationToRemove = () => {
setRemoveMembersConfirmModalVisible(true);
};
const getMemberOptions = () => {
let result = [];
_.each(props.report.visibleChatMemberAccountIDs, (accountID) => {
const details = personalDetails[accountID];
if (!details) {
Log.hmmm(`[RoomMembersPage] no personal details found for room 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 += ` ${PersonalDetailsUtils.getDisplayNameOrDefault(details).toLowerCase()}`;
}
if (details.phoneNumber) {
memberDetails += ` ${details.phoneNumber.toLowerCase()}`;
}
if (!OptionsListUtils.isSearchStringMatch(searchValue.trim(), memberDetails)) {
return;
}
}
result.push({
keyForList: String(accountID),
accountID: Number(accountID),
isSelected: _.contains(selectedMembers, Number(accountID)),
isDisabled: accountID === props.session.accountID,
text: props.formatPhoneNumber(PersonalDetailsUtils.getDisplayNameOrDefault(details)),
alternateText: props.formatPhoneNumber(details.login),
icons: [
{
source: UserUtils.getAvatar(details.avatar, accountID),
name: details.login,
type: CONST.ICON_TYPE_AVATAR,
},
],
});
});
result = _.sortBy(result, (value) => value.text.toLowerCase());
return result;
};
const isPolicyMember = useMemo(() => PolicyUtils.isPolicyMember(props.report.policyID, props.policies), [props.report.policyID, props.policies]);
const data = getMemberOptions();
const headerMessage = searchValue.trim() && !data.length ? props.translate('roomMembersPage.memberNotFound') : '';
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
style={[styles.defaultModalContainer]}
testID={RoomMembersPage.displayName}
>
<FullPageNotFoundView
shouldShow={_.isEmpty(props.report) || !isPolicyMember}
subtitleKey={_.isEmpty(props.report) ? undefined : 'roomMembersPage.notAuthorized'}
onBackButtonPress={() => Navigation.goBack(ROUTES.REPORT_WITH_ID_DETAILS.getRoute(props.report.reportID))}
>
<HeaderWithBackButton
title={props.translate('workspace.common.members')}
subtitle={ReportUtils.getReportName(props.report)}
onBackButtonPress={() => {
setSearchValue('');
Navigation.goBack(ROUTES.REPORT_WITH_ID_DETAILS.getRoute(props.report.reportID));
}}
/>
<ConfirmModal
danger
title={props.translate('workspace.people.removeMembersTitle')}
isVisible={removeMembersConfirmModalVisible}
onConfirm={removeUsers}
onCancel={() => setRemoveMembersConfirmModalVisible(false)}
prompt={props.translate('roomMembersPage.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={selectedMembers.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')}
disableKeyboardShortcuts={removeMembersConfirmModalVisible}
textInputValue={searchValue}
onChangeText={setSearchValue}
headerMessage={headerMessage}
onSelectRow={(item) => toggleUser(item.keyForList)}
onSelectAll={() => toggleAllUsers(data)}
showLoadingPlaceholder={!OptionsListUtils.isPersonalDetailsReady(personalDetails) || !didLoadRoomMembers}
showScrollIndicator
shouldPreventDefaultFocusOnSelectRow={!DeviceCapabilities.canUseTouchScreen()}
/>
</View>
</View>
</FullPageNotFoundView>
</ScreenWrapper>
);
}
RoomMembersPage.propTypes = propTypes;
RoomMembersPage.defaultProps = defaultProps;
RoomMembersPage.displayName = 'RoomMembersPage';
export default compose(
withLocalize,
withWindowDimensions,
withReportOrNotFound(),
withOnyx({
session: {
key: ONYXKEYS.SESSION,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
}),
withCurrentUserPersonalDetails,
)(RoomMembersPage);