Vulcan/packages/nova-voting/lib/containers/withVote.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-11-11 18:29:49 +09:00
import React, { PropTypes, Component } from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import { operateOnItem } from '../vote.js';
2016-11-11 18:29:49 +09:00
const withVote = component => {
2016-11-11 18:29:49 +09:00
return graphql(gql`
mutation vote($documentId: String, $voteType: String, $collectionName: String) {
vote(documentId: $documentId, voteType: $voteType, collectionName: $collectionName) {
2016-11-11 18:29:49 +09:00
_id
upvotes
upvoters {
_id
}
downvotes
downvoters {
_id
}
baseScore
2016-11-11 18:29:49 +09:00
}
}
`, {
props: ({ownProps, mutate}) => ({
vote: ({document, voteType, collection, currentUser}) => {
const voteResult = operateOnItem(collection, document, currentUser, voteType, true);
2016-11-11 18:29:49 +09:00
return mutate({
variables: {
documentId: document._id,
voteType,
collectionName: collection._name,
},
2016-11-11 18:29:49 +09:00
optimisticResponse: {
__typename: 'Mutation',
vote: {
...voteResult,
2016-11-11 18:29:49 +09:00
},
}
})
}
}),
})(component);
}
export default withVote;