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
|
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
var utils = require('../utils/utils.jsx');
var Client =require('../utils/client.jsx');
var UserStore = require('../stores/user_store.jsx');
var ConfirmModal = require('./confirm_modal.jsx');
module.exports = React.createClass({
componentDidMount: function() {
var self = this;
$('#invite_member').on('hide.bs.modal', function(e) {
if ($('#invite_member').attr('data-confirm') === 'true') {
$('#invite_member').attr('data-confirm', 'false');
return;
}
var not_empty = false;
for (var i = 0; i < self.state.invite_ids.length; i++) {
var index = self.state.invite_ids[i];
if (self.refs["email"+index].getDOMNode().value.trim() !== '') {
not_empty = true;
break;
}
}
if (not_empty) {
$('#confirm_invite_modal').modal('show');
e.preventDefault();
}
});
$('#invite_member').on('hidden.bs.modal', function() {
self.clearFields();
});
},
handleSubmit: function(e) {
var invite_ids = this.state.invite_ids;
var count = invite_ids.length;
var invites = [];
var email_errors = this.state.email_errors;
var first_name_errors = this.state.first_name_errors;
var last_name_errors = this.state.last_name_errors;
var valid = true;
for (var i = 0; i < count; i++) {
var index = invite_ids[i];
var invite = {};
invite.email = this.refs["email"+index].getDOMNode().value.trim();
if (!invite.email || !utils.isEmail(invite.email)) {
email_errors[index] = "Please enter a valid email address";
valid = false;
} else {
email_errors[index] = "";
}
if (config.AllowInviteNames) {
invite.first_name = this.refs["first_name"+index].getDOMNode().value.trim();
if (!invite.first_name ) {
first_name_errors[index] = "This is a required field";
valid = false;
} else {
first_name_errors[index] = "";
}
invite.last_name = this.refs["last_name"+index].getDOMNode().value.trim();
if (!invite.last_name ) {
last_name_errors[index] = "This is a required field";
valid = false;
} else {
last_name_errors[index] = "";
}
}
invites.push(invite);
}
this.setState({ email_errors: email_errors, first_name_errors: first_name_errors, last_name_errors: last_name_errors });
if (!valid || invites.length === 0) return;
var data = {}
data["invites"] = invites;
Client.inviteMembers(data,
function() {
$(this.refs.modal.getDOMNode()).attr('data-confirm', 'true');
$(this.refs.modal.getDOMNode()).modal('hide');
}.bind(this),
function(err) {
this.setState({ server_error: err });
}.bind(this)
);
},
componentDidUpdate: function() {
$(this.refs.modalBody.getDOMNode()).css('max-height', $(window).height() - 200);
$(this.refs.modalBody.getDOMNode()).css('overflow-y', 'scroll');
},
addInviteFields: function() {
var count = this.state.id_count + 1;
var invite_ids = this.state.invite_ids;
invite_ids.push(count);
this.setState({ invite_ids: invite_ids, id_count: count });
},
clearFields: function() {
var invite_ids = this.state.invite_ids;
for (var i = 0; i < invite_ids.length; i++) {
var index = invite_ids[i];
this.refs["email"+index].getDOMNode().value = "";
if (config.AllowInviteNames) {
this.refs["first_name"+index].getDOMNode().value = "";
this.refs["last_name"+index].getDOMNode().value = "";
}
}
this.setState({
invite_ids: [0],
id_count: 0,
email_errors: {},
first_name_errors: {},
last_name_errors: {}
});
},
removeInviteFields: function(index) {
var invite_ids = this.state.invite_ids;
var i = invite_ids.indexOf(index);
if (index > -1) invite_ids.splice(i, 1);
this.setState({ invite_ids: invite_ids });
},
getInitialState: function() {
return {
invite_ids: [0],
id_count: 0,
email_errors: {},
first_name_errors: {},
last_name_errors: {}
};
},
render: function() {
var currentUser = UserStore.getCurrentUser()
if (currentUser != null) {
var invite_sections = [];
var invite_ids = this.state.invite_ids;
var self = this;
for (var i = 0; i < invite_ids.length; i++) {
var index = invite_ids[i];
var email_error = this.state.email_errors[index] ? <label className='control-label'>{ this.state.email_errors[index] }</label> : null;
var first_name_error = this.state.first_name_errors[index] ? <label className='control-label'>{ this.state.first_name_errors[index] }</label> : null;
var last_name_error = this.state.last_name_errors[index] ? <label className='control-label'>{ this.state.last_name_errors[index] }</label> : null;
invite_sections[index] = (
<div key={"key" + index}>
{ i ?
<div>
<button type="button" className="btn remove__member" onClick={function(){self.removeInviteFields(index);}}>×</button>
</div>
: ""}
<div className={ email_error ? "form-group invite has-error" : "form-group invite" }>
<input onKeyUp={this.displayNameKeyUp} type="text" ref={"email"+index} className="form-control" placeholder="email@domain.com" maxLength="64" />
{ email_error }
</div>
{ config.AllowInviteNames ?
<div className={ first_name_error ? "form-group invite has-error" : "form-group invite" }>
<input type="text" className="form-control" ref={"first_name"+index} placeholder="First name" maxLength="64" />
{ first_name_error }
</div>
: "" }
{ config.AllowInviteNames ?
<div className={ last_name_error ? "form-group invite has-error" : "form-group invite" }>
<input type="text" className="form-control" ref={"last_name"+index} placeholder="Last name" maxLength="64" />
{ last_name_error }
</div>
: "" }
</div>
);
}
var server_error = this.state.server_error ? <div className='form-group has-error'><label className='control-label'>{ this.state.server_error }</label></div> : null;
return (
<div>
<div className="modal fade" ref="modal" id="invite_member" tabIndex="-1" role="dialog" aria-hidden="true">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" data-dismiss="modal" aria-label="Close" data-reactid=".5.0.0.0.0"><span aria-hidden="true" data-reactid=".5.0.0.0.0.0">×</span></button>
<h4 className="modal-title" id="myModalLabel">Invite New Member</h4>
</div>
<div ref="modalBody" className="modal-body">
<form role="form">
{ invite_sections }
</form>
{ server_error }
<button type="button" className="btn btn-default" onClick={this.addInviteFields}>Add another</button>
<br/>
<br/>
<label className='control-label'>People invited automatically join Town Square channel.</label>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-default" data-dismiss="modal">Close</button>
<button onClick={this.handleSubmit} type="button" className="btn btn-primary">Send Invitations</button>
</div>
</div>
</div>
</div>
<ConfirmModal
id="confirm_invite_modal"
parent_id="invite_member"
title="Discard Invitations?"
message="You have unsent invitations, are you sure you want to discard them?"
confirm_button="Yes, Discard"
/>
</div>
);
} else {
return <div/>;
}
}
});
|