Skip to content

Instantly share code, notes, and snippets.

@acr13
Created September 8, 2017 20:20
Show Gist options
  • Save acr13/69c57a9aec03fcc66c6a9f3742a180f6 to your computer and use it in GitHub Desktop.
Save acr13/69c57a9aec03fcc66c6a9f3742a180f6 to your computer and use it in GitHub Desktop.
import { call, put, select, takeEvery } from 'redux-saga/effects';
import {
API_BUTTON_CLICK,
API_BUTTON_CLICK_SUCCESS,
API_BUTTON_CLICK_ERROR,
} from './actions/consts';
import { getDataFromAPI } from './api';
import { getUserAccessToken } from './selectors';
export function* apiSideEffect(action) {
try {
const accessToken = yield select(getUserAccessToken);
if (!accessToken) {
throw new Error('No access token - aborting');
}
const data = yield call(getDataFromAPI, accessToken);
yield put({ type: API_BUTTON_CLICK_SUCCESS, payload: data });
} catch (e) {
yield put({ type: API_BUTTON_CLICK_ERROR, payload: e });
}
}
// the 'watcher' - on every 'API_BUTTON_CLICK' action, run our side effect
export function* apiSaga() {
yield takeEvery(API_BUTTON_CLICK, apiSideEffect);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment