MyCrypto/common/sagas/rates.js
Bryan Fillmer 2616129409 Create a basic saga pattern to handle potential network errors. (#144)
* First pass at handling API failure.

* Bity saga logic upgrade.

* Reusable response.ok logic. Small optimization to array join.

* Flow fixes.

* Streamlined some error handling, moved types.
2017-08-31 09:30:46 -07:00

29 lines
797 B
JavaScript

// @flow
import { put, call } from 'redux-saga/effects';
import { handleJSONResponse } from 'api/utils';
import { setRates } from 'actions/rates';
import { showNotification } from 'actions/notifications';
import type { Yield, Return, Next } from 'sagas/types';
const symbols = ['USD', 'EUR', 'GBP', 'BTC', 'CHF', 'REP'];
const symbolsURL = symbols.join(',');
const fetchRates = () =>
fetch(
`https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=${symbolsURL}`
).then(response =>
handleJSONResponse(response, 'Could not fetch rate data.')
);
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));
}
}