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

48 lines
1.4 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 { performVoteClient } from '../modules/vote.js';
import { VoteableCollections } from '../modules/make_voteable.js';
2016-11-11 18:29:49 +09:00
2017-09-27 17:15:49 +02:00
export const withVote = component => {
2016-11-11 18:29:49 +09:00
return graphql(gql`
mutation vote($documentId: String, $voteType: String, $collectionName: String, $voteId: String) {
vote(documentId: $documentId, voteType: $voteType, collectionName: $collectionName, voteId: $voteId) {
${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
2017-09-30 08:37:15 +09:00
score
}
`).join('\n')}
2016-11-11 18:29:49 +09:00
}
}
`, {
props: ({ownProps, mutate}) => ({
vote: ({document, voteType, collection, currentUser, voteId = Random.id()}) => {
2017-09-25 22:09:09 +02:00
const newDocument = performVoteClient({collection, document, user: currentUser, voteType, voteId});
2017-09-25 22:09:09 +02:00
2016-11-11 18:29:49 +09:00
return mutate({
variables: {
documentId: document._id,
voteType,
2017-09-27 17:15:49 +02:00
collectionName: collection.options.collectionName,
voteId,
},
2016-11-11 18:29:49 +09:00
optimisticResponse: {
__typename: 'Mutation',
2017-09-27 17:15:49 +02:00
vote: newDocument,
2016-11-11 18:29:49 +09:00
}
})
}
}),
})(component);
2017-09-27 17:15:49 +02:00
}