This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
channel_invite_modal.jsx
255 lines (223 loc) · 8.03 KB
/
channel_invite_modal.jsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import PropTypes from 'prop-types';
import React from 'react';
import {Modal} from 'react-bootstrap';
import {FormattedMessage} from 'react-intl';
import {Client4} from 'mattermost-redux/client';
import {filterProfilesMatchingTerm} from 'mattermost-redux/utils/user_utils';
import {displayEntireNameForUser, localizeMessage} from 'utils/utils.jsx';
import ProfilePicture from 'components/profile_picture.jsx';
import MultiSelect from 'components/multiselect/multiselect.jsx';
import AddIcon from 'components/icon/add_icon';
import {searchUsers} from 'actions/user_actions.jsx';
import Constants from 'utils/constants.jsx';
const USERS_PER_PAGE = 50;
const MAX_SELECTABLE_VALUES = 20;
export default class ChannelInviteModal extends React.Component {
static propTypes = {
profilesNotInCurrentChannel: PropTypes.array.isRequired,
onHide: PropTypes.func.isRequired,
channel: PropTypes.object.isRequired,
actions: PropTypes.shape({
addUsersToChannel: PropTypes.func.isRequired,
getProfilesNotInChannel: PropTypes.func.isRequired,
getTeamStats: PropTypes.func.isRequired,
}).isRequired,
};
constructor(props) {
super(props);
this.searchTimeoutId = 0;
this.state = {
values: [],
term: '',
show: true,
saving: false,
loadingUsers: true,
};
}
addValue = (value) => {
const values = Object.assign([], this.state.values);
if (values.indexOf(value) === -1) {
values.push(value);
}
this.setState({values});
};
componentDidMount() {
this.props.actions.getProfilesNotInChannel(this.props.channel.team_id, this.props.channel.id, 0).then(() => {
this.setUsersLoadingState(false);
});
this.props.actions.getTeamStats(this.props.channel.team_id);
}
onHide = () => {
this.setState({show: false});
};
handleInviteError = (err) => {
if (err) {
this.setState({
saving: false,
inviteError: err.message,
});
}
};
handleDelete = (values) => {
this.setState({values});
};
setUsersLoadingState = (loadingState) => {
this.setState({
loadingUsers: loadingState,
});
};
handlePageChange = (page, prevPage) => {
if (page > prevPage) {
this.setUsersLoadingState(true);
this.props.actions.getProfilesNotInChannel(this.props.channel.team_id, this.props.channel.id, page + 1, USERS_PER_PAGE).then(() => {
this.setUsersLoadingState(false);
});
}
};
handleSubmit = (e) => {
const {actions, channel} = this.props;
if (e) {
e.preventDefault();
}
const userIds = this.state.values.map((v) => v.id);
if (userIds.length === 0) {
return;
}
this.setState({saving: true});
actions.addUsersToChannel(channel.id, userIds).then((result) => {
if (result.error) {
this.handleInviteError(result.error);
} else {
this.setState({
saving: false,
inviteError: null,
});
this.onHide();
}
});
};
search = (term) => {
clearTimeout(this.searchTimeoutId);
this.setState({
term,
});
this.searchTimeoutId = setTimeout(
() => {
this.setUsersLoadingState(true);
searchUsers(term, this.props.channel.team_id, {not_in_channel_id: this.props.channel.id}).then(() => {
this.setUsersLoadingState(false);
});
},
Constants.SEARCH_TIMEOUT_MILLISECONDS
);
};
renderOption = (option, isSelected, onAdd) => {
var rowSelected = '';
if (isSelected) {
rowSelected = 'more-modal__row--selected';
}
let tag = null;
if (option.is_bot) {
tag = (
<div className='bot-indicator bot-indicator__popoverlist'>
<FormattedMessage
id='post_info.bot'
defaultMessage='BOT'
/>
</div>
);
}
return (
<div
key={option.id}
ref={isSelected ? 'selected' : option.id}
className={'more-modal__row clickable ' + rowSelected}
onClick={() => onAdd(option)}
>
<ProfilePicture
src={Client4.getProfilePictureUrl(option.id, option.last_picture_update)}
width='32'
height='32'
/>
<div className='more-modal__details'>
<div className='more-modal__name'>
{displayEntireNameForUser(option)}
{tag}
</div>
</div>
<div className='more-modal__actions'>
<div className='more-modal__actions--round'>
<AddIcon/>
</div>
</div>
</div>
);
};
renderValue(props) {
return props.data.username;
}
render() {
let inviteError = null;
if (this.state.inviteError) {
inviteError = (<label className='has-error control-label'>{this.state.inviteError}</label>);
}
const numRemainingText = (
<FormattedMessage
id='multiselect.numPeopleRemaining'
defaultMessage='Use ↑↓ to browse, ↵ to select. You can add {num, number} more {num, plural, one {person} other {people}}. '
values={{
num: MAX_SELECTABLE_VALUES - this.state.values.length,
}}
/>
);
const buttonSubmitText = localizeMessage('multiselect.add', 'Add');
const buttonSubmitLoadingText = localizeMessage('multiselect.adding', 'Adding...');
let users = filterProfilesMatchingTerm(this.props.profilesNotInCurrentChannel, this.state.term);
users = users.filter((user) => user.delete_at === 0);
const content = (
<MultiSelect
key='addUsersToChannelKey'
options={users}
optionRenderer={this.renderOption}
values={this.state.values}
valueRenderer={this.renderValue}
perPage={USERS_PER_PAGE}
handlePageChange={this.handlePageChange}
handleInput={this.search}
handleDelete={this.handleDelete}
handleAdd={this.addValue}
handleSubmit={this.handleSubmit}
maxValues={MAX_SELECTABLE_VALUES}
numRemainingText={numRemainingText}
buttonSubmitText={buttonSubmitText}
buttonSubmitLoadingText={buttonSubmitLoadingText}
saving={this.state.saving}
loading={this.state.loadingUsers}
/>
);
return (
<Modal
dialogClassName='more-modal'
show={this.state.show}
onHide={this.onHide}
onExited={this.props.onHide}
>
<Modal.Header closeButton={true}>
<Modal.Title>
<FormattedMessage
id='channel_invite.addNewMembers'
defaultMessage='Add New Members to '
/>
<span className='name'>{this.props.channel.display_name}</span>
</Modal.Title>
</Modal.Header>
<Modal.Body>
{inviteError}
{content}
</Modal.Body>
</Modal>
);
}
}