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-06-05 12:12:04 +09:00
|
|
|
import { getSetting, getFragment, getFragmentName, getCollection, singleClientTemplate, Utils } from 'meteor/vulcan:lib';
|
2016-11-22 16:15:00 +09:00
|
|
|
|
2018-06-05 11:51:25 +09:00
|
|
|
export default function withSingle(options) {
|
|
|
|
|
|
|
|
const {
|
|
|
|
collectionName,
|
|
|
|
pollInterval = getSetting('pollInterval', 20000),
|
|
|
|
enableCache = false,
|
|
|
|
extraQueries,
|
|
|
|
} = options;
|
2016-11-22 16:15:00 +09:00
|
|
|
|
2018-01-26 22:00:36 -06:00
|
|
|
const collection = options.collection || getCollection(collectionName);
|
2018-06-05 11:51:25 +09:00
|
|
|
const typeName = collection.options.typeName;
|
2018-06-05 12:12:04 +09:00
|
|
|
const resolverName = Utils.camelCaseify(typeName);
|
2018-06-05 11:51:25 +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);
|
|
|
|
|
2018-06-05 16:07:38 +09:00
|
|
|
const query = gql`${singleClientTemplate({ typeName, fragmentName, extraQueries })}${fragment}`;
|
2018-06-05 11:51:25 +09:00
|
|
|
|
|
|
|
return graphql(query, {
|
|
|
|
alias: `with${typeName}`,
|
2018-08-30 20:02:09 +09:00
|
|
|
|
|
|
|
options({ documentId, slug, selector = { documentId, slug } }) { // OpenCrud backwards compatibility
|
2017-11-01 15:37:57 +09:00
|
|
|
const graphQLOptions = {
|
2018-06-05 11:51:25 +09:00
|
|
|
variables: {
|
2018-06-05 12:12:04 +09:00
|
|
|
input: {
|
2018-08-29 20:36:30 +09:00
|
|
|
selector,
|
2018-06-05 12:12:04 +09:00
|
|
|
enableCache,
|
|
|
|
}
|
2018-02-18 18:00:07 +09:00
|
|
|
},
|
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-06-05 11:51:25 +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]),
|
2018-06-05 12:12:04 +09:00
|
|
|
[propertyName]: data[resolverName] && data[resolverName].result,
|
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
|
|
|
}
|