From 884c6a00b92b7b3b4d128f5a697bcd62d5c2ef13 Mon Sep 17 00:00:00 2001 From: Sacha Greif Date: Fri, 11 Nov 2016 18:29:49 +0900 Subject: [PATCH] withVoteMutation --- .../nova-base-components/lib/common/Vote.jsx | 11 ++- packages/nova-base-containers/lib/client.js | 4 +- .../lib/containers/UsersSingleContainer.jsx | 80 +++++++-------- .../lib/containers/VoteContainer.jsx | 98 +++++++++---------- .../lib/containers/withVoteMutation.js | 0 .../lib/mutations/withVoteMutation.js | 38 +++++++ packages/nova-base-containers/lib/server.js | 4 +- 7 files changed, 142 insertions(+), 93 deletions(-) delete mode 100644 packages/nova-base-containers/lib/containers/withVoteMutation.js create mode 100644 packages/nova-base-containers/lib/mutations/withVoteMutation.js diff --git a/packages/nova-base-components/lib/common/Vote.jsx b/packages/nova-base-components/lib/common/Vote.jsx index 470026eb5..7c2d0318b 100644 --- a/packages/nova-base-components/lib/common/Vote.jsx +++ b/packages/nova-base-components/lib/common/Vote.jsx @@ -2,6 +2,9 @@ import Telescope from 'meteor/nova:lib'; import React, { PropTypes, Component } from 'react'; import classNames from 'classnames'; import Users from 'meteor/nova:users'; +import { bindActionCreators } from 'redux'; +import { connect } from 'react-redux'; +import { withVoteMutation } from 'meteor/nova:base-containers'; class Vote extends Component { @@ -79,5 +82,9 @@ Vote.contextTypes = { events: React.PropTypes.object, }; -module.exports = Vote; -export default Vote; \ No newline at end of file +// redux state + actions for messages +const mapStateToProps = state => ({ messages: state.messages }); +const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch); + +module.exports = connect(mapStateToProps, mapDispatchToProps)(withVoteMutation(Vote)); +export default connect(mapStateToProps, mapDispatchToProps)(withVoteMutation(Vote)); \ No newline at end of file diff --git a/packages/nova-base-containers/lib/client.js b/packages/nova-base-containers/lib/client.js index 4362c02c6..5b7aec29d 100644 --- a/packages/nova-base-containers/lib/client.js +++ b/packages/nova-base-containers/lib/client.js @@ -5,4 +5,6 @@ import withPostsSingle from './containers/withPostsSingle.js'; import withCommentsList from './containers/withCommentsList.js'; import withUsersSingle from './containers/withUsersSingle.js'; -export {withPostsList, withPostsSingle, withCommentsList, withUsersSingle} \ No newline at end of file +import withVoteMutation from './mutations/withVoteMutation.js'; + +export { withPostsList, withPostsSingle, withCommentsList, withUsersSingle, withVoteMutation } \ No newline at end of file diff --git a/packages/nova-base-containers/lib/containers/UsersSingleContainer.jsx b/packages/nova-base-containers/lib/containers/UsersSingleContainer.jsx index 47f7745e6..79c4e74a4 100644 --- a/packages/nova-base-containers/lib/containers/UsersSingleContainer.jsx +++ b/packages/nova-base-containers/lib/containers/UsersSingleContainer.jsx @@ -1,49 +1,49 @@ -import Telescope from 'meteor/nova:lib'; -import React from 'react'; -import Posts from "meteor/nova:posts"; -import Users from "meteor/nova:users"; +// import Telescope from 'meteor/nova:lib'; +// import React from 'react'; +// import Posts from "meteor/nova:posts"; +// import Users from "meteor/nova:users"; -import { graphql } from 'react-apollo'; -import gql from 'graphql-tag'; +// import { graphql } from 'react-apollo'; +// import gql from 'graphql-tag'; -const UsersSingleContainer = (props, context) => { +// const UsersSingleContainer = (props, context) => { - const {loading, user} = props.data; - const Component = props.component +// const {loading, user} = props.data; +// const Component = props.component - return loading ? : ; -}; +// return loading ? : ; +// }; -UsersSingleContainer.propTypes = { - data: React.PropTypes.shape({ - loading: React.PropTypes.bool, - user: React.PropTypes.object, - }).isRequired, - params: React.PropTypes.object -}; +// UsersSingleContainer.propTypes = { +// data: React.PropTypes.shape({ +// loading: React.PropTypes.bool, +// user: React.PropTypes.object, +// }).isRequired, +// params: React.PropTypes.object +// }; -UsersSingleContainer.contextTypes = { - currentUser: React.PropTypes.object -}; +// UsersSingleContainer.contextTypes = { +// currentUser: React.PropTypes.object +// }; -UsersSingleContainer.displayName = "UsersSingleContainer"; +// UsersSingleContainer.displayName = "UsersSingleContainer"; -const UsersSingleContainerWithData = graphql(gql` - query getUser($userId: String, $slug: String) { - user(_id: $userId, slug: $slug) { - ${Users.graphQLQueries.single} - } - } - `, { - options(ownProps) { - return { - variables: { userId: ownProps.userId, slug: ownProps.slug }, - // pollInterval: 20000, - }; - }, -})(UsersSingleContainer); +// const UsersSingleContainerWithData = graphql(gql` +// query getUser($userId: String, $slug: String) { +// user(_id: $userId, slug: $slug) { +// ${Users.graphQLQueries.single} +// } +// } +// `, { +// options(ownProps) { +// return { +// variables: { userId: ownProps.userId, slug: ownProps.slug }, +// // pollInterval: 20000, +// }; +// }, +// })(UsersSingleContainer); -module.exports = UsersSingleContainerWithData; \ No newline at end of file +// module.exports = UsersSingleContainerWithData; \ No newline at end of file diff --git a/packages/nova-base-containers/lib/containers/VoteContainer.jsx b/packages/nova-base-containers/lib/containers/VoteContainer.jsx index 805bebb99..cd1bdae89 100644 --- a/packages/nova-base-containers/lib/containers/VoteContainer.jsx +++ b/packages/nova-base-containers/lib/containers/VoteContainer.jsx @@ -1,53 +1,53 @@ -import Telescope from 'meteor/nova:lib'; -import React, { PropTypes, Component } from 'react'; -import classNames from 'classnames'; -import { bindActionCreators } from 'redux'; -import { connect } from 'react-redux'; -import { graphql } from 'react-apollo'; -import gql from 'graphql-tag'; +// import Telescope from 'meteor/nova:lib'; +// import React, { PropTypes, Component } from 'react'; +// import classNames from 'classnames'; +// import { bindActionCreators } from 'redux'; +// import { connect } from 'react-redux'; +// import { graphql } from 'react-apollo'; +// import gql from 'graphql-tag'; -class VoteContainer extends Component { - render() { - const Component = this.props.component; - return - } -} +// class VoteContainer extends Component { +// render() { +// const Component = this.props.component; +// return +// } +// } -const VoteContainerWithMutation = graphql(gql` - mutation postsVote($documentId: String, $voteType: String) { - postsVote(documentId: $documentId, voteType: $voteType) { - _id - baseScore - downvotes - downvoters { - _id - } - upvotes - upvoters { - _id - } - } - } -`, { - props: ({ownProps, mutate}) => ({ - vote: ({post, voteType, currentUser}) => { - const votedItem = Telescope.operateOnItem(Posts, post, currentUser, voteType, true); - return mutate({ - variables: {documentId: post._id, voteType}, - optimisticResponse: { - __typename: 'Mutation', - postsVote: { - ...votedItem, - }, - } - }) - } - }), -})(VoteContainer); +// const VoteContainerWithMutation = graphql(gql` +// mutation postsVote($documentId: String, $voteType: String) { +// postsVote(documentId: $documentId, voteType: $voteType) { +// _id +// baseScore +// downvotes +// downvoters { +// _id +// } +// upvotes +// upvoters { +// _id +// } +// } +// } +// `, { +// props: ({ownProps, mutate}) => ({ +// vote: ({post, voteType, currentUser}) => { +// const votedItem = Telescope.operateOnItem(Posts, post, currentUser, voteType, true); +// return mutate({ +// variables: {documentId: post._id, voteType}, +// optimisticResponse: { +// __typename: 'Mutation', +// postsVote: { +// ...votedItem, +// }, +// } +// }) +// } +// }), +// })(VoteContainer); -// redux state + actions for messages -const mapStateToProps = state => ({ messages: state.messages }); -const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch); +// // redux state + actions for messages +// const mapStateToProps = state => ({ messages: state.messages }); +// const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch); -module.exports = connect(mapStateToProps, mapDispatchToProps)(VoteContainerWithMutation); -export default connect(mapStateToProps, mapDispatchToProps)(VoteContainerWithMutation); \ No newline at end of file +// module.exports = connect(mapStateToProps, mapDispatchToProps)(VoteContainerWithMutation); +// export default connect(mapStateToProps, mapDispatchToProps)(VoteContainerWithMutation); \ No newline at end of file diff --git a/packages/nova-base-containers/lib/containers/withVoteMutation.js b/packages/nova-base-containers/lib/containers/withVoteMutation.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/packages/nova-base-containers/lib/mutations/withVoteMutation.js b/packages/nova-base-containers/lib/mutations/withVoteMutation.js new file mode 100644 index 000000000..0f15fd808 --- /dev/null +++ b/packages/nova-base-containers/lib/mutations/withVoteMutation.js @@ -0,0 +1,38 @@ +import Telescope from 'meteor/nova:lib'; +import React, { PropTypes, Component } from 'react'; +import { graphql } from 'react-apollo'; +import gql from 'graphql-tag'; + +export default function withVoteMutation (component, options) { + return graphql(gql` + mutation postsVote($documentId: String, $voteType: String) { + postsVote(documentId: $documentId, voteType: $voteType) { + _id + baseScore + downvotes + downvoters { + _id + } + upvotes + upvoters { + _id + } + } + } + `, { + props: ({ownProps, mutate}) => ({ + vote: ({post, voteType, currentUser}) => { + const votedItem = Telescope.operateOnItem(Posts, post, currentUser, voteType, true); + return mutate({ + variables: {documentId: post._id, voteType}, + optimisticResponse: { + __typename: 'Mutation', + postsVote: { + ...votedItem, + }, + } + }) + } + }), + })(component); +} \ No newline at end of file diff --git a/packages/nova-base-containers/lib/server.js b/packages/nova-base-containers/lib/server.js index 4362c02c6..5b7aec29d 100644 --- a/packages/nova-base-containers/lib/server.js +++ b/packages/nova-base-containers/lib/server.js @@ -5,4 +5,6 @@ import withPostsSingle from './containers/withPostsSingle.js'; import withCommentsList from './containers/withCommentsList.js'; import withUsersSingle from './containers/withUsersSingle.js'; -export {withPostsList, withPostsSingle, withCommentsList, withUsersSingle} \ No newline at end of file +import withVoteMutation from './mutations/withVoteMutation.js'; + +export { withPostsList, withPostsSingle, withCommentsList, withUsersSingle, withVoteMutation } \ No newline at end of file