// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import $ from 'jquery'; import ReactDOM from 'react-dom'; import * as AsyncClient from 'utils/async_client.jsx'; import Client from 'client/web_client.jsx'; import Constants from 'utils/constants.jsx'; import {intlShape, injectIntl, defineMessages, FormattedMessage} from 'react-intl'; import PreferenceStore from 'stores/preference_store.jsx'; import {Modal} from 'react-bootstrap'; const holders = defineMessages({ error: { id: 'edit_channel_purpose_modal.error', defaultMessage: 'This channel purpose is too long, please enter a shorter one' } }); import React from 'react'; export default class EditChannelPurposeModal extends React.Component { constructor(props) { super(props); this.handleHide = this.handleHide.bind(this); this.handleSave = this.handleSave.bind(this); this.handleKeyDown = this.handleKeyDown.bind(this); this.onPreferenceChange = this.onPreferenceChange.bind(this); this.ctrlSend = PreferenceStore.getBool(Constants.Preferences.CATEGORY_ADVANCED_SETTINGS, 'send_on_ctrl_enter'); this.state = { serverError: '', submitted: false }; } componentDidMount() { PreferenceStore.addChangeListener(this.onPreferenceChange); } componentWillUnmount() { PreferenceStore.removeChangeListener(this.onPreferenceChange); } componentDidUpdate() { if (this.props.show) { $(ReactDOM.findDOMNode(this.refs.purpose)).focus(); } } handleHide() { this.setState({serverError: ''}); if (this.props.onModalDismissed) { this.props.onModalDismissed(); } } onPreferenceChange() { this.ctrlSend = PreferenceStore.getBool(Constants.Preferences.CATEGORY_ADVANCED_SETTINGS, 'send_on_ctrl_enter'); } handleKeyDown(e) { if (this.ctrlSend && e.keyCode === Constants.KeyCodes.ENTER && e.ctrlKey) { e.preventDefault(); this.handleSave(e); } else if (!this.ctrlSend && e.keyCode === Constants.KeyCodes.ENTER && !e.shiftKey && !e.altKey) { e.preventDefault(); this.handleSave(e); } } handleSave() { if (!this.props.channel) { return; } this.setState({submitted: true}); Client.updateChannelPurpose( this.props.channel.id, ReactDOM.findDOMNode(this.refs.purpose).value.trim(), () => { AsyncClient.getChannel(this.props.channel.id); this.handleHide(); }, (err) => { if (err.id === 'api.context.invalid_param.app_error') { this.setState({serverError: this.props.intl.formatMessage(holders.error)}); } else { this.setState({serverError: err.message}); } } ); } render() { if (!this.props.show) { return null; } let serverError = null; if (this.state.serverError) { serverError = (