Browse Source

24-342 Storing recipes via ngrx

ngrx
Nils Dittberner 8 years ago
parent
commit
66bb853828
5 changed files with 18 additions and 11 deletions
  1. +1
    -5
      src/app/core/header/header.component.ts
  2. +13
    -2
      src/app/recipes/ngrx/recipe.effects.ts
  3. +1
    -1
      src/app/recipes/ngrx/recipe.reducers.ts
  4. +1
    -1
      src/app/recipes/recipe-detail/recipe-detail.component.html
  5. +2
    -2
      src/app/shared/logging.interceptor.ts

+ 1
- 5
src/app/core/header/header.component.ts View File

@@ -22,11 +22,7 @@ export class HeaderComponent implements OnInit {
} }


onSaveData() { onSaveData() {
this.dataStorageService.storeRecipes().subscribe(
response => {
// console.log(response);
}
);
this.store.dispatch(new RecipeActions.StoreRecipes());
} }


onFetchData() { onFetchData() {


+ 13
- 2
src/app/recipes/ngrx/recipe.effects.ts View File

@@ -2,15 +2,22 @@ import { Injectable } from "@angular/core";
import { Actions, Effect } from "@ngrx/effects"; import { Actions, Effect } from "@ngrx/effects";
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap'; import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/withLatestFrom';
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'; import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http';
import { Store } from "@ngrx/store";


import * as RecipeActions from '../ngrx/recipe.actions'; import * as RecipeActions from '../ngrx/recipe.actions';
import { Recipe } from "../recipe.model"; import { Recipe } from "../recipe.model";
import * as fromRecipe from './recipe.reducers';


@Injectable() @Injectable()
export class RecipeEffects { export class RecipeEffects {
readonly baseUrl: string = 'https://my-recipe-book-cb837.firebaseio.com/'; readonly baseUrl: string = 'https://my-recipe-book-cb837.firebaseio.com/';


constructor(private actions$: Actions,
private httpClient: HttpClient,
private store: Store<fromRecipe.FeatureState>) {}

@Effect() @Effect()
recipeFetch = this.actions$.ofType(RecipeActions.FETCH_RECIPES) recipeFetch = this.actions$.ofType(RecipeActions.FETCH_RECIPES)
.switchMap((action: RecipeActions.FetchRecipes) => { .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);
});
} }

+ 1
- 1
src/app/recipes/ngrx/recipe.reducers.ts View File

@@ -12,7 +12,7 @@ export interface State {
} }


const initialState: State = { const initialState: State = {
recipes: [new Recipe("Foo", "Bar", "image.jpg", [])]
recipes: []
}; };


export function recipeReducer(state = initialState, action: RecipeActions.RecipeActions) { export function recipeReducer(state = initialState, action: RecipeActions.RecipeActions) {


+ 1
- 1
src/app/recipes/recipe-detail/recipe-detail.component.html View File

@@ -35,7 +35,7 @@
<li <li
class="list-group-item" class="list-group-item"
*ngFor="let ingredient of (recipeState | async).recipes[id].ingredients"> *ngFor="let ingredient of (recipeState | async).recipes[id].ingredients">
{{ (recipeState | async).recipes[id].name }} - {{ (recipeState | async).recipes[id].amount }}
{{ ingredient.name }} - {{ ingredient.amount }}
</li> </li>
</ul> </ul>
</div> </div>

+ 2
- 2
src/app/shared/logging.interceptor.ts View File

@@ -4,10 +4,10 @@ import 'rxjs/add/operator/do';


export class LoggingInterceptor implements HttpInterceptor { export class LoggingInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// do without consuming it!
// do() without consuming it!
return next.handle(req).do( return next.handle(req).do(
event => { event => {
console.log('Logging interceptor', event);
// console.log('Logging interceptor', event);
} }
) )
} }

Loading…
Cancel
Save