// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import Textbox from './textbox.jsx';
import BrowserStore from 'stores/browser_store.jsx';
import PostStore from 'stores/post_store.jsx';
import MessageHistoryStore from 'stores/message_history_store.jsx';
import PreferenceStore from 'stores/preference_store.jsx';
import * as GlobalActions from 'actions/global_actions.jsx';
import {loadPosts} from 'actions/post_actions.jsx';
import Client from 'client/web_client.jsx';
import * as UserAgent from 'utils/user_agent.jsx';
import * as AsyncClient from 'utils/async_client.jsx';
import * as Utils from 'utils/utils.jsx';
import Constants from 'utils/constants.jsx';
const KeyCodes = Constants.KeyCodes;
import $ from 'jquery';
import React from 'react';
import ReactDOM from 'react-dom';
import {FormattedMessage} from 'react-intl';
export default class EditPostModal extends React.Component {
constructor(props) {
super(props);
this.handleEdit = this.handleEdit.bind(this);
this.handleEditKeyPress = this.handleEditKeyPress.bind(this);
this.handleEditPostEvent = this.handleEditPostEvent.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleChange = this.handleChange.bind(this);
this.onPreferenceChange = this.onPreferenceChange.bind(this);
this.onModalHidden = this.onModalHidden.bind(this);
this.onModalShow = this.onModalShow.bind(this);
this.onModalShown = this.onModalShown.bind(this);
this.onModalHide = this.onModalHide.bind(this);
this.onModalKeyDown = this.onModalKeyDown.bind(this);
this.state = {
editText: '',
originalText: '',
title: '',
post_id: '',
channel_id: '',
comments: 0,
refocusId: '',
ctrlSend: PreferenceStore.getBool(Constants.Preferences.CATEGORY_ADVANCED_SETTINGS, 'send_on_ctrl_enter'),
postError: ''
};
}
handleEdit() {
const updatedPost = {
message: this.state.editText,
id: this.state.post_id,
channel_id: this.state.channel_id
};
if (this.state.postError) {
this.setState({errorClass: 'animation--highlight'});
setTimeout(() => {
this.setState({errorClass: null});
}, Constants.ANIMATION_TIMEOUT);
return;
}
if (updatedPost.message === this.state.originalText) {
// no changes so just close the modal
$('#edit_post').modal('hide');
return;
}
MessageHistoryStore.storeMessageInHistory(updatedPost.message);
if (updatedPost.message.trim().length === 0) {
var tempState = this.state;
Reflect.deleteProperty(tempState, 'editText');
BrowserStore.setItem('edit_state_transfer', tempState);
$('#edit_post').modal('hide');
GlobalActions.showDeletePostModal(PostStore.getPost(this.state.channel_id, this.state.post_id), this.state.comments);
return;
}
Client.updatePost(
updatedPost,
() => {
loadPosts(updatedPost.channel_id);
window.scrollTo(0, 0);
},
(err) => {
AsyncClient.dispatchError(err, 'updatePost');
}
);
$('#edit_post').modal('hide');
}
handleChange(e) {
const message = e.target.value;
this.setState({
editText: message
});
if (message.length > Constants.CHARACTER_LIMIT) {
const errorMessage = (