mirror of
https://github.com/vale981/Vulcan
synced 2025-03-10 04:26:41 -04:00
40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
import React, { PropTypes, Component } from 'react';
|
|
import { graphql } from 'react-apollo';
|
|
import gql from 'graphql-tag';
|
|
import { getFragment, getFragmentName } from 'meteor/nova:core';
|
|
|
|
export default function withDocument (options) {
|
|
|
|
const { queryName, collection, pollInterval = 20000 } = options,
|
|
fragment = options.fragment || getFragment(options.fragmentName),
|
|
fragmentName = getFragmentName(fragment),
|
|
singleResolverName = collection.options.resolvers.single && collection.options.resolvers.single.name;
|
|
|
|
return graphql(gql`
|
|
query ${queryName}($documentId: String, $slug: String) {
|
|
${singleResolverName}(documentId: $documentId, slug: $slug) {
|
|
...${fragmentName}
|
|
}
|
|
}
|
|
${fragment}
|
|
`, {
|
|
alias: 'withDocument',
|
|
|
|
options(ownProps) {
|
|
return {
|
|
variables: { documentId: ownProps.documentId, slug: ownProps.slug },
|
|
pollInterval, // note: pollInterval can be set to 0 to disable polling (20s by default)
|
|
};
|
|
},
|
|
props: returnedProps => {
|
|
const { ownProps, data } = returnedProps;
|
|
return {
|
|
loading: data.networkStatus === 1,
|
|
// document: Utils.convertDates(collection, data[singleResolverName]),
|
|
document: data[singleResolverName],
|
|
fragmentName,
|
|
fragment,
|
|
};
|
|
},
|
|
});
|
|
}
|