// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import PropTypes from 'prop-types'; import React from 'react'; import {FormattedMessage} from 'react-intl'; import {browserHistory} from 'utils/browser_history'; import {loadMyTeamMembers, updateActive} from 'actions/user_actions.jsx'; import * as Utils from 'utils/utils.jsx'; import ConfirmModal from 'components/confirm_modal.jsx'; export default class TeamMembersDropdown extends React.Component { static propTypes = { user: PropTypes.object.isRequired, currentUser: PropTypes.object.isRequired, currentChannelId: PropTypes.string.isRequired, teamMember: PropTypes.object.isRequired, teamUrl: PropTypes.string.isRequired, actions: PropTypes.shape({ getUser: PropTypes.func.isRequired, getTeamStats: PropTypes.func.isRequired, getChannelStats: PropTypes.func.isRequired, updateTeamMemberSchemeRoles: PropTypes.func.isRequired, removeUserFromTeam: PropTypes.func.isRequired, }).isRequired, } constructor(props) { super(props); this.state = { serverError: null, showDemoteModal: false, user: null, role: null, }; } handleMakeMember = async () => { const me = this.props.currentUser; if (this.props.user.id === me.id && me.roles.includes('system_admin')) { this.handleDemote(this.props.user, 'team_user'); } else { const {error} = await this.props.actions.updateTeamMemberSchemeRoles(this.props.teamMember.team_id, this.props.user.id, true, false); if (error) { this.setState({serverError: error.message}); } else { this.props.actions.getUser(this.props.user.id); if (this.props.user.id === me.id) { loadMyTeamMembers(); } } } } handleRemoveFromTeam = async () => { const {data, error} = await this.props.actions.removeUserFromTeam(this.props.teamMember.team_id, this.props.user.id); if (error) { this.setState({serverError: error.message}); } } handleMakeActive = () => { updateActive(this.props.user.id, true, () => { this.props.actions.getChannelStats(this.props.currentChannelId); this.props.actions.getTeamStats(this.props.teamMember.team_id); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeNotActive = () => { updateActive(this.props.user.id, false, () => { this.props.actions.getChannelStats(this.props.currentChannelId); this.props.actions.getTeamStats(this.props.teamMember.team_id); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeAdmin = async () => { const me = this.props.currentUser; if (this.props.user.id === me.id && me.roles.includes('system_admin')) { this.handleDemote(this.props.user, 'team_user team_admin'); } else { const {error} = await this.props.actions.updateTeamMemberSchemeRoles(this.props.teamMember.team_id, this.props.user.id, true, true); if (error) { this.setState({serverError: error.message}); } else { this.props.actions.getUser(this.props.user.id); } } } handleDemote = (user, role, newRole) => { this.setState({ serverError: this.state.serverError, showDemoteModal: true, user, role, newRole, }); } handleDemoteCancel = () => { this.setState({ serverError: null, showDemoteModal: false, user: null, role: null, newRole: null, }); } handleDemoteSubmit = async () => { const {error} = await this.props.actions.updateTeamMemberSchemeRoles(this.props.teamMember.team_id, this.props.user.id, true, false); if (error) { this.setState({serverError: error.message}); } else { this.props.actions.getUser(this.props.user.id); browserHistory.push(this.props.teamUrl); } } render() { let serverError = null; if (this.state.serverError) { serverError = (