1
+ import { Injectable } from '@angular/core' ;
1
2
import { Actions , createEffect , ofType } from '@ngrx/effects' ;
3
+ import { MatSnackBar } from '@angular/material/snack-bar' ;
2
4
import { of } from 'rxjs' ;
3
- import { mergeMap , map , catchError } from 'rxjs/operators' ;
5
+ import { mergeMap , map , catchError , tap } from 'rxjs/operators' ;
4
6
5
7
import { CategoryService } from '../services/category.service' ;
6
8
import { categoriesListLoaded , loadCategoriesSuccess , loadCategoriesError , deleteCategory , deleteCategorySuccess , addCategory , addCategorySuccess , addCategoryError } from './actions' ;
7
9
10
+ @Injectable ( )
8
11
export class CategoriesEffects {
9
12
10
13
categoriesListLoaded$ = createEffect ( ( ) => this . actions$ . pipe (
@@ -15,25 +18,31 @@ export class CategoriesEffects {
15
18
) )
16
19
) ) ;
17
20
18
- deleteCategory$ = createEffect ( ( ) => this . actions$ . pipe (
19
- ofType ( deleteCategory ) ,
20
- mergeMap ( ( { payload} ) => this . categoriesService . deleteCategory ( payload ) . pipe (
21
- map ( ( ) => deleteCategorySuccess ( { payload} ) ) ,
22
- catchError ( ( ) => of ( loadCategoriesError ( ) ) ) ,
23
- ) )
24
- ) ) ;
21
+ // deleteCategory$ = createEffect(() => this.actions$.pipe(
22
+ // ofType(deleteCategory),
23
+ // mergeMap(({payload}) => this.categoriesService.deleteCategory(payload).pipe(
24
+ // map(() => deleteCategorySuccess({payload})),
25
+ // catchError(() => of(loadCategoriesError())),
26
+ // ))
27
+ // ));
25
28
26
29
addCategory$ = createEffect ( ( ) => this . actions$ . pipe (
27
30
ofType ( addCategory ) ,
28
31
mergeMap ( ( { payload} ) => this . categoriesService . addCategory ( payload ) . pipe (
29
- map ( ( result ) => addCategorySuccess ( { payload : result } ) ) ,
32
+ map ( ( result ) => addCategorySuccess ( { payload : { data : result , message : 'Category successfully added' } } ) ) ,
30
33
catchError ( ( ) => of ( addCategoryError ( ) ) ) ,
31
34
) ) ,
32
35
) ) ;
33
36
37
+ handleSuccessMessage$ = createEffect ( ( ) => this . actions$ . pipe (
38
+ ofType ( addCategorySuccess , deleteCategorySuccess ) ,
39
+ tap ( ( { payload} ) => this . snackBar . open ( payload . message , 'Dismiss' , { duration : 2000 } ) ) ,
40
+ ) ) ;
41
+
34
42
constructor (
35
43
private readonly actions$ : Actions ,
36
44
private readonly categoriesService : CategoryService ,
45
+ private readonly snackBar : MatSnackBar ,
37
46
) { }
38
47
39
48
}
0 commit comments