Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import { push} from 'connected-react-router'
import { call, put, takeEvery } from 'redux-saga/effects'
import { getUrl, postUrl } from './api'
import {
ARTICLES_FETCH_REQUESTED,
ARTICLES_FETCH_SUCCEEDED,
ARTICLES_FETCH_FAILED,
ARTICLE_FETCH_REQUESTED,
ARTICLE_FETCH_SUCCEEDED,
ARTICLE_FETCH_FAILED,
ARTICLE_CREATE_REQUESTED,
ARTICLE_CREATE_FAILED
} from '../actions/constants'
function* fetchArticles(action) {
try {
const articles = yield call(
getUrl,
'http://localhost:8080/articles');
yield put({type: ARTICLES_FETCH_SUCCEEDED, articles: articles});
} catch(e) {
yield put({type: ARTICLES_FETCH_FAILED, hasError: e.message});
}
}
function* fetchArticle(action) {
try {
const article = yield call(
getUrl,
'http://localhost:8080/articles/' + action.articleId);
yield put({type: ARTICLE_FETCH_SUCCEEDED, article: article});
} catch(e) {
yield put({type: ARTICLE_FETCH_FAILED, hasError: e.message});
}
}
function* createArticle(action) {
try {
yield call(
postUrl,
'http://localhost:8080/articles/',
action.articleTitle,
action.articleContent);
yield put(push('/'))
} catch(e) {
yield put({type: ARTICLE_CREATE_FAILED, hasError: e.message});
}
}
function* apiSaga() {
yield takeEvery(ARTICLES_FETCH_REQUESTED, fetchArticles)
yield takeEvery(ARTICLE_FETCH_REQUESTED, fetchArticle)
yield takeEvery(ARTICLE_CREATE_REQUESTED, createArticle)
}
export default apiSaga;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment