Skip to content

Instantly share code, notes, and snippets.

@jtomaszewski
Last active July 9, 2019 06:38
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 jtomaszewski/ca6cefc0b92c4869a49e1c0622e1f45a to your computer and use it in GitHub Desktop.
Save jtomaszewski/ca6cefc0b92c4869a49e1c0622e1f45a to your computer and use it in GitHub Desktop.
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