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

48 lines
1.3 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';
2017-09-25 22:09:09 +02:00
import { voteOnItem } from '../modules/vote.js';
import { VoteableCollections } from '../modules/make_voteable.js';
2016-11-11 18:29:49 +09:00
const withVote = component => {
2016-11-11 18:29:49 +09:00
return graphql(gql`
2017-09-25 22:09:09 +02:00
mutation vote($documentId: String, $operationType: String, $collectionName: String) {
vote(documentId: $documentId, operationType: $operationType, collectionName: $collectionName) {
${VoteableCollections.map(collection => `
... on ${collection.typeName} {
__typename
_id
2017-09-25 22:09:09 +02:00
currentUserVotes{
_id
2017-09-25 22:09:09 +02:00
voteType
power
}
baseScore
}
`).join('\n')}
2016-11-11 18:29:49 +09:00
}
}
`, {
props: ({ownProps, mutate}) => ({
2017-09-25 22:09:09 +02:00
vote: ({document, operationType, collection, currentUser}) => {
const voteResult = voteOnItem(collection, document, currentUser, operationType, true);
2016-11-11 18:29:49 +09:00
return mutate({
variables: {
documentId: document._id,
2017-09-25 22:09:09 +02:00
operationType,
collectionName: collection._name,
},
2016-11-11 18:29:49 +09:00
optimisticResponse: {
__typename: 'Mutation',
2017-09-25 22:09:09 +02:00
vote: voteResult.document,
2016-11-11 18:29:49 +09:00
}
})
}
}),
})(component);
}
export default withVote;