1 | declare var require: any;
|
2 | import React, { useState } from 'react';
|
3 |
|
4 |
|
5 |
|
6 | const StorageContext = React.createContext({});
|
7 |
|
8 | export interface IAttachStorage {
|
9 | addToRenderList: (fRender, hashValue) => void,
|
10 | isOffline: Boolean | undefined
|
11 | config: any,
|
12 | children: any,
|
13 | preloadedFiles: any,
|
14 | renderListResults: any
|
15 | }
|
16 |
|
17 |
|
18 | const AttachStorage = (props: IAttachStorage) => {
|
19 |
|
20 | return <StorageContext.Provider
|
21 | value={{
|
22 | addRenderSsr: props.addToRenderList,
|
23 | config: props.config,
|
24 | isOffline: props.isOffline,
|
25 | preloadedFiles: props.preloadedFiles,
|
26 | renderListResults: props.renderListResults
|
27 | }}>{props.children}</StorageContext.Provider>
|
28 |
|
29 | };
|
30 |
|
31 |
|
32 | export const serviceWithStorage = (complementedCallback: (listFiles, cbreq, cbres, cbnext) => any) => {
|
33 |
|
34 |
|
35 | return [
|
36 | async function (req, res, next) {
|
37 | return await complementedCallback(req.listFiles, req, res, next)
|
38 | }
|
39 | ]
|
40 | };
|
41 |
|
42 |
|
43 | export const serviceAttachStorage = (listFiles) => {
|
44 | return (req, res, next) => {
|
45 |
|
46 | req.listFiles = listFiles;
|
47 | next();
|
48 | };
|
49 | }
|
50 |
|
51 | export function withStorageSsrRendering(Component) {
|
52 | return function WrapperComponent(props) {
|
53 | return (
|
54 | <StorageContext.Consumer>
|
55 | {(context: any) => {
|
56 | return <Component
|
57 | {...props}
|
58 | renderSsr={context.addRenderSsr}
|
59 | config={context.config}
|
60 | isOffline={context.isOffline}
|
61 | preloadedFiles={context.preloadedFiles}
|
62 | renderListResults={context.renderListResults}
|
63 | />
|
64 | }}
|
65 | </StorageContext.Consumer>
|
66 | );
|
67 | };
|
68 | }
|
69 |
|
70 | export default AttachStorage;
|
71 |
|
72 |
|
73 |
|
74 |
|