2018-02-20 09:30:36 +00:00
|
|
|
/* @flow */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import { LOCATION_CHANGE } from 'react-router-redux';
|
|
|
|
import { httpRequest } from '../utils/networkUtils';
|
|
|
|
import { resolveAfter } from '../utils/promiseUtils';
|
|
|
|
|
|
|
|
const loadRateAction = (): any => {
|
|
|
|
return async (dispatch, getState) => {
|
2018-03-08 16:10:53 +00:00
|
|
|
const config = getState().localStorage.config;
|
2018-02-20 09:30:36 +00:00
|
|
|
try {
|
|
|
|
|
2018-03-08 16:10:53 +00:00
|
|
|
for (let i = 0; i < config.fiatValueTickers.length; i++) {
|
|
|
|
const rate = await httpRequest(`${config.fiatValueTickers[i].url}?convert=USD`, 'json');
|
|
|
|
dispatch({
|
|
|
|
type: 'rate__update',
|
|
|
|
network: config.fiatValueTickers[i].network,
|
|
|
|
rate: rate[0]
|
|
|
|
});
|
|
|
|
}
|
2018-02-20 09:30:36 +00:00
|
|
|
|
|
|
|
} catch(error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await resolveAfter(50000);
|
|
|
|
// dispatch( loadRateAction() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware
|
|
|
|
*/
|
|
|
|
const LocalStorageService = (store: any) => (next: any) => (action: any) => {
|
|
|
|
|
2018-03-08 16:10:53 +00:00
|
|
|
next(action);
|
|
|
|
|
|
|
|
//if (action.type === LOCATION_CHANGE && !store.getState().router.location) {
|
|
|
|
if (action.type === 'storage__ready') {
|
2018-02-20 09:30:36 +00:00
|
|
|
store.dispatch(loadRateAction());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default LocalStorageService;
|