2017-09-25 02:06:28 +00:00
|
|
|
import throttle from 'lodash/throttle';
|
|
|
|
import { routerMiddleware } from 'react-router-redux';
|
2017-12-11 17:44:53 +00:00
|
|
|
import { State as ConfigState, INITIAL_STATE as configInitialState } from 'reducers/config';
|
|
|
|
import {
|
|
|
|
State as CustomTokenState,
|
|
|
|
INITIAL_STATE as customTokensInitialState
|
|
|
|
} from 'reducers/customTokens';
|
|
|
|
import { State as SwapState, 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';
|
2018-02-05 19:40:33 +00:00
|
|
|
import RootReducer, { AppState } from './reducers';
|
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';
|
2018-01-09 20:13:14 +00:00
|
|
|
import { getNetworkConfigFromId } from 'utils/network';
|
2018-01-11 06:50:31 +00:00
|
|
|
import { dedupeCustomTokens } from 'utils/tokens';
|
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 };
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
const savedConfigState = loadStatePropertyOrEmptyObject<ConfigState>('config');
|
2017-11-18 20:33:53 +00:00
|
|
|
|
|
|
|
// 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;
|
2018-01-09 20:13:14 +00:00
|
|
|
const network = getNetworkConfigFromId(savedNode.network, savedConfigState.customNetworks);
|
|
|
|
if (network) {
|
|
|
|
savedConfigState.network = network;
|
|
|
|
}
|
2017-11-18 20:33:53 +00:00
|
|
|
} else {
|
|
|
|
savedConfigState.nodeSelection = configInitialState.nodeSelection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 06:50:31 +00:00
|
|
|
// Dedupe custom tokens initially
|
|
|
|
const savedCustomTokensState =
|
|
|
|
loadStatePropertyOrEmptyObject<CustomTokenState>('customTokens') || customTokensInitialState;
|
|
|
|
const initialNetwork =
|
|
|
|
(savedConfigState && savedConfigState.network) || configInitialState.network;
|
|
|
|
const customTokens = dedupeCustomTokens(initialNetwork.tokens, savedCustomTokensState);
|
|
|
|
|
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
|
|
|
},
|
2018-01-11 06:50:31 +00:00
|
|
|
customTokens,
|
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') {
|
2017-12-11 17:44:53 +00:00
|
|
|
persistedInitialState.config.nodeSelection = configInitialState.nodeSelection;
|
2017-11-10 03:30:20 +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(() => {
|
2018-02-05 19:40:33 +00:00
|
|
|
const state: AppState = store.getState();
|
2017-07-05 23:28:54 +00:00
|
|
|
saveState({
|
|
|
|
config: {
|
2017-11-18 20:33:53 +00:00
|
|
|
nodeSelection: state.config.nodeSelection,
|
|
|
|
languageSelection: state.config.languageSelection,
|
2017-12-01 16:09:51 +00:00
|
|
|
customNodes: state.config.customNodes,
|
2018-02-05 19:40:33 +00:00
|
|
|
customNetworks: state.config.customNetworks
|
2017-07-13 21:02:39 +00:00
|
|
|
},
|
2017-12-11 17:44:53 +00:00
|
|
|
swap: {
|
|
|
|
...state.swap,
|
2018-01-02 18:04:50 +00:00
|
|
|
options: {
|
|
|
|
byId: {},
|
|
|
|
allIds: []
|
|
|
|
},
|
|
|
|
bityRates: {
|
|
|
|
byId: {},
|
|
|
|
allIds: []
|
|
|
|
},
|
|
|
|
shapeshiftRates: {
|
|
|
|
byId: {},
|
|
|
|
allIds: []
|
|
|
|
}
|
2017-12-11 17:44:53 +00:00
|
|
|
},
|
2017-11-18 20:33:53 +00:00
|
|
|
customTokens: state.customTokens
|
2017-07-05 23:28:54 +00:00
|
|
|
});
|
2017-12-20 02:39:17 +00:00
|
|
|
}, 50)
|
2017-07-05 23:28:54 +00:00
|
|
|
);
|
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();
|