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-02-12 20:43:07 +00:00
|
|
|
import { State as SwapState, INITIAL_STATE as swapInitialState } from 'reducers/swap';
|
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';
|
|
|
|
import { gasPricetoBase } from 'libs/units';
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
const localSwapState = loadStatePropertyOrEmptyObject<SwapState>('swap');
|
|
|
|
const swapState =
|
|
|
|
localSwapState && localSwapState.step === 3
|
|
|
|
? {
|
|
|
|
...swapInitialState,
|
|
|
|
...localSwapState
|
|
|
|
}
|
|
|
|
: { ...swapInitialState };
|
|
|
|
|
|
|
|
const savedTransactionState = loadStatePropertyOrEmptyObject<TransactionState>('transaction');
|
2018-03-14 20:10:14 +00:00
|
|
|
const savedTransactionsState = loadStatePropertyOrEmptyObject<TransactionsState>('transactions');
|
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,
|
|
|
|
value: gasPricetoBase(+savedTransactionState.fields.gasPrice.raw)
|
|
|
|
}
|
|
|
|
: transactionInitialState.fields.gasPrice
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// ONLY LOAD SWAP STATE FROM LOCAL STORAGE IF STEP WAS 3
|
|
|
|
swap: swapState,
|
2018-03-14 20:10:14 +00:00
|
|
|
transactions: {
|
|
|
|
...initialTransactionsState,
|
|
|
|
...savedTransactionsState
|
|
|
|
},
|
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-02-12 20:43:07 +00:00
|
|
|
...getConfigAndCustomTokensStateToSubscribe(state)
|
|
|
|
});
|
|
|
|
}, 50)
|
|
|
|
);
|
|
|
|
|
|
|
|
return store;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const configuredStore = configureStore();
|