| @@ -2,6 +2,7 @@ import { BrowserModule } from '@angular/platform-browser'; | |||||
| import { NgModule } from '@angular/core'; | import { NgModule } from '@angular/core'; | ||||
| import { FormsModule, ReactiveFormsModule } from '@angular/forms'; | import { FormsModule, ReactiveFormsModule } from '@angular/forms'; | ||||
| import { HttpModule } from '@angular/http'; | import { HttpModule } from '@angular/http'; | ||||
| import { HttpClientModule } from '@angular/common/http'; | |||||
| import { AppComponent } from './app.component'; | import { AppComponent } from './app.component'; | ||||
| @@ -18,6 +19,7 @@ import { AppRoutingModule } from './app-routing.module'; | |||||
| import { RecipeStartComponent } from './recipes/recipe-start/recipe-start.component'; | import { RecipeStartComponent } from './recipes/recipe-start/recipe-start.component'; | ||||
| import { RecipeEditComponent } from './recipes/recipe-edit/recipe-edit.component'; | import { RecipeEditComponent } from './recipes/recipe-edit/recipe-edit.component'; | ||||
| import { RecipeService } from './recipes/recipe.service'; | import { RecipeService } from './recipes/recipe.service'; | ||||
| import { DataStorageService } from './shared/data-storage.service'; | |||||
| @NgModule({ | @NgModule({ | ||||
| @@ -38,10 +40,11 @@ import { RecipeService } from './recipes/recipe.service'; | |||||
| BrowserModule, | BrowserModule, | ||||
| FormsModule, | FormsModule, | ||||
| ReactiveFormsModule, | ReactiveFormsModule, | ||||
| HttpClientModule, | |||||
| HttpModule, | HttpModule, | ||||
| AppRoutingModule | AppRoutingModule | ||||
| ], | ], | ||||
| providers: [ShoppingListService, RecipeService], | |||||
| providers: [ShoppingListService, RecipeService, DataStorageService], | |||||
| bootstrap: [AppComponent] | bootstrap: [AppComponent] | ||||
| }) | }) | ||||
| export class AppModule { } | export class AppModule { } | ||||
| @@ -13,8 +13,8 @@ | |||||
| <li class="dropdown" appDropdown> | <li class="dropdown" appDropdown> | ||||
| <a style="cursor: pointer;" class="dropdown-toggle" role="button">Manage <span class="caret"></span></a> | <a style="cursor: pointer;" class="dropdown-toggle" role="button">Manage <span class="caret"></span></a> | ||||
| <ul class="dropdown-menu"> | <ul class="dropdown-menu"> | ||||
| <li><a style="cursor: pointer;">Save</a></li> | |||||
| <li><a style="cursor: pointer;">Fetch</a></li> | |||||
| <li><a style="cursor: pointer;" (click)="onSaveData()">Save</a></li> | |||||
| <li><a style="cursor: pointer;" (click)="onFetchData()">Fetch</a></li> | |||||
| </ul> | </ul> | ||||
| </li> | </li> | ||||
| </ul> | </ul> | ||||
| @@ -1,8 +1,23 @@ | |||||
| import { Component } from "@angular/core"; | import { Component } from "@angular/core"; | ||||
| import { DataStorageService } from "../shared/data-storage.service"; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-header', | selector: 'app-header', | ||||
| templateUrl: './header.component.html' | templateUrl: './header.component.html' | ||||
| }) | }) | ||||
| export class HeaderComponent { | export class HeaderComponent { | ||||
| constructor(private dataStorageService: DataStorageService) {} | |||||
| onSaveData() { | |||||
| this.dataStorageService.storeRecipes().subscribe( | |||||
| response => { | |||||
| console.log(response); | |||||
| } | |||||
| ); | |||||
| } | |||||
| onFetchData() { | |||||
| this.dataStorageService.fetchRecipes(); | |||||
| } | |||||
| } | } | ||||
| @@ -3,6 +3,7 @@ import { EventEmitter, Injectable } from "@angular/core"; | |||||
| import { Ingredient } from "../shared/ingredient.model"; | import { Ingredient } from "../shared/ingredient.model"; | ||||
| import { ShoppingListService } from "../shopping-list/shopping-list.service"; | import { ShoppingListService } from "../shopping-list/shopping-list.service"; | ||||
| import { Subject } from "rxjs/Subject"; | import { Subject } from "rxjs/Subject"; | ||||
| import { nextTick } from "q"; | |||||
| @Injectable() | @Injectable() | ||||
| export class RecipeService { | export class RecipeService { | ||||
| @@ -30,6 +31,11 @@ export class RecipeService { | |||||
| constructor(private shoppingListService: ShoppingListService) { } | constructor(private shoppingListService: ShoppingListService) { } | ||||
| replaceRecipes(recipes: Recipe[]) { | |||||
| this.recipes = recipes; | |||||
| this.recipesChanged.next(this.recipes.slice()); | |||||
| } | |||||
| getRecipes() { | getRecipes() { | ||||
| return this.recipes.slice(); | return this.recipes.slice(); | ||||
| } | } | ||||
| @@ -0,0 +1,24 @@ | |||||
| import { Injectable } from '@angular/core'; | |||||
| import { HttpClient } from '@angular/common/http' | |||||
| import { RecipeService } from '../recipes/recipe.service'; | |||||
| import { Recipe } from '../recipes/recipe.model'; | |||||
| @Injectable() | |||||
| export class DataStorageService { | |||||
| readonly baseUrl: string = 'https://my-recipe-book-cb837.firebaseio.com/'; | |||||
| constructor(private httpClient: HttpClient, private recipeService: RecipeService) {} | |||||
| storeRecipes() { | |||||
| return this.httpClient.put(this.baseUrl + 'recipes.json', this.recipeService.getRecipes()); | |||||
| } | |||||
| fetchRecipes() { | |||||
| this.httpClient.get<Recipe[]>(this.baseUrl + 'recipes.json').subscribe( | |||||
| recipes => { | |||||
| this.recipeService.replaceRecipes(recipes); | |||||
| } | |||||
| ) | |||||
| } | |||||
| } | |||||