UNPKG

5.63 kBTypeScriptView Raw
1import {createApolloClient, getGraphqlUrl} from "./datalayer-integration";
2declare var require: any;
3import * as React from 'react';
4
5import { getEntryListQuery } from '../datalayer/datalayer-libs'
6
7// create empty context as default
8const DataLayerContext = React.createContext({});
9
10interface AttachDataLayerProps {
11 apolloClient?: any,
12 dataLayer: any
13}
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: React.SFC<AttachDataLayerProps> = (props) => {
23
24 //console.log("AttachDataLayer: ", props.dataLayer);
25 return <DataLayerContext.Provider
26 value={{
27 apolloClient: props.apolloClient,
28 dataLayer: props.dataLayer
29 }}>{props.children}</DataLayerContext.Provider>
30
31
32};
33
34/**
35 * @param Component
36 * @returns {function(any): any}
37 */
38export function withDataLayer(Component) {
39 return function WrapperComponent(props) {
40 return (
41 <DataLayerContext.Consumer>
42 {(context: any) => {
43 //console.log("value of context: ", context);
44 const entryListQuery = (entryId, dictKey) => {
45
46 //console.log("attach datalayer layer: ", context.dataLayer["isOffline"])
47 return context.dataLayer.getEntryListQuery(
48 entryId,
49 dictKey
50 );
51
52 };
53
54 const getEntryQuery = (entryId, dictKey) => {
55
56 return context.dataLayer.getEntryQuery(
57 entryId,
58 dictKey
59 );
60
61 };
62
63 const setEntryMutation = (entryId, values) => {
64
65 //console.log("attach datalayer layer: ", context.dataLayer["isOffline"])
66 return context.dataLayer.setEntryMutation(
67 entryId,
68 values
69 );
70 }
71
72 const getEntryScanQuery = (entryId, dictKey) => {
73
74 return context.dataLayer.getEntryScanQuery(
75 entryId,
76 dictKey
77 );
78
79 };
80
81 const deleteEntryMutation = (entryId, values) => {
82 return context.dataLayer.deleteEntryMutation(
83 entryId,
84 values
85 );
86 }
87
88 const createSetMutation = (useMutation, entryId, values) => {
89 const { mutation } = setEntryMutation(entryId, values);
90
91 const [f, { data }] = useMutation(mutation, {
92 context: context,
93 client: context.apolloClient
94 });
95
96 return f;
97 };
98
99 const createDeleteMutation = (useMutation, entryId, values) => {
100 const { mutation } = deleteEntryMutation(entryId, values);
101
102 const [f, { data }] = useMutation(mutation, {
103 context: context,
104 client: context.apolloClient
105 });
106
107 return f;
108 };
109
110 const createQuery = (useQuery, {query}) => {
111 return useQuery(query, {
112 context: context,
113 client: context.apolloClient
114 })
115 }
116
117 //console.log("entryListQuery: ", entryListQuery);
118
119 return <Component
120 {...props}
121 apolloClient={context.apolloClient}
122 getEntryListQuery={entryListQuery}
123 getEntryQuery={getEntryQuery}
124 setEntryMutation={setEntryMutation}
125 getEntryScanQuery={getEntryScanQuery}
126 deleteEntryMutation={deleteEntryMutation}
127 createSetMutation={createSetMutation}
128 createDeleteMutation={createDeleteMutation}
129 createQuery={createQuery}
130 />
131 }}
132 </DataLayerContext.Consumer>
133 );
134 };
135}
136
137
138export const serviceWithDataLayer = (complementedCallback: (cbdataLayer, cbreq, cbres, cbnext) => any) => {
139
140 // we return an array of valid middleware-callbacks
141 return [
142 async function (req, res, next) {
143 return await complementedCallback(req.dataLayer, req, res, next)
144 }
145 ]
146};
147
148export const serviceAttachDataLayer = (dataLayer) => {
149 return (req, res, next) => {
150
151 const client = createApolloClient(dataLayer, getGraphqlUrl(dataLayer.isOffline), req);
152 dataLayer.setClient(client);
153
154 //console.log("attaching the dataLayer, client: ", client);
155
156 req.dataLayer = dataLayer;
157 req.dataLayer.client = client;
158 next();
159 };
160}
161
162export default AttachDataLayer;
163
164
165
166