11import { History } from 'history'
22import { routerMiddleware , routerReducer } from 'react-router-redux'
3- import {
4- applyMiddleware ,
5- combineReducers ,
6- compose ,
7- createStore as _createStore ,
8- Store ,
9- StoreEnhancer
10- } from 'redux'
3+ import { applyMiddleware , compose , createStore as _createStore , Store , StoreEnhancer } from 'redux'
4+ import { persistCombineReducers , PersistConfig , persistStore } from 'redux-persist'
5+ import { createFilter } from 'redux-persist-transform-filter'
6+ import storage from 'redux-persist/lib/storage' // defaults to localStorage
117import createSagaMiddleware from 'redux-saga'
128
139import reducers from './reducers'
14- import { IState } from './reducers/states'
10+ import { IApplicationState , IPlaygroundState , ISessionState , IState } from './reducers/states'
1511import mainSaga from './sagas'
1612import { history as appHistory } from './utils/history'
1713
1814declare var __REDUX_DEVTOOLS_EXTENSION_COMPOSE__ : ( ) => StoreEnhancer < IState >
1915
20- function createStore ( history : History ) : Store < IState > {
16+ type IPersistState = Pick < IApplicationState , 'environment' > &
17+ Pick < IPlaygroundState , 'editorValue' > &
18+ Pick < ISessionState , 'historyHelper' > &
19+ Pick < ISessionState , 'token' > &
20+ Pick < ISessionState , 'username' >
21+
22+ function createStore ( history : History ) {
2123 let composeEnhancers : any = compose
2224 const sagaMiddleware = createSagaMiddleware ( )
2325 const middleware = [ sagaMiddleware , routerMiddleware ( history ) ]
@@ -26,15 +28,32 @@ function createStore(history: History): Store<IState> {
2628 composeEnhancers = __REDUX_DEVTOOLS_EXTENSION_COMPOSE__
2729 }
2830
29- const rootReducer = combineReducers ( {
31+ const transforms = [
32+ createFilter < IState , IPersistState > ( 'application' , [ 'environment' ] ) ,
33+ createFilter < IState , IPersistState > ( 'playground' , [ 'editorValue' ] ) ,
34+ createFilter < IState , IPersistState > ( 'session' , [ 'token' , 'username' , 'historyHelper' ] )
35+ ]
36+
37+ const persistConfig : PersistConfig = {
38+ key : 'root' ,
39+ storage,
40+ transforms : [ ...transforms ]
41+ }
42+
43+ const persistedReducer = persistCombineReducers < IState > ( persistConfig , {
3044 ...reducers ,
3145 router : routerReducer
3246 } )
33- const enchancers = composeEnhancers ( applyMiddleware ( ...middleware ) )
34- const createdStore = _createStore ( rootReducer , enchancers )
3547
48+ const enchancers = composeEnhancers ( applyMiddleware ( ...middleware ) )
49+ const createdStore = _createStore ( persistedReducer , enchancers ) as Store < IState >
3650 sagaMiddleware . run ( mainSaga )
3751 return createdStore
3852}
3953
40- export const store = createStore ( appHistory ) as Store < IState >
54+ function createPersistor ( createdStore : Store < IState > ) {
55+ return persistStore ( createdStore )
56+ }
57+
58+ export const store = createStore ( appHistory )
59+ export const persistor = createPersistor ( store )
0 commit comments