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
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
const AsyncClient = require('../utils/async_client.jsx');
const ChannelStore = require('../stores/channel_store.jsx');
const Constants = require('../utils/constants.jsx');
const Client = require('../utils/client.jsx');
const Modal = ReactBootstrap.Modal;
const PreferenceStore = require('../stores/preference_store.jsx');
const TeamStore = require('../stores/team_store.jsx');
const UserStore = require('../stores/user_store.jsx');
const Utils = require('../utils/utils.jsx');
export default class MoreDirectChannels extends React.Component {
constructor(props) {
super(props);
this.handleFilterChange = this.handleFilterChange.bind(this);
this.handleHide = this.handleHide.bind(this);
this.handleShowDirectChannel = this.handleShowDirectChannel.bind(this);
this.handleUserChange = this.handleUserChange.bind(this);
this.createRowForUser = this.createRowForUser.bind(this);
this.state = {
users: this.getUsersFromStore(),
filter: '',
loadingDMChannel: -1
};
}
getUsersFromStore() {
const currentId = UserStore.getCurrentId();
const profiles = UserStore.getProfiles();
const users = [];
for (const id in profiles) {
if (id !== currentId) {
users.push(profiles[id]);
}
}
users.sort((a, b) => a.username.localeCompare(b.username));
return users;
}
componentDidMount() {
UserStore.addChangeListener(this.handleUserChange);
}
componentWillUnmount() {
UserStore.addChangeListener(this.handleUserChange);
}
handleFilterChange() {
const filter = ReactDOM.findDOMNode(this.refs.filter).value;
if (filter !== this.state.filter) {
this.setState({filter});
}
}
handleHide() {
if (this.props.onModalDismissed) {
this.props.onModalDismissed();
}
this.setState({filter: ''});
}
handleShowDirectChannel(teammate, e) {
if (this.state.loadingDMChannel !== -1) {
return;
}
e.preventDefault();
const channelName = Utils.getDirectChannelName(UserStore.getCurrentId(), teammate.id);
let channel = ChannelStore.getByName(channelName);
const preference = PreferenceStore.setPreference(Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, teammate.id, 'true');
AsyncClient.savePreferences([preference]);
if (channel) {
Utils.switchChannel(channel);
this.handleHide();
} else {
this.setState({loadingDMChannel: teammate.id});
channel = {
name: channelName,
last_post_at: 0,
total_msg_count: 0,
type: 'D',
display_name: teammate.username,
teammate_id: teammate.id,
status: UserStore.getStatus(teammate.id)
};
Client.createDirectChannel(
channel,
teammate.id,
(data) => {
this.setState({loadingDMChannel: -1});
AsyncClient.getChannel(data.id);
Utils.switchChannel(data);
this.handleHide();
},
() => {
this.setState({loadingDMChannel: -1});
window.location.href = TeamStore.getCurrentTeamUrl() + '/channels/' + channelName;
}
);
}
}
handleUserChange() {
this.setState({users: this.getUsersFromStore()});
}
createRowForUser(user) {
const details = [];
const fullName = Utils.getFullName(user);
if (fullName) {
details.push(
<span
key={`${user.id}__full-name`}
className='full-name'
>
{fullName}
</span>
);
}
if (user.nickname) {
const separator = fullName ? ' - ' : '';
details.push(
<span
key={`${user.nickname}__nickname`}
>
{separator + user.nickname}
</span>
);
}
let joinButton;
if (this.state.loadingDMChannel === user.id) {
joinButton = (
<img
className='channel-loading-gif'
src='/static/images/load.gif'
/>
);
} else {
joinButton = (
<button
type='button'
className='btn btn-primary btn-message'
onClick={this.handleShowDirectChannel.bind(this, user)}
>
{'Message'}
</button>
);
}
return (
<tr>
<td
key={user.id}
className='direct-channel'
>
<img
className='profile-img pull-left'
width='38'
height='38'
src={`/api/v1/users/${user.id}/image?time=${user.update_at}&${Utils.getSessionIndex()}`}
/>
<div className='more-name'>
{user.username}
</div>
<div className='more-description'>
{details}
</div>
</td>
<td className='td--action lg'>
{joinButton}
</td>
</tr>
);
}
componentDidUpdate(prevProps) {
if (!prevProps.show && this.props.show) {
$(ReactDOM.findDOMNode(this.refs.userList)).css('max-height', $(window).height() - 300);
if ($(window).width() > 768) {
$(ReactDOM.findDOMNode(this.refs.userList)).perfectScrollbar();
}
}
}
render() {
if (!this.props.show) {
return null;
}
let users = this.state.users;
if (this.state.filter !== '') {
users = users.filter((user) => {
return user.username.indexOf(this.state.filter) !== -1 ||
user.first_name.indexOf(this.state.filter) !== -1 ||
user.last_name.indexOf(this.state.filter) !== -1 ||
user.nickname.indexOf(this.state.filter) !== -1;
});
}
const userEntries = users.map(this.createRowForUser);
if (userEntries.length === 0) {
userEntries.push(<tr key='no-users-found'><td>{'No users found :('}</td></tr>);
}
let memberString = 'Member';
if (users.length !== 1) {
memberString += 's';
}
let count;
if (users.length === this.state.users.length) {
count = `${users.length} ${memberString}`;
} else {
count = `${users.length} ${memberString} of ${this.state.users.length} Total`;
}
return (
<Modal
className='modal-direct-channels'
show={this.props.show}
onHide={this.handleHide}
>
<Modal.Header closeButton={true}>
<Modal.Title>{'Direct Messages'}</Modal.Title>
</Modal.Header>
<Modal.Body>
<div className='row filter-row'>
<div className='col-sm-6'>
<input
ref='filter'
className='form-control filter-textbox'
placeholder='Search members'
onInput={this.handleFilterChange}
/>
</div>
<div className='col-sm-6'>
<span className='member-count'>{count}</span>
</div>
</div>
<div
ref='userList'
className='user-list'
>
<table className='more-table table'>
<tbody>
{userEntries}
</tbody>
</table>
</div>
</Modal.Body>
<Modal.Footer>
<button
type='button'
className='btn btn-default'
onClick={this.handleHide}
>
{'Close'}
</button>
</Modal.Footer>
</Modal>
);
}
}
MoreDirectChannels.propTypes = {
show: React.PropTypes.bool.isRequired,
onModalDismissed: React.PropTypes.func
};
|