2016-11-22 16:15:00 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2017-08-02 15:51:11 +09:00
|
|
|
import { getFragment, getFragmentName } 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) {
|
2016-12-01 12:13:27 +09:00
|
|
|
|
2017-04-03 16:07:36 +09:00
|
|
|
const { collection, pollInterval = 20000 } = options,
|
|
|
|
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
|
|
|
|
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`
|
2016-12-07 15:30:40 +09:00
|
|
|
query ${queryName}($documentId: String, $slug: String) {
|
|
|
|
${singleResolverName}(documentId: $documentId, slug: $slug) {
|
2017-04-04 16:28:10 +09:00
|
|
|
__typename
|
2016-11-22 16:15:00 +09:00
|
|
|
...${fragmentName}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${fragment}
|
|
|
|
`, {
|
2017-01-14 08:29:51 +09:00
|
|
|
alias: 'withDocument',
|
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
options(ownProps) {
|
|
|
|
return {
|
2016-12-07 15:30:40 +09:00
|
|
|
variables: { documentId: ownProps.documentId, slug: ownProps.slug },
|
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
|
|
|
};
|
|
|
|
},
|
|
|
|
props: returnedProps => {
|
|
|
|
const { ownProps, data } = returnedProps;
|
|
|
|
return {
|
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]),
|
|
|
|
document: data[singleResolverName],
|
2016-11-23 11:07:48 +09:00
|
|
|
fragmentName,
|
|
|
|
fragment,
|
2016-11-22 16:15:00 +09:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
2016-12-21 18:14:13 +01:00
|
|
|
}
|