UNPKG

infrastructure-components

Version:

Infrastructure-Components configure the infrastructure of your React-App as part of your React-Components.

50 lines 1.93 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const React = __importStar(require("react")); const datalayer_libs_1 = require("./datalayer-libs"); // create empty context as default const DataLayerContext = React.createContext({}); /** * This HOC attaches the req sent to the server down to the Components - on server side only, of course! * * see hocs with context: https://itnext.io/combining-hocs-with-the-new-reacts-context-api-9d3617dccf0b * * When using the req: either check whether it is undefined or whether we run on the server --> * how to check whether running on server or in browser: https://www.npmjs.com/package/exenv */ const AttachDataLayer = (props) => { return React.createElement(DataLayerContext.Provider, { value: props.apolloClient }, props.children); }; /* getEntryListQuery("entry", { //name: "e286e212-8849-4e04-82da-173fe921a491" kind: "test" }, { name: GraphQLString, kind: GraphQLString, data: GraphQLString, additional: GraphQLString } */ /** * @param Component * @returns {function(any): any} */ function withDataLayer(Component) { return function WrapperComponent(props) { return (React.createElement(DataLayerContext.Consumer, null, value => { const entryListQuery = (entryId, dictKey) => datalayer_libs_1.getEntryListQuery(entryId, dictKey, {}); return React.createElement(Component, Object.assign({}, props, { apolloClient: value, getEntryListQuery: entryListQuery })); })); }; } exports.withDataLayer = withDataLayer; exports.default = AttachDataLayer; //# sourceMappingURL=attach-data-layer.js.map