2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2015-11-19 21:12:56 -05:00
|
|
|
import * as Client from '../utils/client.jsx';
|
|
|
|
|
import PostStore from '../stores/post_store.jsx';
|
|
|
|
|
import ModalStore from '../stores/modal_store.jsx';
|
2015-11-13 12:20:33 -05:00
|
|
|
var Modal = ReactBootstrap.Modal;
|
2015-11-19 21:12:56 -05:00
|
|
|
import * as Utils from '../utils/utils.jsx';
|
|
|
|
|
import * as AsyncClient from '../utils/async_client.jsx';
|
|
|
|
|
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
|
|
|
|
|
import Constants from '../utils/constants.jsx';
|
2016-02-02 19:39:56 -03:00
|
|
|
|
|
|
|
|
import {FormattedMessage} from 'mm-intl';
|
|
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
var ActionTypes = Constants.ActionTypes;
|
|
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
export default class DeletePostModal extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.handleDelete = this.handleDelete.bind(this);
|
2015-11-13 12:20:33 -05:00
|
|
|
this.handleToggle = this.handleToggle.bind(this);
|
|
|
|
|
this.handleHide = this.handleHide.bind(this);
|
2015-08-31 09:35:31 -07:00
|
|
|
this.onListenerChange = this.onListenerChange.bind(this);
|
|
|
|
|
|
2015-11-13 12:20:33 -05:00
|
|
|
this.selectedList = null;
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2016-01-20 13:16:53 -08:00
|
|
|
show: false,
|
2015-11-13 12:20:33 -05:00
|
|
|
post: null,
|
|
|
|
|
commentCount: 0,
|
|
|
|
|
error: ''
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
ModalStore.addModalListener(ActionTypes.TOGGLE_DELETE_POST_MODAL, this.handleToggle);
|
|
|
|
|
PostStore.addSelectedPostChangeListener(this.onListenerChange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
PostStore.removeSelectedPostChangeListener(this.onListenerChange);
|
|
|
|
|
ModalStore.removeModalListener(ActionTypes.TOGGLE_DELETE_POST_MODAL, this.handleToggle);
|
2015-08-31 09:35:31 -07:00
|
|
|
}
|
2015-11-13 12:20:33 -05:00
|
|
|
|
2016-01-20 13:16:53 -08:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
|
if (this.state.show && !prevState.show) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
$(ReactDOM.findDOMNode(this.refs.deletePostBtn)).focus();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
handleDelete() {
|
2015-11-13 12:20:33 -05:00
|
|
|
Client.deletePost(
|
|
|
|
|
this.state.post.channel_id,
|
|
|
|
|
this.state.post.id,
|
|
|
|
|
() => {
|
|
|
|
|
var selectedList = this.selectedList;
|
|
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
if (selectedList && selectedList.order && selectedList.order.length > 0) {
|
|
|
|
|
var selectedPost = selectedList.posts[selectedList.order[0]];
|
2015-11-13 12:20:33 -05:00
|
|
|
if ((selectedPost.id === this.state.post.id && !this.state.root_id) || selectedPost.root_id === this.state.post.id) {
|
2015-06-14 23:53:32 -08:00
|
|
|
AppDispatcher.handleServerAction({
|
2016-02-08 10:33:59 -05:00
|
|
|
type: ActionTypes.RECEIVED_SEARCH,
|
2015-06-14 23:53:32 -08:00
|
|
|
results: null
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AppDispatcher.handleServerAction({
|
2016-02-08 10:33:59 -05:00
|
|
|
type: ActionTypes.RECEIVED_POST_SELECTED,
|
2015-06-14 23:53:32 -08:00
|
|
|
results: null
|
|
|
|
|
});
|
2015-11-13 12:20:33 -05:00
|
|
|
} else if (selectedPost.id === this.state.post.id && this.state.root_id) {
|
2015-08-31 09:35:31 -07:00
|
|
|
if (selectedPost.root_id && selectedPost.root_id.length > 0 && selectedList.posts[selectedPost.root_id]) {
|
|
|
|
|
selectedList.order = [selectedPost.root_id];
|
|
|
|
|
delete selectedList.posts[selectedPost.id];
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
AppDispatcher.handleServerAction({
|
2016-02-08 10:33:59 -05:00
|
|
|
type: ActionTypes.RECEIVED_POST_SELECTED,
|
2015-08-31 09:35:31 -07:00
|
|
|
post_list: selectedList
|
2015-06-14 23:53:32 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AppDispatcher.handleServerAction({
|
2016-02-08 10:33:59 -05:00
|
|
|
type: ActionTypes.RECEIVED_SEARCH,
|
2015-06-14 23:53:32 -08:00
|
|
|
results: null
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-13 12:20:33 -05:00
|
|
|
|
2016-02-01 15:22:44 -05:00
|
|
|
PostStore.deletePost(this.state.post);
|
2015-11-13 12:20:33 -05:00
|
|
|
AsyncClient.getPosts(this.state.post.channel_id);
|
|
|
|
|
},
|
|
|
|
|
(err) => {
|
2015-08-31 09:35:31 -07:00
|
|
|
AsyncClient.dispatchError(err, 'deletePost');
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
);
|
2015-11-13 12:20:33 -05:00
|
|
|
|
|
|
|
|
this.handleHide();
|
2015-08-31 09:35:31 -07:00
|
|
|
}
|
2015-11-13 12:20:33 -05:00
|
|
|
|
|
|
|
|
handleToggle(value, args) {
|
|
|
|
|
this.setState({
|
|
|
|
|
show: value,
|
|
|
|
|
post: args.post,
|
|
|
|
|
commentCount: args.commentCount,
|
|
|
|
|
error: ''
|
|
|
|
|
});
|
2015-08-31 09:35:31 -07:00
|
|
|
}
|
2015-11-13 12:20:33 -05:00
|
|
|
|
|
|
|
|
handleHide() {
|
|
|
|
|
this.setState({show: false});
|
2015-08-31 09:35:31 -07:00
|
|
|
}
|
2015-11-13 12:20:33 -05:00
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
onListenerChange() {
|
2015-06-14 23:53:32 -08:00
|
|
|
var newList = PostStore.getSelectedPost();
|
2015-11-13 12:20:33 -05:00
|
|
|
if (!Utils.areObjectsEqual(this.selectedList, newList)) {
|
|
|
|
|
this.selectedList = newList;
|
2015-08-31 09:35:31 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-11-13 12:20:33 -05:00
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
render() {
|
2015-11-13 12:20:33 -05:00
|
|
|
if (!this.state.post) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 09:35:31 -07:00
|
|
|
var error = null;
|
|
|
|
|
if (this.state.error) {
|
|
|
|
|
error = <div className='form-group has-error'><label className='control-label'>{this.state.error}</label></div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var commentWarning = '';
|
2015-11-13 12:20:33 -05:00
|
|
|
if (this.state.commentCount > 0) {
|
2016-02-02 19:39:56 -03:00
|
|
|
commentWarning = (
|
|
|
|
|
<FormattedMessage
|
|
|
|
|
id='delete_post.warning'
|
|
|
|
|
defaultMessage='This post has {count} comment(s) on it.'
|
|
|
|
|
values={{
|
|
|
|
|
count: this.state.commentCount
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-02 19:39:56 -03:00
|
|
|
const postTerm = this.state.post.root_id ? (
|
|
|
|
|
<FormattedMessage
|
|
|
|
|
id='delete_post.comment'
|
|
|
|
|
defaultMessage='Comment'
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<FormattedMessage
|
|
|
|
|
id='delete_post.post'
|
|
|
|
|
defaultMessage='Post'
|
|
|
|
|
/>
|
|
|
|
|
);
|
2015-11-13 12:20:33 -05:00
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
return (
|
2015-11-13 12:20:33 -05:00
|
|
|
<Modal
|
|
|
|
|
show={this.state.show}
|
|
|
|
|
onHide={this.handleHide}
|
2015-08-31 09:35:31 -07:00
|
|
|
>
|
2015-11-13 12:20:33 -05:00
|
|
|
<Modal.Header closeButton={true}>
|
2016-02-02 19:39:56 -03:00
|
|
|
<Modal.Title>
|
|
|
|
|
<FormattedMessage
|
|
|
|
|
id='delete_post.confirm'
|
|
|
|
|
defaultMessage='Confirm {term} Delete'
|
|
|
|
|
values={{
|
|
|
|
|
term: (postTerm)
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Modal.Title>
|
2015-11-13 12:20:33 -05:00
|
|
|
</Modal.Header>
|
|
|
|
|
<Modal.Body>
|
2016-02-02 19:39:56 -03:00
|
|
|
<FormattedMessage
|
|
|
|
|
id='delete_post.question'
|
2016-02-09 16:44:19 -03:00
|
|
|
defaultMessage='Are you sure you want to delete this {term}?'
|
2016-02-02 19:39:56 -03:00
|
|
|
values={{
|
|
|
|
|
term: (postTerm)
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2015-11-13 12:20:33 -05:00
|
|
|
<br />
|
|
|
|
|
<br />
|
2015-08-31 09:35:31 -07:00
|
|
|
{commentWarning}
|
2015-11-13 12:20:33 -05:00
|
|
|
{error}
|
|
|
|
|
</Modal.Body>
|
|
|
|
|
<Modal.Footer>
|
2015-08-31 09:35:31 -07:00
|
|
|
<button
|
|
|
|
|
type='button'
|
|
|
|
|
className='btn btn-default'
|
2015-11-13 12:20:33 -05:00
|
|
|
onClick={this.handleHide}
|
2015-08-31 09:35:31 -07:00
|
|
|
>
|
2016-02-02 19:39:56 -03:00
|
|
|
<FormattedMessage
|
|
|
|
|
id='delete_post.cancel'
|
|
|
|
|
defaultMessage='Cancel'
|
|
|
|
|
/>
|
2015-08-31 09:35:31 -07:00
|
|
|
</button>
|
|
|
|
|
<button
|
2016-01-20 13:16:53 -08:00
|
|
|
ref='deletePostBtn'
|
2015-08-31 09:35:31 -07:00
|
|
|
type='button'
|
|
|
|
|
className='btn btn-danger'
|
|
|
|
|
onClick={this.handleDelete}
|
|
|
|
|
>
|
2016-02-02 19:39:56 -03:00
|
|
|
<FormattedMessage
|
|
|
|
|
id='delete_post.del'
|
|
|
|
|
defaultMessage='Delete'
|
|
|
|
|
/>
|
2015-08-31 09:35:31 -07:00
|
|
|
</button>
|
2015-11-13 12:20:33 -05:00
|
|
|
</Modal.Footer>
|
|
|
|
|
</Modal>
|
2015-06-14 23:53:32 -08:00
|
|
|
);
|
|
|
|
|
}
|
2015-08-31 09:35:31 -07:00
|
|
|
}
|