2016-11-11 18:29:49 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2016-12-12 16:43:23 +09:00
|
|
|
import { operateOnItem } from '../vote.js';
|
2016-11-11 18:29:49 +09:00
|
|
|
|
2017-01-09 15:42:24 +09:00
|
|
|
const withVote = component => {
|
|
|
|
|
2016-11-11 18:29:49 +09:00
|
|
|
return graphql(gql`
|
2017-01-09 15:42:24 +09:00
|
|
|
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
|
|
|
|
}
|
2017-01-09 15:42:24 +09:00
|
|
|
downvotes
|
|
|
|
downvoters {
|
|
|
|
_id
|
|
|
|
}
|
|
|
|
baseScore
|
2016-11-11 18:29:49 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`, {
|
|
|
|
props: ({ownProps, mutate}) => ({
|
2017-01-09 15:42:24 +09:00
|
|
|
vote: ({document, voteType, collection, currentUser}) => {
|
|
|
|
const voteResult = operateOnItem(collection, document, currentUser, voteType, true);
|
2016-11-11 18:29:49 +09:00
|
|
|
return mutate({
|
2017-01-09 15:42:24 +09:00
|
|
|
variables: {
|
|
|
|
documentId: document._id,
|
|
|
|
voteType,
|
|
|
|
collectionName: collection._name,
|
|
|
|
},
|
2016-11-11 18:29:49 +09:00
|
|
|
optimisticResponse: {
|
|
|
|
__typename: 'Mutation',
|
2017-01-09 15:42:24 +09:00
|
|
|
vote: {
|
|
|
|
...voteResult,
|
2016-11-11 18:29:49 +09:00
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
})(component);
|
2017-01-09 15:42:24 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export default withVote;
|