Last active
July 9, 2019 06:15
-
-
Save jtomaszewski/1a13fb5027bce5108aa033b300d04ebd to your computer and use it in GitHub Desktop.
This file contains 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
import { call, put, takeLatest } from "redux-saga/effects"; | |
// Action creator function | |
function fetchArticles(params) { | |
return { type: FETCH_ARTICLES, params }; | |
} | |
// Sagas | |
function* fetchArticlesSaga() { | |
yield takeLatest(FETCH_ARTICLES, triggerFetchArticles); | |
} | |
function* triggerFetchArticles({ params }) { | |
yield put({ type: FETCH_ARTICLES_START }); | |
try { | |
const response = yield call( | |
ArticlesService.getArticles, | |
params | |
); | |
yield put({ | |
type: FETCH_ARTICLES_SUCCESS, | |
data: response.data | |
}); | |
} catch (error) { | |
yield put({ type: FETCH_ARTICLES_FAILURE, message: error }); | |
} | |
} | |
// Configuration | |
const sagaMiddleware = createSagaMiddleware(); | |
const store = createStore( | |
reducer, | |
applyMiddleware(sagaMiddleware) | |
); | |
sagaMiddleware.run(fetchArticlesSaga); | |
// Actual usage | |
dispatch(fetchArticles(params)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment