Skip to content

Commit 0d12d77

Browse files
committed
Unsubscribed the form subscription
1 parent 57b185f commit 0d12d77

File tree

3 files changed

+1260
-1215
lines changed

3 files changed

+1260
-1215
lines changed

src/app/form/form.component.ts

+55-46
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,72 @@
1-
import {Component, OnInit} from '@angular/core';
1+
import {Component, OnDestroy, OnInit} from '@angular/core';
22
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
33
import {Select, Store} from '@ngxs/store';
44
import {ActivatedRoute, Router} from '@angular/router';
55
import {TodoState} from '../states/todo.state';
66
import {AddTodo, SetSelectedTodo, UpdateTodo} from '../actions/todo.action';
7-
import {Observable} from 'rxjs';
7+
import {Observable, Subscription} from 'rxjs';
88
import {Todo} from '../models/Todo';
99

1010
@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']
1414
})
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();
1920

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+
}
2324

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
3632
});
37-
}
33+
this.editTodo = true;
34+
} else {
35+
this.editTodo = false;
36+
}
37+
});
38+
}
3839

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+
}
4643

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+
}
5851

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+
);
6265
}
66+
}
67+
68+
clearForm() {
69+
this.todoForm.reset();
70+
this.store.dispatch(new SetSelectedTodo(null));
71+
}
6372
}

src/app/states/todo.state.ts

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export class TodoState {
5555
@Action(UpdateTodo)
5656
updateTodo({getState, setState}: StateContext<TodoStateModel>, {payload, id}: UpdateTodo) {
5757
return this.todoService.updateTodo(payload, id).pipe(tap((result) => {
58-
console.log(result);
5958
const state = getState();
6059
const todoList = [...state.todos];
6160
const todoIndex = todoList.findIndex(item => item.id === id);

0 commit comments

Comments
 (0)