2016-03-31 09:36:25 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-06-10 10:25:38 +09:00
|
|
|
import { intlShape } from 'react-intl';
|
2016-11-07 22:46:31 +09:00
|
|
|
import NovaForm from "meteor/nova:forms";
|
2016-06-14 10:01:44 +09:00
|
|
|
import { withRouter } from 'react-router'
|
2016-06-23 11:40:35 +09:00
|
|
|
import Posts from "meteor/nova:posts";
|
2016-11-07 17:45:17 +09:00
|
|
|
import update from 'immutability-helper';
|
2016-11-07 22:27:34 +09:00
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { connect } from 'react-redux';
|
2016-03-31 09:36:25 +09:00
|
|
|
|
2016-04-14 10:12:35 +09:00
|
|
|
const PostsNewForm = (props, context) => {
|
2016-03-31 09:36:25 +09:00
|
|
|
return (
|
2016-11-04 14:14:19 +01:00
|
|
|
<Telescope.components.CanDo
|
|
|
|
action="posts.new"
|
|
|
|
noPermissionMessage="users.cannot_post"
|
|
|
|
displayNoPermissionMessage={true}
|
|
|
|
>
|
|
|
|
<div className="posts-new-form">
|
|
|
|
<NovaForm
|
|
|
|
collection={Posts}
|
2016-11-07 17:45:17 +09:00
|
|
|
mutationName="postsNew"
|
2016-11-07 22:27:34 +09:00
|
|
|
resultQuery={Posts.graphQLQueries.single}
|
2016-11-07 17:45:17 +09:00
|
|
|
updateQueries={{
|
|
|
|
getPostsView: (prev, { mutationResult }) => {
|
|
|
|
const newPost = mutationResult.data.postsNew;
|
|
|
|
const newList = update(prev, {
|
|
|
|
posts: {
|
|
|
|
$unshift: [newPost],
|
|
|
|
},
|
|
|
|
postsViewTotal: {
|
|
|
|
$set: prev.postsViewTotal + 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return newList;
|
|
|
|
}
|
|
|
|
}}
|
2016-11-04 14:14:19 +01:00
|
|
|
successCallback={post => {
|
|
|
|
props.router.push({pathname: Posts.getPageUrl(post)});
|
|
|
|
props.flash(context.intl.formatMessage({id: "posts.created_message"}), "success");
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Telescope.components.CanDo>
|
2016-11-04 13:57:27 +01:00
|
|
|
);
|
|
|
|
};
|
2016-03-31 09:36:25 +09:00
|
|
|
|
2016-11-05 18:37:46 +09:00
|
|
|
PostsNewForm.propTypes = {
|
|
|
|
router: React.PropTypes.object,
|
2016-11-07 11:46:58 +09:00
|
|
|
flash: React.PropTypes.func,
|
2016-11-05 18:37:46 +09:00
|
|
|
}
|
|
|
|
|
2016-04-14 10:12:35 +09:00
|
|
|
PostsNewForm.contextTypes = {
|
2016-06-10 10:25:38 +09:00
|
|
|
currentUser: React.PropTypes.object,
|
2016-11-03 14:39:27 +09:00
|
|
|
triggerMainRefetch: React.PropTypes.func,
|
2016-06-10 10:25:38 +09:00
|
|
|
intl: intlShape
|
2016-03-31 09:36:25 +09:00
|
|
|
};
|
|
|
|
|
2016-05-22 16:42:24 +09:00
|
|
|
PostsNewForm.displayName = "PostsNewForm";
|
|
|
|
|
2016-11-07 22:27:34 +09:00
|
|
|
const mapStateToProps = state => ({ messages: state.messages });
|
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch);
|
|
|
|
|
|
|
|
module.exports = withRouter(connect(mapStateToProps, mapDispatchToProps)(PostsNewForm));
|