|
1 | 1 | import { applyMiddleware, combineReducers, createStore } from 'redux' |
2 | | -import { bindActionCreators } from 'redux' |
3 | | -import thunk from 'redux-thunk' |
| 2 | +import { bindActionCreatorsToStore } from 'redux-module-builder'; |
| 3 | +import { bindActionCreators, compose } from 'redux' |
| 4 | +import thunkMiddleware from 'redux-thunk' |
| 5 | +import {persistStore, autoRehydrate} from 'redux-persist' |
| 6 | +import {AsyncStorage} from 'react-native' |
4 | 7 |
|
5 | 8 | import Firestack from 'react-native-firestack' |
6 | 9 | import env from '../../config/environment' |
7 | 10 |
|
8 | | -import * as currentUser from './modules/currentUser' |
9 | | -import * as navigation from './modules/navigation' |
| 11 | +import { rootReducer, actions, initialState } from './rootReducer'; |
10 | 12 |
|
11 | 13 | export const configureStore = (userInitialState = {}) => { |
12 | | - const middleware = applyMiddleware(thunk); |
| 14 | + let middleware = [ |
| 15 | + thunkMiddleware, |
| 16 | + ] |
| 17 | + |
| 18 | + let tools = []; |
| 19 | + if (process.env.NODE_ENV === 'development') { |
| 20 | + // const devTools = require('remote-redux-devtools'); |
| 21 | + // tools.push(devTools()); |
| 22 | + } |
| 23 | + |
| 24 | + tools.push(autoRehydrate()) |
13 | 25 |
|
14 | 26 | const firestack = new Firestack(env.firestack) |
15 | 27 |
|
16 | | - const rootReducer = combineReducers({ |
17 | | - user: currentUser.reducer, |
18 | | - navigation: navigation.reducer, |
19 | | - firestack: (state) => firestack |
20 | | - }); |
| 28 | + let finalCreateStore; |
| 29 | + finalCreateStore = compose( |
| 30 | + applyMiddleware(...middleware), |
| 31 | + ...tools |
| 32 | + )(createStore); |
21 | 33 |
|
22 | | - let initialState = |
23 | | - Object.assign({}, { |
24 | | - navigation: navigation.initialState |
25 | | - }) |
| 34 | + const finalInitialState = Object.assign({}, |
| 35 | + initialState, |
| 36 | + userInitialState, |
| 37 | + {firestack} |
| 38 | + ); |
26 | 39 |
|
27 | | - const store = createStore( |
| 40 | + const store = finalCreateStore( |
28 | 41 | rootReducer, |
29 | | - initialState, |
30 | | - middleware); |
| 42 | + finalInitialState |
| 43 | + ); |
| 44 | + |
| 45 | + const persistor = persistStore(store, { |
| 46 | + storage: AsyncStorage, |
| 47 | + blacklist: ['firestack'] |
| 48 | + }); |
31 | 49 |
|
32 | 50 | firestack.setStore(store); |
33 | 51 |
|
34 | | - const dispatch = store.dispatch; |
35 | | - const actions = { |
36 | | - currentUser: bindActionCreators(currentUser.actions, dispatch), |
37 | | - navigation: bindActionCreators(navigation.actions, dispatch), |
| 52 | + if (module.hot) { |
| 53 | + module.hot.accept('./rootReducer', () => { |
| 54 | + const {rootReducer} = require('./rootReducer').default; |
| 55 | + store.replaceReducer(rootReducer); |
| 56 | + }); |
38 | 57 | } |
39 | 58 |
|
40 | | - return {store,actions} |
| 59 | + const boundActions = bindActionCreatorsToStore(actions, store); |
| 60 | + return { |
| 61 | + store, |
| 62 | + actions: boundActions |
| 63 | + } |
41 | 64 | } |
42 | 65 |
|
43 | 66 | export default configureStore |
0 commit comments