UNPKG

1.93 kBJavaScriptView Raw
1"use strict";
2var __importStar = (this && this.__importStar) || function (mod) {
3 if (mod && mod.__esModule) return mod;
4 var result = {};
5 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6 result["default"] = mod;
7 return result;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10const React = __importStar(require("react"));
11const datalayer_libs_1 = require("./datalayer-libs");
12// create empty context as default
13const DataLayerContext = React.createContext({});
14/**
15 * This HOC attaches the req sent to the server down to the Components - on server side only, of course!
16 *
17 * see hocs with context: https://itnext.io/combining-hocs-with-the-new-reacts-context-api-9d3617dccf0b
18 *
19 * When using the req: either check whether it is undefined or whether we run on the server -->
20 * how to check whether running on server or in browser: https://www.npmjs.com/package/exenv
21 */
22const AttachDataLayer = (props) => {
23 return React.createElement(DataLayerContext.Provider, { value: props.apolloClient }, props.children);
24};
25/*
26getEntryListQuery("entry", {
27 //name: "e286e212-8849-4e04-82da-173fe921a491"
28 kind: "test"
29}, {
30 name: GraphQLString,
31 kind: GraphQLString,
32 data: GraphQLString,
33 additional: GraphQLString
34}
35*/
36/**
37 * @param Component
38 * @returns {function(any): any}
39 */
40function withDataLayer(Component) {
41 return function WrapperComponent(props) {
42 return (React.createElement(DataLayerContext.Consumer, null, value => {
43 const entryListQuery = (entryId, dictKey) => datalayer_libs_1.getEntryListQuery(entryId, dictKey, {});
44 return React.createElement(Component, Object.assign({}, props, { apolloClient: value, getEntryListQuery: entryListQuery }));
45 }));
46 };
47}
48exports.withDataLayer = withDataLayer;
49exports.default = AttachDataLayer;
50//# sourceMappingURL=attach-data-layer.js.map
\No newline at end of file