-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathwithQueryContainer.js
executable file
·51 lines (44 loc) · 1.55 KB
/
withQueryContainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React from 'react';
import PropTypes from 'prop-types';
import {Query, NamedQuery} from 'meteor/cultofcoders:grapher-react';
import getDisplayName from './getDisplayName';
import {withTracker} from 'meteor/react-meteor-data';
const propTypes = {
grapher: PropTypes.shape({
isLoading: PropTypes.bool.isRequired,
error: PropTypes.object,
data: PropTypes.array,
query: PropTypes.oneOfType([
PropTypes.instanceOf(Query),
PropTypes.instanceOf(NamedQuery),
])
}).isRequired,
config: PropTypes.object.isRequired,
props: PropTypes.object,
};
export default function withQueryContainer(WrappedComponent) {
let GrapherQueryContainer = function({grapher, config, query, props}) {
const {isLoading, error, data} = grapher;
if (error && config.errorComponent) {
return React.createElement(config.errorComponent, {
error,
query,
})
}
if (isLoading && config.loadingComponent) {
return React.createElement(config.loadingComponent, {
query,
})
}
return React.createElement(WrappedComponent, {
...props,
isLoading: error ? false : isLoading,
error,
[config.dataProp]: config.single ? data[0] : data,
query
})
};
GrapherQueryContainer.propTypes = propTypes;
GrapherQueryContainer.displayName = `GrapherQuery(${getDisplayName(WrappedComponent)})`;
return GrapherQueryContainer;
}