Skip to content

Instantly share code, notes, and snippets.

@itto-ki
Created July 11, 2018 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itto-ki/3ff31487571ea48a5a99a7ef4e839ef5 to your computer and use it in GitHub Desktop.
Save itto-ki/3ff31487571ea48a5a99a7ef4e839ef5 to your computer and use it in GitHub Desktop.
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