Vulcan/packages/nova-base-components/lib/containers/CommentsEditFormContainer.jsx

76 lines
2.4 KiB
React
Raw Normal View History

2016-11-05 18:37:46 +09:00
import Telescope from 'meteor/nova:lib';
import React, { PropTypes, Component } from 'react';
import { FormattedMessage, intlShape } from 'react-intl';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { withRouter } from 'react-router'
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
2016-11-07 15:03:45 +09:00
import update from 'immutability-helper';
2016-11-05 18:37:46 +09:00
const CommentsEditFormContainer = (props, context) => {
const Component = props.component;
2016-11-07 15:03:45 +09:00
return <Component {...props} />;
2016-11-05 18:37:46 +09:00
};
CommentsEditFormContainer.propTypes = {
flash: React.PropTypes.func,
novaFormMutation: React.PropTypes.func,
2016-11-07 15:03:45 +09:00
comment: React.PropTypes.object.isRequired,
2016-11-05 18:37:46 +09:00
router: React.PropTypes.object,
2016-11-07 15:03:45 +09:00
component: React.PropTypes.func,
successCallback: React.PropTypes.func,
cancelCallback: React.PropTypes.func,
2016-11-05 18:37:46 +09:00
};
const mapStateToProps = state => ({ messages: state.messages, });
const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch);
const CommentsEditFormContainerWithMutation = graphql(gql`
mutation commentsEdit($commentId: String, $set: CommentInput, $unset: CommentUnsetModifier) {
commentsEdit(commentId: $commentId, set: $set, unset: $unset) {
_id
2016-11-07 15:03:45 +09:00
body
htmlBody
2016-11-05 18:37:46 +09:00
}
}
`, {
props: ({ownProps, mutate}) => ({
novaFormMutation: ({documentId, set, unset}) => {
return mutate({
2016-11-07 15:03:45 +09:00
variables: {commentId: documentId, set, unset},
2016-11-05 18:37:46 +09:00
// optimisticResponse: {
// __typename: 'Mutation',
// postsEdit: {
// __typename: 'Post',
// ...set
// }
// },
2016-11-07 15:03:45 +09:00
updateQueries: {
getPost: (prev, { mutationResult }) => {
const editedComment = mutationResult.data.commentsEdit;
const commentIndex = Telescope.utils.findIndex(prev.post.comments, comment => comment._id = editedComment._id);
const newCommentList = _.clone(prev.post.comments);
newCommentList[commentIndex] = Object.assign(newCommentList[commentIndex], editedComment);
const newPost = update(prev, {
post: {
comments: {
$set: newCommentList
}
}
});
return newPost;
},
}
2016-11-05 18:37:46 +09:00
})
}
}),
})(CommentsEditFormContainer);
module.exports = withRouter(connect(mapStateToProps, mapDispatchToProps)(CommentsEditFormContainerWithMutation));