2017-07-13 21:02:39 +00:00
|
|
|
// @flow
|
|
|
|
import { put, call } from 'redux-saga/effects';
|
2017-08-31 16:30:46 +00:00
|
|
|
|
|
|
|
import { handleJSONResponse } from 'api/utils';
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
import { setRates } from 'actions/rates';
|
2017-08-31 16:30:46 +00:00
|
|
|
import { showNotification } from 'actions/notifications';
|
|
|
|
|
|
|
|
import type { Yield, Return, Next } from 'sagas/types';
|
2017-07-13 21:02:39 +00:00
|
|
|
|
|
|
|
const symbols = ['USD', 'EUR', 'GBP', 'BTC', 'CHF', 'REP'];
|
2017-08-31 16:30:46 +00:00
|
|
|
const symbolsURL = symbols.join(',');
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-08-31 16:30:46 +00:00
|
|
|
const fetchRates = () =>
|
|
|
|
fetch(
|
|
|
|
`https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=${symbolsURL}`
|
|
|
|
).then(response =>
|
|
|
|
handleJSONResponse(response, 'Could not fetch rate data.')
|
|
|
|
);
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-08-31 16:30:46 +00:00
|
|
|
export default function* ratesSaga(): Generator<Yield, Return, Next> {
|
|
|
|
try {
|
|
|
|
const rates = yield call(fetchRates);
|
|
|
|
yield put(setRates(rates));
|
|
|
|
} catch (error) {
|
|
|
|
yield put(showNotification('danger', error));
|
|
|
|
}
|
2017-07-13 21:02:39 +00:00
|
|
|
}
|