Created
February 26, 2018 16:50
-
-
Save GarryBrown/7f3901fb6772393b7cc03894d803ace0 to your computer and use it in GitHub Desktop.
Simple effect to load data
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 { 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