1
- import { Component , OnInit } from ' @angular/core' ;
2
- import { FormGroup , FormBuilder , Validators } from ' @angular/forms' ;
3
- import { DatePipe } from ' @angular/common' ;
1
+ import { DatePipe } from " @angular/common" ;
2
+ import { Component , OnDestroy , OnInit } from " @angular/core" ;
3
+ import { FormBuilder , FormGroup , Validators } from " @angular/forms" ;
4
4
5
- import { Task } from 'src/app/tasks/shared/models/task.model' ;
6
- import { TasksHttpService } from 'src/app/tasks/shared/services/tasks-http.service' ;
5
+ import { Subscription } from "rxjs" ;
6
+
7
+ import { Task } from "src/app/tasks/shared/models/task.model" ;
8
+ import { TasksHttpService } from "src/app/tasks/shared/services/tasks-http.service" ;
7
9
8
10
@Component ( {
9
- selector : ' app-add-tasks' ,
10
- templateUrl : ' ./add-tasks.component.html' ,
11
- styleUrls : [ ' ./add-tasks.component.scss' ]
11
+ selector : " app-add-tasks" ,
12
+ templateUrl : " ./add-tasks.component.html" ,
13
+ styleUrls : [ " ./add-tasks.component.scss" ] ,
12
14
} )
13
- export class AddTasksComponent implements OnInit {
14
-
15
+ export class AddTasksComponent implements OnInit , OnDestroy {
15
16
public taskForm : FormGroup ;
16
17
public isEditMode : boolean ;
17
18
public isSubmitted : boolean ;
18
- private datePipe : DatePipe = new DatePipe ( 'en-US' ) ;
19
+ private datePipe : DatePipe = new DatePipe ( "en-US" ) ;
20
+ private subscriptions : Subscription = new Subscription ( ) ;
19
21
20
22
constructor (
21
23
private formBuilder : FormBuilder ,
22
24
private tasksHttpService : TasksHttpService
23
- ) { }
25
+ ) { }
24
26
25
27
ngOnInit ( ) {
26
28
this . buildTaskForm ( ) ;
@@ -29,6 +31,10 @@ export class AddTasksComponent implements OnInit {
29
31
} ) ;
30
32
}
31
33
34
+ ngOnDestroy ( ) : void {
35
+ this . subscriptions . unsubscribe ( ) ;
36
+ }
37
+
32
38
public submitTaskForm ( ) : void {
33
39
this . isSubmitted = true ;
34
40
if ( this . taskForm . invalid ) {
@@ -39,7 +45,7 @@ export class AddTasksComponent implements OnInit {
39
45
}
40
46
41
47
public getActualDate ( ) : string {
42
- const today = this . datePipe . transform ( new Date ( ) , ' yyyy-MM-dd' ) ;
48
+ const today = this . datePipe . transform ( new Date ( ) , " yyyy-MM-dd" ) ;
43
49
return today ;
44
50
}
45
51
@@ -48,7 +54,10 @@ export class AddTasksComponent implements OnInit {
48
54
}
49
55
50
56
private addTask ( ) : void {
51
- this . taskForm . value . dueDate = this . datePipe . transform ( this . taskForm . value . dueDate , 'yyyy-MM-dd' ) ;
57
+ this . taskForm . value . dueDate = this . datePipe . transform (
58
+ this . taskForm . value . dueDate ,
59
+ "yyyy-MM-dd"
60
+ ) ;
52
61
const newTask : Task = this . taskForm . value ;
53
62
54
63
this . tasksHttpService . createTask ( newTask ) . subscribe ( ( task : Task ) => {
@@ -59,12 +68,17 @@ export class AddTasksComponent implements OnInit {
59
68
}
60
69
61
70
private updateTask ( ) : void {
62
- this . taskForm . value . dueDate = this . datePipe . transform ( this . taskForm . value . dueDate , 'yyyy-MM-dd' ) ;
63
- this . tasksHttpService . updateTask ( this . taskForm . value ) . subscribe ( ( updatedTask : Task ) => {
64
- if ( updatedTask ) {
65
- this . updateTaskList ( ) ;
66
- }
67
- } ) ;
71
+ this . taskForm . value . dueDate = this . datePipe . transform (
72
+ this . taskForm . value . dueDate ,
73
+ "yyyy-MM-dd"
74
+ ) ;
75
+ this . tasksHttpService
76
+ . updateTask ( this . taskForm . value )
77
+ . subscribe ( ( updatedTask : Task ) => {
78
+ if ( updatedTask ) {
79
+ this . updateTaskList ( ) ;
80
+ }
81
+ } ) ;
68
82
this . isEditMode = false ;
69
83
}
70
84
@@ -75,9 +89,9 @@ export class AddTasksComponent implements OnInit {
75
89
76
90
private buildTaskForm ( ) : void {
77
91
this . taskForm = this . formBuilder . group ( {
78
- id : [ '' ] ,
79
- title : [ '' , Validators . required ] ,
80
- dueDate : [ this . getActualDate ( ) ]
92
+ id : [ "" ] ,
93
+ title : [ "" , Validators . required ] ,
94
+ dueDate : [ this . getActualDate ( ) ] ,
81
95
} ) ;
82
96
}
83
97
}
0 commit comments