// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import * as Client from '../../utils/client.jsx'; import * as Utils from '../../utils/utils.jsx'; import UserStore from '../../stores/user_store.jsx'; import ConfirmModal from '../confirm_modal.jsx'; import TeamStore from '../../stores/team_store.jsx'; import {FormattedMessage} from 'mm-intl'; export default class UserItem extends React.Component { constructor(props) { super(props); this.handleMakeMember = this.handleMakeMember.bind(this); this.handleMakeActive = this.handleMakeActive.bind(this); this.handleMakeNotActive = this.handleMakeNotActive.bind(this); this.handleMakeAdmin = this.handleMakeAdmin.bind(this); this.handleMakeSystemAdmin = this.handleMakeSystemAdmin.bind(this); this.handleResetPassword = this.handleResetPassword.bind(this); this.handleDemote = this.handleDemote.bind(this); this.handleDemoteSubmit = this.handleDemoteSubmit.bind(this); this.handleDemoteCancel = this.handleDemoteCancel.bind(this); this.state = { serverError: null, showDemoteModal: false, user: null, role: null }; } handleMakeMember(e) { e.preventDefault(); const me = UserStore.getCurrentUser(); if (this.props.user.id === me.id) { this.handleDemote(this.props.user, ''); } else { const data = { user_id: this.props.user.id, new_roles: '' }; Client.updateRoles(data, () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } } handleMakeActive(e) { e.preventDefault(); Client.updateActive(this.props.user.id, true, () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeNotActive(e) { e.preventDefault(); Client.updateActive(this.props.user.id, false, () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } handleMakeAdmin(e) { e.preventDefault(); const me = UserStore.getCurrentUser(); if (this.props.user.id === me.id) { this.handleDemote(this.props.user, 'admin'); } else { const data = { user_id: this.props.user.id, new_roles: 'admin' }; Client.updateRoles(data, () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } } handleMakeSystemAdmin(e) { e.preventDefault(); const data = { user_id: this.props.user.id, new_roles: 'system_admin' }; Client.updateRoles(data, () => { this.props.refreshProfiles(); }, (err) => { this.setState({serverError: err.message}); } ); } handleResetPassword(e) { e.preventDefault(); this.props.doPasswordReset(this.props.user); } handleDemote(user, role) { this.setState({ serverError: this.state.serverError, showDemoteModal: true, user, role }); } handleDemoteCancel() { this.setState({ serverError: null, showDemoteModal: false, user: null, role: null }); } handleDemoteSubmit() { const data = { user_id: this.props.user.id, new_roles: this.state.role }; Client.updateRoles(data, () => { this.setState({ serverError: null, showDemoteModal: false, user: null, role: null }); const teamUrl = TeamStore.getCurrentTeamUrl(); if (teamUrl) { window.location.href = teamUrl; } else { window.location.href = '/'; } }, (err) => { this.setState({ serverError: err.message }); } ); } render() { let serverError = null; if (this.state.serverError) { serverError = (