2018-02-12 20:43:07 +00:00
|
|
|
import throttle from 'lodash/throttle';
|
|
|
|
import { routerMiddleware } from 'react-router-redux';
|
|
|
|
import {
|
|
|
|
INITIAL_STATE as transactionInitialState,
|
|
|
|
State as TransactionState
|
|
|
|
} from 'reducers/transaction';
|
2018-03-14 20:10:14 +00:00
|
|
|
import {
|
|
|
|
INITIAL_STATE as initialTransactionsState,
|
|
|
|
State as TransactionsState
|
|
|
|
} from 'reducers/transactions';
|
2018-04-14 19:10:21 +00:00
|
|
|
import { State as SwapState, INITIAL_STATE as initialSwapState } from 'reducers/swap';
|
|
|
|
import { State as WalletState, INITIAL_STATE as initialWalletState } from 'reducers/wallet';
|
2018-03-07 23:36:05 +00:00
|
|
|
import { applyMiddleware, createStore, Store } from 'redux';
|
2018-02-12 20:43:07 +00:00
|
|
|
import { composeWithDevTools } from 'redux-devtools-extension';
|
|
|
|
import { createLogger } from 'redux-logger';
|
|
|
|
import createSagaMiddleware from 'redux-saga';
|
|
|
|
import { loadStatePropertyOrEmptyObject, saveState } from 'utils/localStorage';
|
|
|
|
import RootReducer, { AppState } from 'reducers';
|
|
|
|
import sagas from 'sagas';
|
2018-04-14 22:21:33 +00:00
|
|
|
import { gasPriceToBase } from 'libs/units';
|
2018-02-12 20:43:07 +00:00
|
|
|
import {
|
|
|
|
rehydrateConfigAndCustomTokenState,
|
|
|
|
getConfigAndCustomTokensStateToSubscribe
|
|
|
|
} from './configAndTokens';
|
|
|
|
|
|
|
|
const configureStore = () => {
|
|
|
|
const logger = createLogger({
|
|
|
|
collapsed: true
|
|
|
|
});
|
|
|
|
const sagaMiddleware = createSagaMiddleware();
|
|
|
|
let middleware;
|
2018-03-07 23:36:05 +00:00
|
|
|
let store: Store<AppState>;
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
middleware = composeWithDevTools(
|
|
|
|
applyMiddleware(sagaMiddleware, logger, routerMiddleware(history as any))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
middleware = applyMiddleware(sagaMiddleware, routerMiddleware(history as any));
|
|
|
|
}
|
|
|
|
|
2018-04-14 19:10:21 +00:00
|
|
|
// ONLY LOAD SWAP STATE FROM LOCAL STORAGE IF STEP WAS 3
|
2018-02-12 20:43:07 +00:00
|
|
|
const localSwapState = loadStatePropertyOrEmptyObject<SwapState>('swap');
|
|
|
|
const swapState =
|
|
|
|
localSwapState && localSwapState.step === 3
|
|
|
|
? {
|
2018-04-14 19:10:21 +00:00
|
|
|
...initialSwapState,
|
2018-02-12 20:43:07 +00:00
|
|
|
...localSwapState
|
|
|
|
}
|
2018-04-14 19:10:21 +00:00
|
|
|
: { ...initialSwapState };
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
|
|
const savedTransactionState = loadStatePropertyOrEmptyObject<TransactionState>('transaction');
|
2018-03-14 20:10:14 +00:00
|
|
|
const savedTransactionsState = loadStatePropertyOrEmptyObject<TransactionsState>('transactions');
|
2018-04-14 19:10:21 +00:00
|
|
|
const savedWalletState = loadStatePropertyOrEmptyObject<WalletState>('wallet');
|
2018-02-12 20:43:07 +00:00
|
|
|
|
2018-03-07 23:36:05 +00:00
|
|
|
const persistedInitialState: Partial<AppState> = {
|
2018-02-12 20:43:07 +00:00
|
|
|
transaction: {
|
|
|
|
...transactionInitialState,
|
|
|
|
fields: {
|
|
|
|
...transactionInitialState.fields,
|
|
|
|
gasPrice:
|
|
|
|
savedTransactionState && savedTransactionState.fields.gasPrice
|
|
|
|
? {
|
|
|
|
raw: savedTransactionState.fields.gasPrice.raw,
|
2018-04-14 22:21:33 +00:00
|
|
|
value: gasPriceToBase(+savedTransactionState.fields.gasPrice.raw)
|
2018-02-12 20:43:07 +00:00
|
|
|
}
|
|
|
|
: transactionInitialState.fields.gasPrice
|
|
|
|
}
|
|
|
|
},
|
|
|
|
swap: swapState,
|
2018-03-14 20:10:14 +00:00
|
|
|
transactions: {
|
|
|
|
...initialTransactionsState,
|
|
|
|
...savedTransactionsState
|
|
|
|
},
|
2018-04-14 19:10:21 +00:00
|
|
|
wallet: {
|
|
|
|
...initialWalletState,
|
|
|
|
...savedWalletState
|
|
|
|
},
|
2018-02-12 20:43:07 +00:00
|
|
|
...rehydrateConfigAndCustomTokenState()
|
|
|
|
};
|
|
|
|
|
2018-03-07 23:36:05 +00:00
|
|
|
store = createStore<AppState>(RootReducer, persistedInitialState as any, middleware);
|
2018-02-12 20:43:07 +00:00
|
|
|
|
|
|
|
// Add all of the sagas to the middleware
|
2018-03-07 23:36:05 +00:00
|
|
|
Object.keys(sagas).forEach((saga: keyof typeof sagas) => {
|
2018-02-12 20:43:07 +00:00
|
|
|
sagaMiddleware.run(sagas[saga]);
|
|
|
|
});
|
|
|
|
|
|
|
|
store.subscribe(
|
|
|
|
throttle(() => {
|
|
|
|
const state: AppState = store.getState();
|
2018-03-14 20:10:14 +00:00
|
|
|
|
2018-02-12 20:43:07 +00:00
|
|
|
saveState({
|
|
|
|
transaction: {
|
|
|
|
fields: {
|
|
|
|
gasPrice: state.transaction.fields.gasPrice
|
|
|
|
}
|
|
|
|
},
|
|
|
|
swap: {
|
|
|
|
...state.swap,
|
|
|
|
options: {
|
|
|
|
byId: {},
|
|
|
|
allIds: []
|
|
|
|
},
|
|
|
|
bityRates: {
|
|
|
|
byId: {},
|
|
|
|
allIds: []
|
|
|
|
},
|
|
|
|
shapeshiftRates: {
|
|
|
|
byId: {},
|
|
|
|
allIds: []
|
|
|
|
}
|
|
|
|
},
|
2018-03-14 20:10:14 +00:00
|
|
|
transactions: {
|
|
|
|
recent: state.transactions.recent
|
|
|
|
},
|
2018-04-14 19:10:21 +00:00
|
|
|
wallet: {
|
|
|
|
recentAddresses: state.wallet.recentAddresses
|
|
|
|
},
|
2018-02-12 20:43:07 +00:00
|
|
|
...getConfigAndCustomTokensStateToSubscribe(state)
|
|
|
|
});
|
|
|
|
}, 50)
|
|
|
|
);
|
|
|
|
|
|
|
|
return store;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const configuredStore = configureStore();
|