Browse Source

16-208 Implementing shopping list edit with forms

tags/before_ngrx
Nils Dittberner 8 years ago
parent
commit
326dae720c
5 changed files with 77 additions and 15 deletions
  1. +11
    -6
      src/app/shopping-list/shopping-edit/shopping-edit.component.html
  2. +45
    -8
      src/app/shopping-list/shopping-edit/shopping-edit.component.ts
  3. +2
    -1
      src/app/shopping-list/shopping-list.component.html
  4. +4
    -0
      src/app/shopping-list/shopping-list.component.ts
  5. +15
    -0
      src/app/shopping-list/shopping-list.service.ts

+ 11
- 6
src/app/shopping-list/shopping-edit/shopping-edit.component.html View File

@@ -1,6 +1,6 @@
<div class="row"> <div class="row">
<div class="col-xs-12"> <div class="col-xs-12">
<form>
<form (ngSubmit)="onSubmit(f)" #f="ngForm">
<div class="row"> <div class="row">
<div class="col-sm-5 form-group"> <div class="col-sm-5 form-group">
<label for="name">Name</label> <label for="name">Name</label>
@@ -8,7 +8,9 @@
type="text" type="text"
id="name" id="name"
class="form-control" class="form-control"
#nameInput>
name="name"
ngModel
required>
</div> </div>
<div class="col-sm-2 form-group"> <div class="col-sm-2 form-group">
<label for="amount">Amount</label> <label for="amount">Amount</label>
@@ -16,14 +18,17 @@
type="number" type="number"
id="amount" id="amount"
class="form-control" class="form-control"
#amountInput>
name="amount"
ngModel
required
pattern="^[1-9]+[0-9]*$">
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-xs-12"> <div class="col-xs-12">
<button class="btn btn-success" type="submit" (click)="onAddItem()">Add</button>
<button class="btn btn-danger" type="button">Delete</button>
<button class="btn btn-primary" type="button">Clear</button>
<button class="btn btn-success" type="submit" [disabled]="!f.valid">{{ editMode ? 'Update' : 'Add' }}</button>
<button *ngIf="editMode" class="btn btn-danger" type="button" (click)="onDelete()">Delete</button>
<button class="btn btn-primary" type="button" (click)="onClear()">Clear</button>
</div> </div>
</div> </div>
</form> </form>


+ 45
- 8
src/app/shopping-list/shopping-edit/shopping-edit.component.ts View File

@@ -1,4 +1,7 @@
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Subscription } from 'rxjs/Subscription';

import { Ingredient } from '../../shared/ingredient.model'; import { Ingredient } from '../../shared/ingredient.model';
import { ShoppingListService } from '../shopping-list.service'; import { ShoppingListService } from '../shopping-list.service';


@@ -7,18 +10,52 @@ import { ShoppingListService } from '../shopping-list.service';
templateUrl: './shopping-edit.component.html', templateUrl: './shopping-edit.component.html',
styleUrls: ['./shopping-edit.component.css'] styleUrls: ['./shopping-edit.component.css']
}) })
export class ShoppingEditComponent implements OnInit {
@ViewChild('nameInput') nameInputRef: ElementRef;
@ViewChild('amountInput') amountInputRef: ElementRef;
export class ShoppingEditComponent implements OnInit, OnDestroy {
@ViewChild('f') shoppingListForm: NgForm;
subscription: Subscription
editMode = false;
editedItemIndex: number;
editedItem: Ingredient;


constructor(private shoppingListService: ShoppingListService) { } constructor(private shoppingListService: ShoppingListService) { }


ngOnInit() { ngOnInit() {
this.subscription = this.shoppingListService.startedEditing.subscribe(
(index: number) => {
this.editedItemIndex = index;
this.editMode = true;
this.editedItem = this.shoppingListService.getIngredient(index);
this.shoppingListForm.setValue({
name: this.editedItem.name,
amount: this.editedItem.amount
})
}
);
}

onSubmit(form: NgForm) {
const value = form.value
const newIngredient = new Ingredient(value.name, value.amount);
if (this.editMode) {
this.shoppingListService.updateIngredient(this.editedItemIndex, newIngredient);
} else {
this.shoppingListService.addIngredient(newIngredient);
}
this.editMode = false;
form.reset();
}

onClear() {
this.shoppingListForm.reset();
this.editMode = false;
}

onDelete() {
this.shoppingListService.deleteIngredient(this.editedItemIndex);
this.onClear();
} }


onAddItem() {
const newIngredient = new Ingredient(this.nameInputRef.nativeElement.value,
this.amountInputRef.nativeElement.value);
this.shoppingListService.addIngredient(newIngredient);
ngOnDestroy() {
this.subscription.unsubscribe();
} }
} }

+ 2
- 1
src/app/shopping-list/shopping-list.component.html View File

@@ -6,7 +6,8 @@
<a <a
class="list-group-item" class="list-group-item"
style="cursor: pointer;" style="cursor: pointer;"
*ngFor="let ingredient of ingredients"
*ngFor="let ingredient of ingredients; let i = index"
(click)="onEditItem(i)"
> >
{{ ingredient.name }} ({{ ingredient.amount }}) {{ ingredient.name }} ({{ ingredient.amount }})
</a> </a>


+ 4
- 0
src/app/shopping-list/shopping-list.component.ts View File

@@ -24,6 +24,10 @@ export class ShoppingListComponent implements OnInit, OnDestroy {
); );
} }


onEditItem(index: number) {
this.shoppingListService.startedEditing.next(index);
}

ngOnDestroy() { ngOnDestroy() {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
} }


+ 15
- 0
src/app/shopping-list/shopping-list.service.ts View File

@@ -3,6 +3,7 @@ import { Subject } from "rxjs/Subject";


export class ShoppingListService { export class ShoppingListService {
ingredientsChanged = new Subject<Ingredient[]>(); ingredientsChanged = new Subject<Ingredient[]>();
startedEditing = new Subject<number>();
private ingredients: Ingredient[] = [ private ingredients: Ingredient[] = [
new Ingredient('Apples', 5), new Ingredient('Apples', 5),
new Ingredient('Tomatoes', 10) new Ingredient('Tomatoes', 10)
@@ -11,6 +12,10 @@ export class ShoppingListService {
getIngredients() { getIngredients() {
return this.ingredients.slice(); return this.ingredients.slice();
} }
getIngredient(index: number) {
return this.ingredients[index];
}


addIngredient(ingredient: Ingredient) { addIngredient(ingredient: Ingredient) {
this.ingredients.push(ingredient); this.ingredients.push(ingredient);
@@ -21,4 +26,14 @@ export class ShoppingListService {
this.ingredients.push(...ingredients); this.ingredients.push(...ingredients);
this.ingredientsChanged.next(this.ingredients.slice()); this.ingredientsChanged.next(this.ingredients.slice());
} }

updateIngredient(index: number, newIngredient: Ingredient) {
this.ingredients[index] = newIngredient;
this.ingredientsChanged.next(this.ingredients.slice());
}

deleteIngredient(index: number) {
this.ingredients.splice(index, 1);
this.ingredientsChanged.next(this.ingredients.slice());
}
} }

Loading…
Cancel
Save