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
|
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
var UserStore = require('../stores/user_store.jsx');
var ChannelStore = require('../stores/channel_store.jsx');
var MemberList = require('./member_list.jsx');
var LoadingScreen = require('./loading_screen.jsx');
var utils = require('../utils/utils.jsx');
var client = require('../utils/client.jsx');
var AsyncClient = require('../utils/async_client.jsx');
export default class ChannelInviteModal extends React.Component {
constructor() {
super();
this.componentDidMount = this.componentDidMount.bind(this);
this.componentWillUnmount = this.componentWillUnmount.bind(this);
this.onShow = this.onShow.bind(this);
this.onHide = this.onHide.bind(this);
this.onListenerChange = this.onListenerChange.bind(this);
this.handleInvite = this.handleInvite.bind(this);
this.isShown = false;
this.state = this.getStateFromStores();
}
getStateFromStores() {
function getId(user) {
return user.id;
}
var users = UserStore.getActiveOnlyProfiles();
var memberIds = ChannelStore.getCurrentExtraInfo().members.map(getId);
var loading = $.isEmptyObject(users);
var nonmembers = [];
for (var id in users) {
if (memberIds.indexOf(id) === -1) {
nonmembers.push(users[id]);
}
}
nonmembers.sort(function sortByUsername(a, b) {
return a.username.localeCompare(b.username);
});
var channelName = '';
if (ChannelStore.getCurrent()) {
channelName = ChannelStore.getCurrent().display_name;
}
return {
nonmembers: nonmembers,
memberIds: memberIds,
channelName: channelName,
loading: loading
};
}
componentDidMount() {
$(React.findDOMNode(this)).on('hidden.bs.modal', this.onHide);
$(React.findDOMNode(this)).on('show.bs.modal', this.onShow);
ChannelStore.addExtraInfoChangeListener(this.onListenerChange);
ChannelStore.addChangeListener(this.onListenerChange);
UserStore.addChangeListener(this.onListenerChange);
}
componentWillUnmount() {
ChannelStore.removeExtraInfoChangeListener(this.onListenerChange);
ChannelStore.removeChangeListener(this.onListenerChange);
UserStore.removeChangeListener(this.onListenerChange);
}
onShow() {
this.isShown = true;
this.onListenerChange();
}
onHide() {
this.isShown = false;
}
onListenerChange() {
var newState = this.getStateFromStores();
if (!utils.areStatesEqual(this.state, newState) && this.isShown) {
this.setState(newState);
}
}
handleInvite(userId) {
// Make sure the user isn't already a member of the channel
if (this.state.memberIds.indexOf(userId) > -1) {
return;
}
var data = {};
data.user_id = userId;
client.addChannelMember(ChannelStore.getCurrentId(), data,
function sucess() {
var nonmembers = this.state.nonmembers;
var memberIds = this.state.memberIds;
for (var i = 0; i < nonmembers.length; i++) {
if (userId === nonmembers[i].id) {
nonmembers[i].invited = true;
memberIds.push(userId);
break;
}
}
this.setState({inviteError: null, memberIds: memberIds, nonmembers: nonmembers});
AsyncClient.getChannelExtraInfo(true);
}.bind(this),
function error(err) {
this.setState({inviteError: err.message});
}.bind(this)
);
}
render() {
var inviteError = null;
if (this.state.inviteError) {
inviteError = (<label className='has-error control-label'>{this.state.inviteError}</label>);
}
var currentMember = ChannelStore.getCurrentMember();
var isAdmin = false;
if (currentMember) {
isAdmin = currentMember.roles.indexOf('admin') > -1 || UserStore.getCurrentUser().roles.indexOf('admin') > -1;
}
var content;
if (this.state.loading) {
content = (<LoadingScreen />);
} else {
content = (<MemberList memberList={this.state.nonmembers} isAdmin={isAdmin} handleInvite={this.handleInvite} />);
}
return (
<div className='modal fade' id='channel_invite' tabIndex='-1' role='dialog' aria-hidden='true'>
<div className='modal-dialog' role='document'>
<div className='modal-content'>
<div className='modal-header'>
<button type='button' className='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>×</span></button>
<h4 className='modal-title'>Add New Members to <span className='name'>{this.state.channelName}</span></h4>
</div>
<div className='modal-body'>
{inviteError}
{content}
</div>
<div className='modal-footer'>
<button type='button' className='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
);
}
}
ChannelInviteModal.displayName = 'ChannelInviteModal';
|