Vulcan/packages/vulcan-base-components/lib/posts/PostsEditForm.jsx

66 lines
2.3 KiB
React
Raw Normal View History

2017-05-19 14:42:43 -06:00
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { Components, registerComponent, getFragment, withMessages, withCurrentUser } from 'meteor/vulcan:core';
2017-06-01 11:42:30 +09:00
import { intlShape } from 'meteor/vulcan:i18n';
2017-03-23 16:27:59 +09:00
import Posts from "meteor/vulcan:posts";
import Users from "meteor/vulcan:users";
import { withRouter } from 'react-router'
2016-11-04 14:38:31 +09:00
2017-05-19 14:42:43 -06:00
class PostsEditForm extends PureComponent {
2016-03-29 10:13:35 +09:00
2016-05-20 09:33:55 +09:00
renderAdminArea() {
return (
<Components.ShowIf check={Posts.options.mutations.edit.check} document={this.props.post}>
<div className="posts-edit-form-admin">
<div className="posts-edit-form-id">ID: {this.props.post._id}</div>
<Components.PostsStats post={this.props.post} />
</div>
</Components.ShowIf>
2016-05-20 09:33:55 +09:00
)
}
2016-03-29 10:13:35 +09:00
render() {
2016-03-29 10:13:35 +09:00
return (
<div className="posts-edit-form">
{Users.isAdmin(this.props.currentUser) ? this.renderAdminArea() : null}
<Components.SmartForm
collection={Posts}
documentId={this.props.post._id}
mutationFragment={getFragment('PostsPage')}
successCallback={post => {
this.props.closeModal();
2017-05-19 14:42:43 -06:00
this.props.flash(this.context.intl.formatMessage({ id: 'posts.edit_success' }, { title: post.title }), 'success');
}}
2017-05-19 14:42:43 -06:00
removeSuccessCallback={({ documentId, documentTitle }) => {
// post edit form is being included from a single post, redirect to index
// note: this.props.params is in the worst case an empty obj (from react-router)
if (this.props.params._id) {
this.props.router.push('/');
}
2017-05-19 14:42:43 -06:00
const deleteDocumentSuccess = this.context.intl.formatMessage({ id: 'posts.delete_success' }, { title: documentTitle });
this.props.flash(deleteDocumentSuccess, 'success');
// todo: handle events in collection callbacks
// this.context.events.track("post deleted", {_id: documentId});
}}
2016-11-23 11:07:48 +09:00
showRemove={true}
2016-03-29 10:13:35 +09:00
/>
</div>
);
2016-03-29 10:13:35 +09:00
}
}
2016-04-14 10:12:35 +09:00
PostsEditForm.propTypes = {
2017-05-19 14:42:43 -06:00
closeModal: PropTypes.func,
flash: PropTypes.func,
post: PropTypes.object.isRequired,
2016-03-29 10:13:35 +09:00
}
2016-04-14 10:12:35 +09:00
PostsEditForm.contextTypes = {
2016-06-09 17:42:20 +09:00
intl: intlShape
}
2016-03-29 10:13:35 +09:00
registerComponent('PostsEditForm', PostsEditForm, withMessages, withRouter, withCurrentUser);