2016-08-08 11:18:21 +09:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
2016-03-29 10:13:35 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-06-09 17:42:20 +09:00
|
|
|
import { FormattedMessage, intlShape } from 'react-intl';
|
2016-11-07 22:46:31 +09:00
|
|
|
import NovaForm from "meteor/nova:forms";
|
2016-06-23 11:40:35 +09:00
|
|
|
import Posts from "meteor/nova:posts";
|
2016-11-07 22:46:31 +09:00
|
|
|
import { withRouter } from 'react-router'
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { connect } from 'react-redux';
|
2016-11-04 14:38:31 +09:00
|
|
|
|
2016-11-03 14:07:58 +09:00
|
|
|
class PostsEditForm extends Component {
|
2016-03-29 10:13:35 +09:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.deletePost = this.deletePost.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
deletePost() {
|
|
|
|
const post = this.props.post;
|
2016-06-09 17:42:20 +09:00
|
|
|
const deletePostConfirm = this.context.intl.formatMessage({id: "posts.delete_confirm"}, {title: post.title});
|
|
|
|
const deletePostSuccess = this.context.intl.formatMessage({id: "posts.delete_success"}, {title: post.title});
|
|
|
|
|
2016-11-03 14:07:58 +09:00
|
|
|
const successOperations = () => {
|
|
|
|
this.props.flash(deletePostSuccess, "success");
|
|
|
|
this.context.events.track("post deleted", {'_id': post._id});
|
|
|
|
// note: no need to call closeCallback because once the post is deleted, the modal automatically disappears
|
|
|
|
}
|
|
|
|
|
2016-06-09 17:42:20 +09:00
|
|
|
if (window.confirm(deletePostConfirm)) {
|
2016-06-23 11:40:35 +09:00
|
|
|
this.context.actions.call('posts.remove', post._id, (error, result) => {
|
2016-11-03 14:07:58 +09:00
|
|
|
if (this.context.refetchPostsListQuery) {
|
|
|
|
// post edit form is being included from a post list, refresh list
|
|
|
|
this.context.refetchPostsListQuery().then(successOperations);
|
|
|
|
} else {
|
2016-11-03 14:13:26 +09:00
|
|
|
// post edit form is being included from a single post, redirect to root
|
|
|
|
this.props.router.push('/');
|
|
|
|
successOperations();
|
2016-11-03 14:07:58 +09:00
|
|
|
}
|
2016-03-29 10:13:35 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 09:33:55 +09:00
|
|
|
renderAdminArea() {
|
|
|
|
return (
|
2016-08-06 19:47:04 +02:00
|
|
|
<Telescope.components.CanDo action="posts.edit.all">
|
|
|
|
<div className="posts-edit-form-admin">
|
|
|
|
<div className="posts-edit-form-id">ID: {this.props.post._id}</div>
|
|
|
|
<Telescope.components.PostsStats post={this.props.post} />
|
|
|
|
</div>
|
|
|
|
</Telescope.components.CanDo>
|
2016-05-20 09:33:55 +09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-03-29 10:13:35 +09:00
|
|
|
render() {
|
2016-03-31 09:36:25 +09:00
|
|
|
|
2016-03-29 10:13:35 +09:00
|
|
|
return (
|
2016-04-19 15:45:36 +09:00
|
|
|
<div className="posts-edit-form">
|
2016-08-06 19:47:04 +02:00
|
|
|
{this.renderAdminArea()}
|
2016-10-27 16:06:42 +09:00
|
|
|
<Telescope.components.PostsSingleContainer
|
|
|
|
postId={this.props.post._id}
|
2016-11-07 22:46:31 +09:00
|
|
|
component={NovaForm}
|
|
|
|
resultQuery={Posts.graphQLQueries.single}
|
2016-03-31 11:04:24 +09:00
|
|
|
componentProps={{
|
|
|
|
collection: Posts,
|
2016-11-07 22:46:31 +09:00
|
|
|
document: this.props.post,
|
|
|
|
mutationName: "postsEdit",
|
|
|
|
resultQuery: Posts.graphQLQueries.single,
|
2016-06-15 11:53:29 +02:00
|
|
|
successCallback: (post) => {
|
2016-11-02 19:56:44 +09:00
|
|
|
this.props.flash(this.context.intl.formatMessage({id: "posts.edit_success"}, {title: post.title}), 'success');
|
2016-06-15 11:53:29 +02:00
|
|
|
}
|
2016-03-31 09:36:25 +09:00
|
|
|
}}
|
2016-03-29 10:13:35 +09:00
|
|
|
/>
|
2016-04-07 10:33:44 +09:00
|
|
|
<hr/>
|
2016-06-09 17:42:20 +09:00
|
|
|
<a onClick={this.deletePost} className="delete-post-link"><Telescope.components.Icon name="close"/> <FormattedMessage id="posts.delete"/></a>
|
2016-03-29 10:13:35 +09:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 10:12:35 +09:00
|
|
|
PostsEditForm.propTypes = {
|
2016-11-04 14:14:19 +01:00
|
|
|
flash: React.PropTypes.func,
|
|
|
|
novaFormMutation: React.PropTypes.func,
|
|
|
|
post: React.PropTypes.object.isRequired,
|
2016-03-29 10:13:35 +09:00
|
|
|
}
|
|
|
|
|
2016-04-14 10:12:35 +09:00
|
|
|
PostsEditForm.contextTypes = {
|
2016-11-03 14:07:58 +09:00
|
|
|
refetchPostsListQuery: React.PropTypes.func,
|
2016-06-09 17:42:20 +09:00
|
|
|
currentUser: React.PropTypes.object,
|
2016-06-14 17:03:35 +09:00
|
|
|
actions: React.PropTypes.object,
|
|
|
|
events: React.PropTypes.object,
|
2016-11-03 14:07:58 +09:00
|
|
|
closeCallback: React.PropTypes.func,
|
2016-06-09 17:42:20 +09:00
|
|
|
intl: intlShape
|
|
|
|
}
|
2016-03-29 10:13:35 +09:00
|
|
|
|
2016-11-07 22:46:31 +09:00
|
|
|
const mapStateToProps = state => ({ messages: state.messages });
|
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch);
|
|
|
|
|
|
|
|
module.exports = withRouter(connect(mapStateToProps, mapDispatchToProps)(PostsEditForm));
|