Vulcan/packages/base-components/lib/posts/PostEdit.jsx

58 lines
1.6 KiB
React
Raw Normal View History

2016-02-25 17:44:43 +09:00
import Core from "meteor/nova:core";
({Messages, NovaForms} = Core);
2016-02-23 13:10:08 +09:00
2016-02-18 16:26:52 +09:00
import Formsy from 'formsy-react';
const PostEdit = React.createClass({
propTypes: {
2016-02-23 11:34:40 +09:00
document: React.PropTypes.object.isRequired,
currentUser: React.PropTypes.object.isRequired,
categories: React.PropTypes.array
},
2016-02-18 16:26:52 +09:00
submitForm(data) {
2016-02-23 11:34:40 +09:00
const post = this.props.document;
2016-02-18 16:26:52 +09:00
const modifier = {$set: _.compactObject(data)};
2016-02-23 11:34:40 +09:00
event.preventDefault();
Meteor.call('posts.edit', post._id, modifier, (error, post) => {
2016-02-21 14:02:24 +09:00
if (error) {
2016-02-23 13:10:08 +09:00
console.log(error)
Messages.flash(error.message, "error")
2016-02-21 14:02:24 +09:00
} else {
2016-02-23 13:10:08 +09:00
Messages.flash("Post edited.", "success")
2016-02-21 14:02:24 +09:00
FlowRouter.go('posts.single', post);
}
});
2016-02-18 16:26:52 +09:00
},
render() {
({CanEditPost} = Telescope.components);
2016-02-23 11:34:40 +09:00
const post = this.props.document;
2016-02-18 16:26:52 +09:00
const categoriesOptions = this.props.categories.map(category => {
return {
value: category._id,
label: category.name
}
});
2016-02-25 17:44:43 +09:00
const fields = Posts.simpleSchema().getEditableFields(this.props.currentUser);
2016-02-18 16:26:52 +09:00
return (
2016-02-23 11:34:40 +09:00
<CanEditPost user={this.props.currentUser} post={post}>
<div className="post-edit">
2016-02-23 11:34:40 +09:00
<h3>Edit Post {post.title}</h3>
<Formsy.Form onSubmit={this.submitForm}>
2016-02-25 17:44:43 +09:00
{fields.map(fieldName => NovaForms.getComponent(fieldName, Posts.simpleSchema()._schema[fieldName], post))}
<button type="submit" className="button button--primary">Submit</button>
</Formsy.Form>
</div>
</CanEditPost>
2016-02-18 16:26:52 +09:00
)
}
});
2016-02-16 15:08:30 +09:00
module.exports = PostEdit;