// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import PreferenceStore from 'stores/preference_store.jsx'; import Constants from 'utils/constants.jsx'; import * as Utils from 'utils/utils.jsx'; import PropTypes from 'prop-types'; import React from 'react'; import {Modal} from 'react-bootstrap'; import {FormattedMessage} from 'react-intl'; import {updateChannelPurpose} from 'actions/channel_actions.jsx'; 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: '', show: true, submitted: false }; } componentDidMount() { PreferenceStore.addChangeListener(this.onPreferenceChange); Utils.placeCaretAtEnd(this.refs.purpose); } componentWillUnmount() { PreferenceStore.removeChangeListener(this.onPreferenceChange); } handleHide() { this.setState({show: false}); } 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}); updateChannelPurpose( this.props.channel.id, this.refs.purpose.value.trim(), () => { this.handleHide(); }, (err) => { if (err.id === 'api.context.invalid_param.app_error') { this.setState({serverError: Utils.localizeMessage('edit_channel_purpose_modal.error', 'This channel purpose is too long, please enter a shorter one')}); } else { this.setState({serverError: err.message}); } } ); } render() { let serverError = null; if (this.state.serverError) { serverError = (
{channelPurposeModal}
{serverError}