2016-11-11 18:29:49 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2017-09-04 20:28:50 +09:00
|
|
|
import { operateOnItem } from '../modules/vote.js';
|
2017-09-04 20:56:03 +09:00
|
|
|
import { VoteableCollections } from '../modules/make_voteable.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) {
|
2017-09-04 20:56:03 +09:00
|
|
|
${VoteableCollections.map(collection => `
|
|
|
|
... on ${collection.typeName} {
|
|
|
|
__typename
|
2017-01-29 11:17:00 +09:00
|
|
|
_id
|
2017-09-04 20:56:03 +09:00
|
|
|
upvotes
|
|
|
|
upvoters {
|
|
|
|
_id
|
|
|
|
}
|
|
|
|
downvotes
|
|
|
|
downvoters {
|
|
|
|
_id
|
|
|
|
}
|
|
|
|
baseScore
|
2017-01-29 11:17:00 +09:00
|
|
|
}
|
2017-09-04 20:56:03 +09:00
|
|
|
`).join('\n')}
|
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;
|