Skip to content

Instantly share code, notes, and snippets.

@GarryBrown
Created February 26, 2018 16:50
Show Gist options
  • Save GarryBrown/7f3901fb6772393b7cc03894d803ace0 to your computer and use it in GitHub Desktop.
Save GarryBrown/7f3901fb6772393b7cc03894d803ace0 to your computer and use it in GitHub Desktop.
Simple effect to load data
import { Injectable } from '@angular/core';
import { Effect, Actions } from '@ngrx/effects';
import { of } from 'rxjs/observable/of';
import { map, switchMap, catchError } from 'rxjs/operators';
import * as fromRoot from '../../../app/store';
import * as movieActions from '../actions/movie.action';
import * as fromServices from '../../services';
@Injectable()
export class MoviesEffects {
constructor(
private actions$: Actions,
private movieService: fromServices.MoviesService
) {}
@Effect()
loadMovies$ = this.actions$.ofType(movieActions.LOAD_MOVIES).pipe(
switchMap(() => {
return this.movieService
.getMovies()
.pipe(
map(movies => new movieActions.LoadMoviesSuccess(movies)),
catchError(error => of(new movieActions.LoadMoviesFail(error)))
);
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment