-
Notifications
You must be signed in to change notification settings - Fork 5
/
configure-store.js
52 lines (43 loc) · 1.59 KB
/
configure-store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { call, all } from 'redux-saga/effects';
import { applyMiddleware, createStore, compose, combineReducers } from 'redux';
import { routerMiddleware, routerReducer } from 'react-router-redux';
import { createLogger } from 'redux-logger';
import createSagaMiddleware from 'redux-saga';
import { homeSaga, homeReducer } from '../modules/home';
import globalVars from '../utils/globalVars';
import * as R from 'ramda';
import createMemoryHistory from 'history/createMemoryHistory';
const reducers = {
home: homeReducer,
};
const sagas = [call(homeSaga)];
export const configureStore = (
fromServer = false,
initialState = {},
history
) => {
const appReducer = combineReducers({
routing: routerReducer,
...reducers,
});
//SSR since the server has no HTML5 push states, history must be temporarily created in memory
const historyToUse = fromServer ? createMemoryHistory() : history;
const rootSaga = function* rootSaga() {
yield all([...sagas]);
};
const sagaMiddleware = createSagaMiddleware();
let middleware = [routerMiddleware(historyToUse), sagaMiddleware];
if (process.env.NODE_ENV !== 'production') {
middleware = [...middleware, createLogger({ collapsed: true })];
}
const showDevTools = R.pathOr(false, ['SHOW_REDUX_TOOLS'])(globalVars);
const composeEnhancers =
(showDevTools && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;
const store = createStore(
(state, action) => appReducer(state, action),
initialState,
composeEnhancers(applyMiddleware(...middleware))
);
sagaMiddleware.run(rootSaga);
return { historyToUse, store };
};