-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-movie.effects.ts
More file actions
51 lines (47 loc) · 1.42 KB
/
new-movie.effects.ts
File metadata and controls
51 lines (47 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { EMPTY, EmptyError } from 'rxjs';
import { map, mergeMap, catchError, exhaustMap } from 'rxjs/operators';
import { MovieService } from '../movies/movie.service';
import { search, addMovieToCollection } from './new-movie.actions';
import { tap } from 'rxjs/operators';
import { ToastrService } from 'ngx-toastr';
@Injectable()
export class MovieEffects {
searchMovies$ = createEffect(() =>
this.actions$.pipe(
ofType(search),
mergeMap((action) =>
this.moviesService.findMovie(action.payload).pipe(
map((movies) => ({
type: '[New Movie Page] Search Completed',
payload: movies.results,
})),
catchError(() => {
this.toastr.warning('Unable to fetch movies from source');
return EMPTY;
})
)
)
)
);
addMovieToCollection$ = createEffect(
() =>
this.actions$.pipe(
ofType(addMovieToCollection),
tap((action) => {
this.moviesService.addToCollection(action.payload);
this.toastr.success(
action.payload.title + ' added to collection.',
'Movie Added'
);
})
),
{ dispatch: false }
);
constructor(
private actions$: Actions,
private moviesService: MovieService,
private toastr: ToastrService
) {}
}