|
@@ -1,27 +1,39 @@
|
|
|
-import React, { createContext, useContext, useReducer, Reducer } from 'react';
|
|
|
+import React, { createContext, useContext, useReducer } from 'react';
|
|
|
|
|
|
-import { StateType } from '../store/initialState';
|
|
|
+import initialMainState, { StateType as MainStateType } from './initialMainState';
|
|
|
+import initialPdfState, { StateType as PdfStateType } from './initialPdfState';
|
|
|
+
|
|
|
+import reducers from '../reducers'
|
|
|
+import applyMiddleware from '../middleware';
|
|
|
+
|
|
|
+type StateType = MainStateType & PdfStateType
|
|
|
|
|
|
type IContextProps = [
|
|
|
StateType,
|
|
|
({type}:{type:string}) => void,
|
|
|
]
|
|
|
|
|
|
+export const initialState = {
|
|
|
+ ...initialMainState,
|
|
|
+ ...initialPdfState
|
|
|
+};
|
|
|
+
|
|
|
export const StateContext = createContext({} as IContextProps);
|
|
|
|
|
|
export const StoreProvider = ({
|
|
|
- reducer,
|
|
|
- initialState,
|
|
|
children,
|
|
|
-}: {
|
|
|
- reducer: Reducer<any, any>;
|
|
|
- initialState: StateType;
|
|
|
+} : {
|
|
|
children: React.ReactNode;
|
|
|
-}) => (
|
|
|
- <StateContext.Provider
|
|
|
- value={useReducer(reducer, initialState)}
|
|
|
- children={children}
|
|
|
- />
|
|
|
-);
|
|
|
-
|
|
|
-export const useStore = () => useContext(StateContext);
|
|
|
+}) => {
|
|
|
+ const [state, dispatch] = useReducer(reducers, initialState);
|
|
|
+ const enhancedDispatch = applyMiddleware(dispatch);
|
|
|
+
|
|
|
+ return (
|
|
|
+ <StateContext.Provider
|
|
|
+ value={[state, enhancedDispatch]}
|
|
|
+ children={children}
|
|
|
+ />
|
|
|
+ );
|
|
|
+};
|
|
|
+
|
|
|
+export default () => useContext(StateContext);
|