// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import ChannelStore from 'stores/channel_store.jsx'; import TeamStore from 'stores/team_store.jsx'; import UserStore from 'stores/user_store.jsx'; import {removeUserFromChannel, makeUserChannelAdmin, makeUserChannelMember} from 'actions/channel_actions.jsx'; import * as AsyncClient from 'utils/async_client.jsx'; import * as Utils from 'utils/utils.jsx'; import {canManageMembers} from 'utils/channel_utils.jsx'; import React from 'react'; import {FormattedMessage} from 'react-intl'; export default class ChannelMembersDropdown extends React.Component { constructor(props) { super(props); this.handleRemoveFromChannel = this.handleRemoveFromChannel.bind(this); this.handleMakeChannelMember = this.handleMakeChannelMember.bind(this); this.handleMakeChannelAdmin = this.handleMakeChannelAdmin.bind(this); this.state = { serverError: null, user: null, role: null }; } handleRemoveFromChannel() { removeUserFromChannel( this.props.channel.id, this.props.user.id, () => { AsyncClient.getChannelStats(this.props.channel.id); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeChannelMember() { makeUserChannelMember( this.props.channel.id, this.props.user.id, () => { AsyncClient.getChannelStats(this.props.channel.id); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeChannelAdmin() { makeUserChannelAdmin( this.props.channel.id, this.props.user.id, () => { AsyncClient.getChannelStats(this.props.channel.id); }, (err) => { this.setState({serverError: err.message}); } ); } // Checks if the user this menu is for is a channel admin or not. isChannelAdmin() { if (Utils.isChannelAdmin(this.props.channelMember.roles)) { return true; } return false; } // Checks if the current user has the power to change the roles of this member. canChangeMemberRoles() { if (UserStore.isSystemAdminForCurrentUser()) { return true; } else if (TeamStore.isTeamAdminForCurrentTeam()) { return true; } else if (ChannelStore.isChannelAdminForCurrentChannel()) { return true; } return false; } // Checks if the current user has the power to remove this member from the channel. canRemoveMember() { return canManageMembers(this.props.channel, UserStore.isSystemAdminForCurrentUser(), TeamStore.isTeamAdminForCurrentTeam(), ChannelStore.isChannelAdminForCurrentChannel()); } render() { let serverError = null; if (this.state.serverError) { serverError = (
); } if (this.props.user.id === UserStore.getCurrentId()) { return null; } if (this.canChangeMemberRoles()) { let role = ( ); if (this.isChannelAdmin()) { role = ( ); } let removeFromChannel = null; if (this.canRemoveMember()) { removeFromChannel = (
  • ); } let makeChannelMember = null; if (this.isChannelAdmin()) { makeChannelMember = (
  • ); } let makeChannelAdmin = null; if (!this.isChannelAdmin()) { makeChannelAdmin = (
  • ); } return (
    {role}
      {makeChannelMember} {makeChannelAdmin} {removeFromChannel}
    {serverError}
    ); } else if (this.canRemoveMember()) { return ( ); } else if (this.isChannelAdmin()) { return (
    ); } return (
    ); } } ChannelMembersDropdown.propTypes = { channel: React.PropTypes.object.isRequired, user: React.PropTypes.object.isRequired, teamMember: React.PropTypes.object.isRequired, channelMember: React.PropTypes.object.isRequired };