1 | import {createApolloClient, getGraphqlUrl} from "./datalayer-integration";
|
2 | declare var require: any;
|
3 | import * as React from 'react';
|
4 |
|
5 | import { getEntryListQuery } from '../datalayer/datalayer-libs'
|
6 |
|
7 |
|
8 | const DataLayerContext = React.createContext({});
|
9 |
|
10 | interface AttachDataLayerProps {
|
11 | apolloClient?: any,
|
12 | dataLayer: any
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const AttachDataLayer: React.SFC<AttachDataLayerProps> = (props) => {
|
23 |
|
24 |
|
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 |
|
36 |
|
37 |
|
38 | export function withDataLayer(Component) {
|
39 | return function WrapperComponent(props) {
|
40 | return (
|
41 | <DataLayerContext.Consumer>
|
42 | {(context: any) => {
|
43 |
|
44 | const entryListQuery = (entryId, dictKey) => {
|
45 |
|
46 |
|
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 |
|
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 |
|
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 |
|
138 | export const serviceWithDataLayer = (complementedCallback: (cbdataLayer, cbreq, cbres, cbnext) => any) => {
|
139 |
|
140 |
|
141 | return [
|
142 | async function (req, res, next) {
|
143 | return await complementedCallback(req.dataLayer, req, res, next)
|
144 | }
|
145 | ]
|
146 | };
|
147 |
|
148 | export const serviceAttachDataLayer = (dataLayer) => {
|
149 | return (req, res, next) => {
|
150 |
|
151 | const client = createApolloClient(dataLayer, getGraphqlUrl(dataLayer.isOffline), req);
|
152 | dataLayer.setClient(client);
|
153 |
|
154 |
|
155 |
|
156 | req.dataLayer = dataLayer;
|
157 | req.dataLayer.client = client;
|
158 | next();
|
159 | };
|
160 | }
|
161 |
|
162 | export default AttachDataLayer;
|
163 |
|
164 |
|
165 |
|
166 |
|