Created
December 21, 2018 13:03
-
-
Save PavelDemyanenko/2987dc6d69462a8960860b33a2ff577c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://habr.com/post/351168/ | |
Basic saga flow: | |
// sagas/index.js | |
export default function* rootSaga() { | |
yield fork(dataSaga) | |
} | |
// index.js | |
import rootSaga from './sagas' | |
import createSagaMiddleware from 'redux-saga' | |
const sagaMiddleware = createSagaMiddleware() | |
sagaMiddleware.run(rootSaga) | |
// sagas/dataSaga.js | |
import { call, put, takeEvery } from 'redux-saga/effects' | |
import { | |
FETCH_DATA_ENTITIES, | |
fetchDataEntitiesFailure, | |
fetchDataEntitiesSuccess, | |
} from '../actions/entities/data-entities-actions' | |
import apiRoutes from '../constants/api-routes' | |
import apiSaga from './api-saga' | |
function* fetchDataEntitiesSaga({ payload }) { | |
const { id, queryParams } = payload | |
try { | |
const { content } = yield call(apiSaga, { | |
endpoint: `${apiRoutes.DATA}`, | |
}) | |
const successPayload = { | |
entities: content, | |
} | |
yield put(fetchDataEntitiesSuccess(successPayload)) | |
} catch (e) { | |
const errorPayload = { | |
id, | |
error: e.message, | |
} | |
yield put(fetchDataEntitiesFailure(errorPayload)) | |
} | |
} | |
export default function* dataEntitySaga() { | |
yield takeEvery(FETCH_DATA_ENTITIES.request, fetchDataEntitiesSaga) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment