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-11-18 20:33:53 +00:00
|
|
|
import { State as ConfigState } from './reducers/config';
|
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-11-18 20:33:53 +00:00
|
|
|
import { getNodeConfigFromId } from 'utils/node';
|
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-11-18 20:33:53 +00:00
|
|
|
const savedConfigState = loadStatePropertyOrEmptyObject<ConfigState>(
|
|
|
|
'config'
|
|
|
|
);
|
|
|
|
|
|
|
|
// If they have a saved node, make sure we assign that too. The node selected
|
|
|
|
// isn't serializable, so we have to assign it here.
|
|
|
|
if (savedConfigState && savedConfigState.nodeSelection) {
|
|
|
|
const savedNode = getNodeConfigFromId(
|
|
|
|
savedConfigState.nodeSelection,
|
|
|
|
savedConfigState.customNodes
|
|
|
|
);
|
|
|
|
// If we couldn't find it, revert to defaults
|
|
|
|
if (savedNode) {
|
|
|
|
savedConfigState.node = savedNode;
|
|
|
|
} else {
|
|
|
|
savedConfigState.nodeSelection = configInitialState.nodeSelection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
const persistedInitialState = {
|
2017-07-05 23:28:54 +00:00
|
|
|
config: {
|
|
|
|
...configInitialState,
|
2017-11-18 20:33:53 +00:00
|
|
|
...savedConfigState
|
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-11-10 03:30:20 +00:00
|
|
|
// if 'web3' has persisted as node selection, reset to app default
|
|
|
|
// necessary because web3 is only initialized as a node upon MetaMask / Mist unlock
|
|
|
|
if (persistedInitialState.config.nodeSelection === 'web3') {
|
|
|
|
persistedInitialState.config.nodeSelection =
|
|
|
|
configInitialState.nodeSelection;
|
|
|
|
}
|
|
|
|
|
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(() => {
|
2017-11-18 20:33:53 +00:00
|
|
|
const state = store.getState();
|
2017-07-05 23:28:54 +00:00
|
|
|
saveState({
|
|
|
|
config: {
|
2017-11-18 20:33:53 +00:00
|
|
|
gasPriceGwei: state.config.gasPriceGwei,
|
|
|
|
nodeSelection: state.config.nodeSelection,
|
|
|
|
languageSelection: state.config.languageSelection,
|
|
|
|
customNodes: state.config.customNodes
|
2017-07-13 21:02:39 +00:00
|
|
|
},
|
2017-11-21 19:55:46 +00:00
|
|
|
swap: { ...state.swap, bityRates: {} },
|
2017-11-18 20:33:53 +00:00
|
|
|
customTokens: state.customTokens
|
2017-07-05 23:28:54 +00:00
|
|
|
});
|
|
|
|
}),
|
|
|
|
1000
|
|
|
|
);
|
2017-11-21 19:55:46 +00:00
|
|
|
|
2017-07-05 23:28:54 +00:00
|
|
|
return store;
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export const configuredStore = configureStore();
|