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
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
var utils = require('../utils/utils.jsx');
var ActionTypes = require('../utils/constants.jsx').ActionTypes;
var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var Client = require('../utils/client.jsx');
var ModalStore = require('../stores/modal_store.jsx');
var UserStore = require('../stores/user_store.jsx');
var TeamStore = require('../stores/team_store.jsx');
var ConfirmModal = require('./confirm_modal.jsx');
const Modal = ReactBootstrap.Modal;
export default class InviteMemberModal extends React.Component {
constructor(props) {
super(props);
this.handleToggle = this.handleToggle.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleHide = this.handleHide.bind(this);
this.addInviteFields = this.addInviteFields.bind(this);
this.clearFields = this.clearFields.bind(this);
this.removeInviteFields = this.removeInviteFields.bind(this);
this.state = {
show: false,
inviteIds: [0],
idCount: 0,
emailErrors: {},
firstNameErrors: {},
lastNameErrors: {},
emailEnabled: global.window.mm_config.SendEmailNotifications === 'true',
showConfirmModal: false,
isSendingEmails: false
};
}
componentDidMount() {
ModalStore.addModalListener(ActionTypes.TOGGLE_INVITE_MEMBER_MODAL, this.handleToggle);
}
componentWillUnmount() {
ModalStore.removeModalListener(ActionTypes.TOGGLE_INVITE_MEMBER_MODAL, this.handleToggle);
}
handleToggle(value) {
this.setState({
show: value
});
}
handleSubmit() {
if (!this.state.emailEnabled) {
return;
}
var inviteIds = this.state.inviteIds;
var count = inviteIds.length;
var invites = [];
var emailErrors = this.state.emailErrors;
var firstNameErrors = this.state.firstNameErrors;
var lastNameErrors = this.state.lastNameErrors;
var valid = true;
for (var i = 0; i < count; i++) {
var index = inviteIds[i];
var invite = {};
invite.email = ReactDOM.findDOMNode(this.refs['email' + index]).value.trim();
if (!invite.email || !utils.isEmail(invite.email)) {
emailErrors[index] = 'Please enter a valid email address';
valid = false;
} else {
emailErrors[index] = '';
}
invite.firstName = ReactDOM.findDOMNode(this.refs['first_name' + index]).value.trim();
invite.lastName = ReactDOM.findDOMNode(this.refs['last_name' + index]).value.trim();
invites.push(invite);
}
this.setState({emailErrors: emailErrors, firstNameErrors: firstNameErrors, lastNameErrors: lastNameErrors});
if (!valid || invites.length === 0) {
return;
}
var data = {};
data.invites = invites;
this.setState({isSendingEmails: true});
Client.inviteMembers(
data,
() => {
this.handleHide(false);
this.setState({isSendingEmails: false});
},
(err) => {
if (err.message === 'This person is already on your team') {
emailErrors[err.detailed_error] = err.message;
this.setState({emailErrors: emailErrors});
} else {
this.setState({serverError: err.message});
}
this.setState({isSendingEmails: false});
}
);
}
handleHide(requireConfirm) {
if (requireConfirm) {
var notEmpty = false;
for (var i = 0; i < this.state.inviteIds.length; i++) {
var index = this.state.inviteIds[i];
if (ReactDOM.findDOMNode(this.refs['email' + index]).value.trim() !== '') {
notEmpty = true;
break;
}
}
if (notEmpty) {
this.setState({
showConfirmModal: true
});
return;
}
}
this.clearFields();
this.setState({
show: false,
showConfirmModal: false
});
}
componentDidUpdate(prevProps, prevState) {
if (!prevState.show && this.state.show) {
$(ReactDOM.findDOMNode(this.refs.modalBody)).css('max-height', $(window).height() - 300);
if ($(window).width() > 768) {
$(ReactDOM.findDOMNode(this.refs.modalBody)).perfectScrollbar();
}
}
}
addInviteFields() {
var count = this.state.idCount + 1;
var inviteIds = this.state.inviteIds;
inviteIds.push(count);
this.setState({inviteIds: inviteIds, idCount: count});
}
clearFields() {
var inviteIds = this.state.inviteIds;
for (var i = 0; i < inviteIds.length; i++) {
var index = inviteIds[i];
ReactDOM.findDOMNode(this.refs['email' + index]).value = '';
ReactDOM.findDOMNode(this.refs['first_name' + index]).value = '';
ReactDOM.findDOMNode(this.refs['last_name' + index]).value = '';
}
this.setState({
inviteIds: [0],
idCount: 0,
emailErrors: {},
firstNameErrors: {},
lastNameErrors: {}
});
}
removeInviteFields(index) {
var count = this.state.idCount;
var inviteIds = this.state.inviteIds;
var i = inviteIds.indexOf(index);
if (i > -1) {
inviteIds.splice(i, 1);
}
if (!inviteIds.length) {
inviteIds.push(++count);
}
this.setState({inviteIds: inviteIds, idCount: count});
}
render() {
var currentUser = UserStore.getCurrentUser();
if (currentUser != null) {
var inviteSections = [];
var inviteIds = this.state.inviteIds;
for (var i = 0; i < inviteIds.length; i++) {
var index = inviteIds[i];
var emailError = null;
if (this.state.emailErrors[index]) {
emailError = <label className='control-label'>{this.state.emailErrors[index]}</label>;
}
var firstNameError = null;
if (this.state.firstNameErrors[index]) {
firstNameError = <label className='control-label'>{this.state.firstNameErrors[index]}</label>;
}
var lastNameError = null;
if (this.state.lastNameErrors[index]) {
lastNameError = <label className='control-label'>{this.state.lastNameErrors[index]}</label>;
}
var removeButton = null;
if (index) {
removeButton = (<div>
<button
type='button'
className='btn btn-link remove__member'
onClick={this.removeInviteFields.bind(this, index)}
>
<span className='fa fa-trash'></span>
</button>
</div>);
}
var emailClass = 'form-group invite';
if (emailError) {
emailClass += ' has-error';
}
var nameFields = null;
var firstNameClass = 'form-group';
if (firstNameError) {
firstNameClass += ' has-error';
}
var lastNameClass = 'form-group';
if (lastNameError) {
lastNameClass += ' has-error';
}
nameFields = (<div className='row--invite'>
<div className='col-sm-6'>
<div className={firstNameClass}>
<input
type='text'
className='form-control'
ref={'first_name' + index}
placeholder='First name'
maxLength='64'
disabled={!this.state.emailEnabled}
spellCheck='false'
/>
{firstNameError}
</div>
</div>
<div className='col-sm-6'>
<div className={lastNameClass}>
<input
type='text'
className='form-control'
ref={'last_name' + index}
placeholder='Last name'
maxLength='64'
disabled={!this.state.emailEnabled}
spellCheck='false'
/>
{lastNameError}
</div>
</div>
</div>);
inviteSections[index] = (
<div key={'key' + index}>
{removeButton}
<div className={emailClass}>
<input
onKeyUp={this.displayNameKeyUp}
type='text'
ref={'email' + index}
className='form-control'
placeholder='email@domain.com'
maxLength='64'
disabled={!this.state.emailEnabled}
spellCheck='false'
/>
{emailError}
</div>
{nameFields}
</div>
);
}
var serverError = null;
if (this.state.serverError) {
serverError = <div className='form-group has-error'><label className='control-label'>{this.state.serverError}</label></div>;
}
var content = null;
var sendButton = null;
if (this.state.emailEnabled) {
content = (
<div>
{serverError}
<button
type='button'
className='btn btn-default'
onClick={this.addInviteFields}
>Add another</button>
<br/>
<br/>
<span>People invited automatically join Town Square channel.</span>
</div>
);
var sendButtonLabel = 'Send Invitation';
if (this.state.isSendingEmails) {
sendButtonLabel = (
<span><i className='fa fa-spinner fa-spin' />{' Sending'}</span>
);
} else if (this.state.inviteIds.length > 1) {
sendButtonLabel = 'Send Invitations';
}
sendButton = (
<button
onClick={this.handleSubmit}
type='button'
className='btn btn-primary'
disabled={this.state.isSendingEmails}
>
{sendButtonLabel}
</button>
);
} else {
var teamInviteLink = null;
if (currentUser && TeamStore.getCurrent().type === 'O') {
var linkUrl = utils.getWindowLocationOrigin() + '/signup_user_complete/?id=' + TeamStore.getCurrent().invite_id;
var link =
(
<a
href='#'
data-toggle='modal'
data-target='#get_link'
data-title='Team Invite'
data-value={linkUrl}
onClick={() => this.handleHide(this, false)}
>Team Invite Link</a>
);
teamInviteLink = (
<p>
You can also invite people using the {link}.
</p>
);
}
content = (
<div>
<p>Email is currently disabled for your team, and email invitations cannot be sent. Contact your system administrator to enable email and email invitations.</p>
{teamInviteLink}
</div>
);
}
return (
<div>
<Modal
dialogClassName='modal-invite-member'
show={this.state.show}
onHide={this.handleHide.bind(this, true)}
enforceFocus={!this.state.showConfirmModal}
backdrop={this.state.isSendingEmails ? 'static' : true}
>
<Modal.Header closeButton={!this.state.isSendingEmails}>
<Modal.Title>{'Invite New Member'}</Modal.Title>
</Modal.Header>
<Modal.Body ref='modalBody'>
<form role='form'>
{inviteSections}
</form>
{content}
</Modal.Body>
<Modal.Footer>
<button
type='button'
className='btn btn-default'
onClick={this.handleHide.bind(this, true)}
disabled={this.state.isSendingEmails}
>
{'Cancel'}
</button>
{sendButton}
</Modal.Footer>
</Modal>
<ConfirmModal
title='Discard Invitations?'
message='You have unsent invitations, are you sure you want to discard them?'
confirm_button='Yes, Discard'
show={this.state.showConfirmModal}
onConfirm={this.handleHide.bind(this, false)}
onCancel={() => this.setState({showConfirmModal: false})}
/>
</div>
);
}
return null;
}
static show() {
AppDispatcher.handleViewAction({
type: ActionTypes.TOGGLE_INVITE_MEMBER_MODAL,
value: true
});
}
}
InviteMemberModal.propTypes = {
};
|