2018-01-06 07:11:49 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-11-11 18:29:49 +09:00
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2017-09-28 18:15:04 +09:00
|
|
|
import { performVoteClient } 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-09-27 17:15:49 +02:00
|
|
|
export const withVote = component => {
|
2017-01-09 15:42:24 +09:00
|
|
|
|
2016-11-11 18:29:49 +09:00
|
|
|
return graphql(gql`
|
2017-09-28 18:15:04 +09:00
|
|
|
mutation vote($documentId: String, $voteType: String, $collectionName: String, $voteId: String) {
|
|
|
|
vote(documentId: $documentId, voteType: $voteType, collectionName: $collectionName, voteId: $voteId) {
|
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-25 22:09:09 +02:00
|
|
|
currentUserVotes{
|
2017-09-04 20:56:03 +09:00
|
|
|
_id
|
2017-09-25 22:09:09 +02:00
|
|
|
voteType
|
|
|
|
power
|
2017-09-04 20:56:03 +09:00
|
|
|
}
|
|
|
|
baseScore
|
2017-09-30 08:37:15 +09:00
|
|
|
score
|
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-09-28 18:15:04 +09:00
|
|
|
vote: ({document, voteType, collection, currentUser, voteId = Random.id()}) => {
|
2017-09-25 22:09:09 +02:00
|
|
|
|
2017-09-28 18:15:04 +09: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({
|
2017-01-09 15:42:24 +09:00
|
|
|
variables: {
|
|
|
|
documentId: document._id,
|
2017-09-28 18:15:04 +09:00
|
|
|
voteType,
|
2017-09-27 17:15:49 +02:00
|
|
|
collectionName: collection.options.collectionName,
|
|
|
|
voteId,
|
2017-01-09 15:42:24 +09:00
|
|
|
},
|
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
|
|
|
}
|