|
|
|
@@ -2,15 +2,22 @@ import { Injectable } from "@angular/core"; |
|
|
|
import { Actions, Effect } from "@ngrx/effects"; |
|
|
|
import 'rxjs/add/operator/map'; |
|
|
|
import 'rxjs/add/operator/switchMap'; |
|
|
|
import 'rxjs/add/operator/withLatestFrom'; |
|
|
|
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'; |
|
|
|
import { Store } from "@ngrx/store"; |
|
|
|
|
|
|
|
import * as RecipeActions from '../ngrx/recipe.actions'; |
|
|
|
import { Recipe } from "../recipe.model"; |
|
|
|
import * as fromRecipe from './recipe.reducers'; |
|
|
|
|
|
|
|
@Injectable() |
|
|
|
export class RecipeEffects { |
|
|
|
readonly baseUrl: string = 'https://my-recipe-book-cb837.firebaseio.com/'; |
|
|
|
|
|
|
|
constructor(private actions$: Actions, |
|
|
|
private httpClient: HttpClient, |
|
|
|
private store: Store<fromRecipe.FeatureState>) {} |
|
|
|
|
|
|
|
@Effect() |
|
|
|
recipeFetch = this.actions$.ofType(RecipeActions.FETCH_RECIPES) |
|
|
|
.switchMap((action: RecipeActions.FetchRecipes) => { |
|
|
|
@@ -23,6 +30,10 @@ export class RecipeEffects { |
|
|
|
}; |
|
|
|
}); |
|
|
|
|
|
|
|
constructor(private actions$: Actions, |
|
|
|
private httpClient: HttpClient) {} |
|
|
|
@Effect({dispatch: false}) |
|
|
|
recipeStore = this.actions$.ofType(RecipeActions.STORE_RECIPES) |
|
|
|
.withLatestFrom(this.store.select('recipes')) |
|
|
|
.switchMap(([action, state]) => { |
|
|
|
return this.httpClient.put(this.baseUrl + 'recipes.json', state.recipes); |
|
|
|
}); |
|
|
|
} |