2017-09-25 02:06:28 +00:00
|
|
|
import throttle from 'lodash/throttle';
|
|
|
|
import { routerMiddleware } from 'react-router-redux';
|
2017-07-27 23:21:27 +00:00
|
|
|
import { INITIAL_STATE as configInitialState } from 'reducers/config';
|
|
|
|
import { INITIAL_STATE as customTokensInitialState } from 'reducers/customTokens';
|
2017-07-31 23:14:30 +00:00
|
|
|
import { INITIAL_STATE as swapInitialState } from 'reducers/swap';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { applyMiddleware, createStore } from 'redux';
|
2017-07-05 23:28:54 +00:00
|
|
|
import { composeWithDevTools } from 'redux-devtools-extension';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { createLogger } from 'redux-logger';
|
|
|
|
import createSagaMiddleware from 'redux-saga';
|
2017-09-26 03:41:11 +00:00
|
|
|
import { loadStatePropertyOrEmptyObject, saveState } from 'utils/localStorage';
|
2017-07-05 23:28:54 +00:00
|
|
|
import RootReducer from './reducers';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { State as CustomTokenState } from './reducers/customTokens';
|
|
|
|
import { State as SwapState } from './reducers/swap';
|
2017-09-26 03:41:11 +00:00
|
|
|
import promiseMiddleware from 'redux-promise-middleware';
|
2017-07-05 23:28:54 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
import sagas from './sagas';
|
|
|
|
|
2017-07-05 23:28:54 +00:00
|
|
|
const configureStore = () => {
|
|
|
|
const logger = createLogger({
|
|
|
|
collapsed: true
|
|
|
|
});
|
|
|
|
const sagaMiddleware = createSagaMiddleware();
|
2017-09-26 03:41:11 +00:00
|
|
|
const reduxPromiseMiddleWare = promiseMiddleware({
|
|
|
|
promiseTypeSuffixes: ['REQUESTED', 'SUCCEEDED', 'FAILED']
|
|
|
|
});
|
2017-07-05 23:28:54 +00:00
|
|
|
let middleware;
|
|
|
|
let store;
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
middleware = composeWithDevTools(
|
2017-09-26 03:41:11 +00:00
|
|
|
applyMiddleware(
|
|
|
|
sagaMiddleware,
|
|
|
|
logger,
|
|
|
|
reduxPromiseMiddleWare,
|
|
|
|
routerMiddleware(history as any)
|
|
|
|
)
|
2017-07-05 23:28:54 +00:00
|
|
|
);
|
|
|
|
} else {
|
2017-09-25 02:06:28 +00:00
|
|
|
middleware = applyMiddleware(
|
|
|
|
sagaMiddleware,
|
2017-09-26 03:41:11 +00:00
|
|
|
reduxPromiseMiddleWare,
|
2017-09-25 02:06:28 +00:00
|
|
|
routerMiddleware(history as any)
|
|
|
|
);
|
2017-07-05 23:28:54 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
const localSwapState = loadStatePropertyOrEmptyObject<SwapState>('swap');
|
|
|
|
const swapState =
|
|
|
|
localSwapState && localSwapState.step === 3
|
|
|
|
? {
|
|
|
|
...swapInitialState,
|
|
|
|
...localSwapState
|
|
|
|
}
|
|
|
|
: { ...swapInitialState };
|
|
|
|
|
|
|
|
const localCustomTokens = loadStatePropertyOrEmptyObject<CustomTokenState>(
|
|
|
|
'customTokens'
|
|
|
|
);
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
const persistedInitialState = {
|
2017-07-05 23:28:54 +00:00
|
|
|
config: {
|
|
|
|
...configInitialState,
|
|
|
|
...loadStatePropertyOrEmptyObject('config')
|
2017-07-13 21:02:39 +00:00
|
|
|
},
|
2017-09-25 02:06:28 +00:00
|
|
|
customTokens: localCustomTokens || customTokensInitialState,
|
2017-07-31 23:14:30 +00:00
|
|
|
// ONLY LOAD SWAP STATE FROM LOCAL STORAGE IF STEP WAS 3
|
2017-09-25 02:06:28 +00:00
|
|
|
swap: swapState
|
2017-07-05 23:28:54 +00:00
|
|
|
};
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
store = createStore(RootReducer, persistedInitialState, middleware);
|
2017-07-28 00:31:59 +00:00
|
|
|
|
|
|
|
// Add all of the sagas to the middleware
|
|
|
|
Object.keys(sagas).forEach(saga => {
|
|
|
|
sagaMiddleware.run(sagas[saga]);
|
|
|
|
});
|
2017-07-05 23:28:54 +00:00
|
|
|
|
|
|
|
store.subscribe(
|
|
|
|
throttle(() => {
|
|
|
|
saveState({
|
|
|
|
config: {
|
2017-09-25 02:53:01 +00:00
|
|
|
gasPriceGwei: store.getState().config.gasPriceGwei,
|
2017-09-20 00:47:08 +00:00
|
|
|
nodeSelection: store.getState().config.nodeSelection,
|
2017-07-05 23:28:54 +00:00
|
|
|
languageSelection: store.getState().config.languageSelection
|
2017-07-13 21:02:39 +00:00
|
|
|
},
|
2017-07-31 23:14:30 +00:00
|
|
|
swap: store.getState().swap,
|
2017-07-13 21:02:39 +00:00
|
|
|
customTokens: store.getState().customTokens
|
2017-07-05 23:28:54 +00:00
|
|
|
});
|
|
|
|
}),
|
|
|
|
1000
|
|
|
|
);
|
|
|
|
return store;
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export const configuredStore = configureStore();
|