2018-01-06 07:07:38 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-11-22 16:15:00 +09:00
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2018-01-26 22:00:36 -06:00
|
|
|
import { getSetting, getFragment, getFragmentName, getCollection } from 'meteor/vulcan:core';
|
2016-11-22 16:15:00 +09:00
|
|
|
|
2016-12-12 09:54:16 +09:00
|
|
|
export default function withDocument (options) {
|
2017-12-28 11:30:13 +09:00
|
|
|
|
2018-01-27 12:42:12 +09:00
|
|
|
const { collectionName, pollInterval = getSetting('pollInterval', 20000), enableCache = false, extraQueries } = options,
|
2017-04-03 16:07:36 +09:00
|
|
|
queryName = options.queryName || `${collection.options.collectionName}SingleQuery`,
|
2017-02-22 18:04:59 +01:00
|
|
|
singleResolverName = collection.options.resolvers.single && collection.options.resolvers.single.name;
|
2016-11-22 16:15:00 +09:00
|
|
|
|
2018-01-26 22:00:36 -06:00
|
|
|
const collection = options.collection || getCollection(collectionName);
|
2018-01-27 12:42:12 +09:00
|
|
|
|
2017-07-29 09:26:31 +09:00
|
|
|
let fragment;
|
|
|
|
|
|
|
|
if (options.fragment) {
|
|
|
|
fragment = options.fragment;
|
|
|
|
} else if (options.fragmentName) {
|
|
|
|
fragment = getFragment(options.fragmentName);
|
|
|
|
} else {
|
2017-08-02 16:19:15 +09:00
|
|
|
fragment = getFragment(`${collection.options.collectionName}DefaultFragment`);
|
2017-07-29 09:26:31 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
const fragmentName = getFragmentName(fragment);
|
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
return graphql(gql`
|
2017-12-13 18:39:19 +09:00
|
|
|
query ${queryName}($documentId: String, $slug: String, $enableCache: Boolean) {
|
|
|
|
${singleResolverName}(documentId: $documentId, slug: $slug, enableCache: $enableCache) {
|
2017-04-04 16:28:10 +09:00
|
|
|
__typename
|
2016-11-22 16:15:00 +09:00
|
|
|
...${fragmentName}
|
|
|
|
}
|
2017-12-28 11:30:13 +09:00
|
|
|
${extraQueries || ''}
|
2016-11-22 16:15:00 +09:00
|
|
|
}
|
|
|
|
${fragment}
|
|
|
|
`, {
|
2017-01-14 08:29:51 +09:00
|
|
|
alias: 'withDocument',
|
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
options(ownProps) {
|
2017-11-01 15:37:57 +09:00
|
|
|
const graphQLOptions = {
|
2017-12-13 18:39:19 +09:00
|
|
|
variables: { documentId: ownProps.documentId, slug: ownProps.slug, enableCache },
|
2017-01-11 10:36:08 +01:00
|
|
|
pollInterval, // note: pollInterval can be set to 0 to disable polling (20s by default)
|
2016-11-22 16:15:00 +09:00
|
|
|
};
|
2017-11-01 15:37:57 +09:00
|
|
|
|
|
|
|
if (options.fetchPolicy) {
|
|
|
|
graphQLOptions.fetchPolicy = options.fetchPolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return graphQLOptions;
|
2016-11-22 16:15:00 +09:00
|
|
|
},
|
|
|
|
props: returnedProps => {
|
2018-01-25 15:03:03 -06:00
|
|
|
const { /* ownProps, */ data } = returnedProps;
|
2018-01-04 09:41:48 +09:00
|
|
|
|
2017-11-01 15:37:57 +09:00
|
|
|
const propertyName = options.propertyName || 'document';
|
2017-12-28 11:30:13 +09:00
|
|
|
const props = {
|
2017-05-03 14:42:57 +09:00
|
|
|
loading: data.loading,
|
2017-02-03 15:16:48 +09:00
|
|
|
// document: Utils.convertDates(collection, data[singleResolverName]),
|
2017-11-01 15:37:57 +09:00
|
|
|
[ propertyName ]: data[singleResolverName],
|
2016-11-23 11:07:48 +09:00
|
|
|
fragmentName,
|
|
|
|
fragment,
|
2017-12-28 11:30:13 +09:00
|
|
|
data,
|
2016-11-22 16:15:00 +09:00
|
|
|
};
|
2017-12-28 11:30:13 +09:00
|
|
|
|
2018-01-04 09:41:48 +09:00
|
|
|
if (data.error) {
|
|
|
|
// get graphQL error (see https://github.com/thebigredgeek/apollo-errors/issues/12)
|
|
|
|
props.error = data.error.graphQLErrors[0];
|
|
|
|
}
|
|
|
|
|
2017-12-28 11:30:13 +09:00
|
|
|
return props;
|
2016-11-22 16:15:00 +09:00
|
|
|
},
|
|
|
|
});
|
2016-12-21 18:14:13 +01:00
|
|
|
}
|