Skip to content

Commit 7077de4

Browse files
author
Renante D. Entera
committed
Course Project: Wrap up NgRx store
1 parent afb1815 commit 7077de4

File tree

6 files changed

+34
-16
lines changed

6 files changed

+34
-16
lines changed

course-project/src/app/core.module.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { NgModule } from '@angular/core';
22
import { HTTP_INTERCEPTORS } from '@angular/common/http';
33

4-
import { RecipeService } from './recipes/recipe.service';
54
import { AuthInterceptorService } from './auth/auth-interceptor.service';
6-
import { LoggingService } from './logging.service';
75

86
@NgModule({
97
providers: [
10-
RecipeService,
118
{
129
provide: HTTP_INTERCEPTORS,
1310
useClass: AuthInterceptorService,

course-project/src/app/header/header.component.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { Subscription } from 'rxjs';
33
import { map } from 'rxjs/operators';
44
import { Store } from '@ngrx/store';
55

6-
import { DataStorageService } from '../shared/data-storage.service';
7-
import { AuthService } from '../auth/auth.service';
86
import * as fromApp from '../store/app.reducer';
97
import * as AuthActions from '../auth/store/auth.actions';
108
import * as RecipeActions from '../recipes/store/recipe.actions';
@@ -18,8 +16,6 @@ export class HeaderComponent implements OnInit, OnDestroy {
1816
private userSub: Subscription;
1917

2018
constructor(
21-
private dataStorageService: DataStorageService,
22-
private authService: AuthService,
2319
private store: Store<fromApp.AppState>
2420
) {}
2521

@@ -35,7 +31,8 @@ export class HeaderComponent implements OnInit, OnDestroy {
3531
}
3632

3733
onSaveData() {
38-
this.dataStorageService.storeRecipes();
34+
// this.dataStorageService.storeRecipes();
35+
this.store.dispatch(new RecipeActions.StoreRecipes());
3936
}
4037

4138
onFetchData() {

course-project/src/app/recipes/recipe-detail/recipe-detail.component.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { Store } from '@ngrx/store';
44
import { map, switchMap } from 'rxjs/operators';
55

66
import { Recipe } from '../recipe.model';
7-
import { RecipeService } from '../recipe.service';
87
import * as fromApp from '../../store/app.reducer';
98
import * as RecipesActions from '../store/recipe.actions';
9+
import * as ShoppingListActions from '../../shopping-list/store/shopping-list.actions';
1010

1111
@Component({
1212
selector: 'app-recipe-detail',
@@ -18,7 +18,6 @@ export class RecipeDetailComponent implements OnInit {
1818
id: number;
1919

2020
constructor(
21-
private recipeService: RecipeService,
2221
private route: ActivatedRoute,
2322
private router: Router,
2423
private store: Store<fromApp.AppState>
@@ -46,7 +45,10 @@ export class RecipeDetailComponent implements OnInit {
4645
}
4746

4847
onAddToShoppingList() {
49-
this.recipeService.addIngredientsToShoppingList(this.recipe.ingredients);
48+
// this.recipeService.addIngredientsToShoppingList(this.recipe.ingredients);
49+
this.store.dispatch(
50+
new ShoppingListActions.AddIngredients(this.recipe.ingredients)
51+
);
5052
}
5153

5254
onEditRecipe() {

course-project/src/app/recipes/recipe-edit/recipe-edit.component.ts

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Store } from '@ngrx/store';
55
import { map } from 'rxjs/operators';
66
import { Subscription } from 'rxjs';
77

8-
import { RecipeService } from '../recipe.service';
98
import * as fromApp from '../../store/app.reducer';
109
import * as RecipesActions from '../store/recipe.actions';
1110

@@ -27,7 +26,6 @@ export class RecipeEditComponent implements OnInit, OnDestroy {
2726

2827
constructor(
2928
private route: ActivatedRoute,
30-
private recipeService: RecipeService,
3129
private router: Router,
3230
private store: Store<fromApp.AppState>
3331
) {}

course-project/src/app/recipes/store/recipe.actions.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const FETCH_RECIPES = '[Recipes] Fetch Recipes';
77
export const ADD_RECIPE = '[Recipe] Add Recipe';
88
export const UPDATE_RECIPE = '[Recipe] Update Recipe';
99
export const DELETE_RECIPE = '[Recipe] Delete Recipe';
10+
export const STORE_RECIPES = '[Recipe] Store Recipes';
1011

1112
export class SetRecipes implements Action {
1213
readonly type = SET_RECIPES;
@@ -36,9 +37,14 @@ export class DeleteRecipe implements Action {
3637
constructor(public payload: number) {}
3738
}
3839

40+
export class StoreRecipes implements Action {
41+
readonly type = STORE_RECIPES;
42+
}
43+
3944
export type RecipesActions =
4045
| SetRecipes
4146
| FetchRecipes
4247
| AddRecipe
4348
| UpdateRecipe
44-
| DeleteRecipe;
49+
| DeleteRecipe
50+
| StoreRecipes;

course-project/src/app/recipes/store/recipe.effects.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Injectable } from '@angular/core';
22
import { Actions, Effect, ofType } from '@ngrx/effects';
3+
import { Store } from '@ngrx/store';
34
import { HttpClient } from '@angular/common/http';
4-
import { switchMap, map } from 'rxjs/operators';
5+
import { switchMap, map, withLatestFrom } from 'rxjs/operators';
56

67
import * as RecipesActions from './recipe.actions';
78
import { Recipe } from '../recipe.model';
9+
import * as fromApp from '../../store/app.reducer';
810

911
@Injectable()
1012
export class RecipeEffects {
@@ -29,5 +31,21 @@ export class RecipeEffects {
2931
})
3032
);
3133

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+
) {}
3351
}

0 commit comments

Comments
 (0)