Last active
July 9, 2019 06:38
-
-
Save jtomaszewski/ca6cefc0b92c4869a49e1c0622e1f45a 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
import Observable from 'rxjs/observable'; | |
// Action creator function | |
function fetchArticles(params) { | |
return { type: FETCH_ARTICLES, params }; | |
} | |
// Epic | |
const fetchArticlesEpic = action$ => { | |
return action$ | |
.ofType(FETCH_ARTICLES) | |
.switchMap(action => { | |
return Observable.concat( | |
Observable.of({ type: FETCH_ARTICLES_START }), | |
Observable.fromPromise( | |
ArticlesService.getArticles(action.params) | |
.then( | |
user => ({ type: FETCH_ARTICLES_SUCCESS, user }), | |
error => ({ type: FETCH_ARTICLES_FAILURE, error }) | |
) | |
) | |
); | |
}); | |
} | |
// Configuration | |
const epicMiddleware = createEpicMiddleware(fetchArticlesEpic); | |
const store = createStore( | |
reducer, | |
applyMiddleware(epicMiddleware) | |
); | |
// Actual usage | |
dispatch(fetchArticles(params)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment