// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
const MemberList = require('./member_list.jsx');
const LoadingScreen = require('./loading_screen.jsx');
const UserStore = require('../stores/user_store.jsx');
const ChannelStore = require('../stores/channel_store.jsx');
const Utils = require('../utils/utils.jsx');
const Client = require('../utils/client.jsx');
const AsyncClient = require('../utils/async_client.jsx');
const Modal = ReactBootstrap.Modal;
export default class ChannelInviteModal extends React.Component {
constructor() {
super();
this.onListenerChange = this.onListenerChange.bind(this);
this.handleInvite = this.handleInvite.bind(this);
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((a, b) => {
return a.username.localeCompare(b.username);
});
var channelName = '';
if (ChannelStore.getCurrent()) {
channelName = ChannelStore.getCurrent().display_name;
}
return {
nonmembers,
memberIds,
channelName,
loading
};
}
componentWillReceiveProps(nextProps) {
if (!this.props.show && nextProps.show) {
ChannelStore.addExtraInfoChangeListener(this.onListenerChange);
ChannelStore.addChangeListener(this.onListenerChange);
UserStore.addChangeListener(this.onListenerChange);
this.onListenerChange();
} else if (this.props.show && !nextProps.show) {
ChannelStore.removeExtraInfoChangeListener(this.onListenerChange);
ChannelStore.removeChangeListener(this.onListenerChange);
UserStore.removeChangeListener(this.onListenerChange);
}
}
onListenerChange() {
var newState = this.getStateFromStores();
if (!Utils.areStatesEqual(this.state, newState)) {
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,
() => {
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, nonmembers});
AsyncClient.getChannelExtraInfo(true);
},
(err) => {
this.setState({inviteError: err.message});
}
);
}
render() {
var inviteError = null;
if (this.state.inviteError) {
inviteError = ();
}
var currentMember = ChannelStore.getCurrentMember();
var isAdmin = false;
if (currentMember) {
isAdmin = Utils.isAdmin(currentMember.roles) || Utils.isAdmin(UserStore.getCurrentUser().roles);
}
var content;
if (this.state.loading) {
content = ();
} else {
content = (
);
}
return (
{'Add New Members to '}{this.state.channelName}
{inviteError}
{content}
);
}
}
ChannelInviteModal.propTypes = {
show: React.PropTypes.bool.isRequired,
onModalDismissed: React.PropTypes.func.isRequired
};