1
1
import { Injectable } from '@angular/core' ;
2
2
import { Actions , Effect , ofType } from '@ngrx/effects' ;
3
+ import { Store } from '@ngrx/store' ;
3
4
import { HttpClient } from '@angular/common/http' ;
4
- import { switchMap , map } from 'rxjs/operators' ;
5
+ import { switchMap , map , withLatestFrom } from 'rxjs/operators' ;
5
6
6
7
import * as RecipesActions from './recipe.actions' ;
7
8
import { Recipe } from '../recipe.model' ;
9
+ import * as fromApp from '../../store/app.reducer' ;
8
10
9
11
@Injectable ( )
10
12
export class RecipeEffects {
@@ -29,5 +31,21 @@ export class RecipeEffects {
29
31
} )
30
32
) ;
31
33
32
- constructor ( private actions$ : Actions , private http : HttpClient ) { }
34
+ @Effect ( { dispatch : false } )
35
+ storeRecipes = this . actions$ . pipe (
36
+ ofType ( RecipesActions . STORE_RECIPES ) ,
37
+ withLatestFrom ( this . store . select ( 'recipes' ) ) ,
38
+ switchMap ( ( [ actionData , recipesState ] ) => {
39
+ return this . http . put (
40
+ 'https://ng-course-recipe-book-4ce87.firebaseio.com/recipes.json' ,
41
+ recipesState . recipes
42
+ ) ;
43
+ } )
44
+ ) ;
45
+
46
+ constructor (
47
+ private actions$ : Actions ,
48
+ private http : HttpClient ,
49
+ private store : Store < fromApp . AppState >
50
+ ) { }
33
51
}
0 commit comments