From da9beb1a6d154069c5822b7756046f8ad6eeb6da Mon Sep 17 00:00:00 2001 From: JakubWalkowiak Date: Tue, 8 Feb 2022 23:53:26 +0100 Subject: [PATCH] added reactive forms --- .../todo-item-details.component.html | 24 +++++++++++-------- .../todo-item-details.component.ts | 19 ++++++++++++--- todo-app/todo-app/src/app/todo/todo.module.ts | 7 ++++-- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/todo-app/todo-app/src/app/todo/components/todo-item-details/todo-item-details.component.html b/todo-app/todo-app/src/app/todo/components/todo-item-details/todo-item-details.component.html index 883aec5..4ba337c 100644 --- a/todo-app/todo-app/src/app/todo/components/todo-item-details/todo-item-details.component.html +++ b/todo-app/todo-app/src/app/todo/components/todo-item-details/todo-item-details.component.html @@ -1,21 +1,25 @@
Back
-
+
-
-
- - -
+
+ + + Task name + + + + + Description + + +
-
+
- - Loading data... - diff --git a/todo-app/todo-app/src/app/todo/components/todo-item-details/todo-item-details.component.ts b/todo-app/todo-app/src/app/todo/components/todo-item-details/todo-item-details.component.ts index 02f4ac0..b2eb9d1 100644 --- a/todo-app/todo-app/src/app/todo/components/todo-item-details/todo-item-details.component.ts +++ b/todo-app/todo-app/src/app/todo/components/todo-item-details/todo-item-details.component.ts @@ -1,3 +1,4 @@ +import { FormGroup, FormControl } from '@angular/forms'; import { TodoItemDetailsInterface } from './../../interfaces/todo-item-details.interface'; import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { TodoListService } from './../../services/todo-list.service'; @@ -10,16 +11,26 @@ import { ActivatedRoute, Router } from '@angular/router'; }) export class TodoItemDetailsComponent implements OnInit { todoItem: TodoItemDetailsInterface; + todoItemForm: FormGroup; + id: number; constructor(private todoListService: TodoListService, private activatedRoute: ActivatedRoute, private router: Router) { } ngOnInit(): void { - const id = this.activatedRoute.snapshot.params['id']; - if (id) { - this.todoListService.getTodoItemById(id).subscribe(data => { + this.todoItemForm = new FormGroup({ + taskName: new FormControl(''), + description: new FormControl('') + }); + this.id = this.activatedRoute.snapshot.params['id']; + if (this.id) { + this.todoListService.getTodoItemById(this.id).subscribe(data => { this.todoItem = data; + this.todoItemForm = new FormGroup({ + taskName: new FormControl(this.todoItem.name), + description: new FormControl(this.todoItem.description) + }); }); } else { this.todoItem = { @@ -31,6 +42,8 @@ export class TodoItemDetailsComponent implements OnInit { } onSave(): void { + this.todoItem.name = this.todoItemForm.controls['taskName'].value; + this.todoItem.description = this.todoItemForm.controls['description'].value; if (this.todoItem.id) { this.todoListService.putTodoItem(this.todoItem).subscribe(() => { this.router.navigateByUrl('/todo/list'); diff --git a/todo-app/todo-app/src/app/todo/todo.module.ts b/todo-app/todo-app/src/app/todo/todo.module.ts index fa44c9f..9016f37 100644 --- a/todo-app/todo-app/src/app/todo/todo.module.ts +++ b/todo-app/todo-app/src/app/todo/todo.module.ts @@ -1,7 +1,8 @@ +import { MatInputModule } from '@angular/material/input'; import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; -import { FormsModule } from '@angular/forms'; +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { TodoListComponent } from './components/todo-list/todo-list.component'; import { TodoItemComponent } from './components/todo-item/todo-item.component'; import { TodoItemDetailsComponent } from './components/todo-item-details/todo-item-details.component'; @@ -17,7 +18,9 @@ import { TodoRoutingModule } from './todo-routing.module'; CommonModule, TodoRoutingModule, NgbModule, - FormsModule + FormsModule, + ReactiveFormsModule, + MatInputModule ] }) export class TodoModule { }