|
1 |
| -import {Component, OnInit} from '@angular/core'; |
| 1 | +import {Component, OnDestroy, OnInit} from '@angular/core'; |
2 | 2 | import {FormBuilder, FormGroup, Validators} from '@angular/forms';
|
3 | 3 | import {Select, Store} from '@ngxs/store';
|
4 | 4 | import {ActivatedRoute, Router} from '@angular/router';
|
5 | 5 | import {TodoState} from '../states/todo.state';
|
6 | 6 | import {AddTodo, SetSelectedTodo, UpdateTodo} from '../actions/todo.action';
|
7 |
| -import {Observable} from 'rxjs'; |
| 7 | +import {Observable, Subscription} from 'rxjs'; |
8 | 8 | import {Todo} from '../models/Todo';
|
9 | 9 |
|
10 | 10 | @Component({
|
11 |
| - selector: 'app-form', |
12 |
| - templateUrl: './form.component.html', |
13 |
| - styleUrls: ['./form.component.scss'] |
| 11 | + selector: 'app-form', |
| 12 | + templateUrl: './form.component.html', |
| 13 | + styleUrls: ['./form.component.scss'] |
14 | 14 | })
|
15 |
| -export class FormComponent implements OnInit { |
16 |
| - @Select(TodoState.getSelectedTodo) selectedTodo: Observable<Todo>; |
17 |
| - todoForm: FormGroup; |
18 |
| - editTodo = false; |
| 15 | +export class FormComponent implements OnInit, OnDestroy { |
| 16 | + @Select(TodoState.getSelectedTodo) selectedTodo: Observable<Todo>; |
| 17 | + todoForm: FormGroup; |
| 18 | + editTodo = false; |
| 19 | + private formSubscription: Subscription = new Subscription(); |
19 | 20 |
|
20 |
| - constructor(private fb: FormBuilder, private store: Store, private route: ActivatedRoute, private router: Router) { |
21 |
| - this.createForm(); |
22 |
| - } |
| 21 | + constructor(private fb: FormBuilder, private store: Store, private route: ActivatedRoute, private router: Router) { |
| 22 | + this.createForm(); |
| 23 | + } |
23 | 24 |
|
24 |
| - ngOnInit() { |
25 |
| - this.selectedTodo.subscribe(todo => { |
26 |
| - if (todo) { |
27 |
| - this.todoForm.patchValue({ |
28 |
| - id: todo.id, |
29 |
| - userId: todo.userId, |
30 |
| - title: todo.title |
31 |
| - }); |
32 |
| - this.editTodo = true; |
33 |
| - } else { |
34 |
| - this.editTodo = false; |
35 |
| - } |
| 25 | + ngOnInit() { |
| 26 | + this.selectedTodo.subscribe(todo => { |
| 27 | + if (todo) { |
| 28 | + this.todoForm.patchValue({ |
| 29 | + id: todo.id, |
| 30 | + userId: todo.userId, |
| 31 | + title: todo.title |
36 | 32 | });
|
37 |
| - } |
| 33 | + this.editTodo = true; |
| 34 | + } else { |
| 35 | + this.editTodo = false; |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
38 | 39 |
|
39 |
| - createForm() { |
40 |
| - this.todoForm = this.fb.group({ |
41 |
| - id: [''], |
42 |
| - userId: ['', Validators.required], |
43 |
| - title: ['', Validators.required] |
44 |
| - }); |
45 |
| - } |
| 40 | + ngOnDestroy(): void { |
| 41 | + this.formSubscription.unsubscribe(); |
| 42 | + } |
46 | 43 |
|
47 |
| - onSubmit() { |
48 |
| - if (this.editTodo) { |
49 |
| - this.store.dispatch(new UpdateTodo(this.todoForm.value, this.todoForm.value.id)).subscribe(() => { |
50 |
| - this.clearForm(); |
51 |
| - }); |
52 |
| - } else { |
53 |
| - this.store.dispatch(new AddTodo(this.todoForm.value)).subscribe(() => { |
54 |
| - this.clearForm(); |
55 |
| - }); |
56 |
| - } |
57 |
| - } |
| 44 | + createForm() { |
| 45 | + this.todoForm = this.fb.group({ |
| 46 | + id: [''], |
| 47 | + userId: ['', Validators.required], |
| 48 | + title: ['', Validators.required] |
| 49 | + }); |
| 50 | + } |
58 | 51 |
|
59 |
| - clearForm() { |
60 |
| - this.todoForm.reset(); |
61 |
| - this.store.dispatch(new SetSelectedTodo(null)); |
| 52 | + onSubmit() { |
| 53 | + if (this.editTodo) { |
| 54 | + this.formSubscription.add( |
| 55 | + this.store.dispatch(new UpdateTodo(this.todoForm.value, this.todoForm.value.id)).subscribe(() => { |
| 56 | + this.clearForm(); |
| 57 | + }) |
| 58 | + ); |
| 59 | + } else { |
| 60 | + this.formSubscription.add( |
| 61 | + this.formSubscription = this.store.dispatch(new AddTodo(this.todoForm.value)).subscribe(() => { |
| 62 | + this.clearForm(); |
| 63 | + }) |
| 64 | + ); |
62 | 65 | }
|
| 66 | + } |
| 67 | + |
| 68 | + clearForm() { |
| 69 | + this.todoForm.reset(); |
| 70 | + this.store.dispatch(new SetSelectedTodo(null)); |
| 71 | + } |
63 | 72 | }
|
0 commit comments