-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.js
47 lines (39 loc) · 1.24 KB
/
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
import { applyMiddleware, createStore, combineReducers } from "redux";
import createSagaMiddleware from "redux-saga";
import { clone, mergeDeepRight } from "ramda";
import AuthReducer from "modules/Auth/redux/reducer";
import DashboardReducer from "modules/Dashboard/redux/reducer";
import { loadState, saveState } from "./storage";
import rootSaga from "./saga";
const savedState = loadState() || {};
const bindMiddleware = middleware => {
if (process.env.NODE_ENV !== "production") {
const { composeWithDevTools } = require("redux-devtools-extension");
return composeWithDevTools(applyMiddleware(...middleware));
}
return applyMiddleware(...middleware);
};
function configureStore(initialState = {}) {
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
combineReducers({
auth: AuthReducer,
dashboard: DashboardReducer
}),
mergeDeepRight(initialState, savedState),
bindMiddleware([sagaMiddleware])
);
store.sagaTask = sagaMiddleware.run(rootSaga);
store.subscribe(() => {
const state = clone(store.getState());
saveState({
auth: {
data: {
token: state.auth.data.token
}
}
});
});
return store;
}
export default configureStore;