diff --git a/src/app/components/board/board.component.ts b/src/app/components/board/board.component.ts index 81de374..04b1775 100644 --- a/src/app/components/board/board.component.ts +++ b/src/app/components/board/board.component.ts @@ -28,10 +28,6 @@ export class BoardComponent { }); } - ngOnDestroy() { - this.taskService.unsubTask(); - } - getTask(status: string) { if (this.taskService.filteredTasks.length > 0) { return this.taskService.filteredTasks.filter( diff --git a/src/app/components/summary/summary.component.ts b/src/app/components/summary/summary.component.ts index be15330..3822dbf 100644 --- a/src/app/components/summary/summary.component.ts +++ b/src/app/components/summary/summary.component.ts @@ -19,7 +19,7 @@ export class SummaryComponent { urgentTasksDate: String = ''; constructor(private taskService: TaskService) { - this.taskService.tasksLoaded.subscribe(() => { + this.taskService.subTaskList().subscribe(() => { this.updateTasksCount(); this.updateAllTasksCount(); this.updateUrgentTasksCount(); diff --git a/src/app/services/task.service.ts b/src/app/services/task.service.ts index 3aecdc6..14de255 100644 --- a/src/app/services/task.service.ts +++ b/src/app/services/task.service.ts @@ -7,31 +7,32 @@ import { updateDoc, } from '@angular/fire/firestore'; import { Task } from '../interfaces/task.interface'; +import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root', }) export class TaskService { firestore: Firestore = inject(Firestore); - tasksLoaded: EventEmitter = new EventEmitter(); allTasks: Task[] = []; filteredTasks: Task[] = []; - unsubTask; - - constructor() { - this.unsubTask = this.subTaskList(); - } + constructor() {} subTaskList() { - return onSnapshot(this.getTaskRef(), (list) => { - this.allTasks = []; - list.forEach((element) => { - const taskData = { ...(element.data() as Task), id: element.id }; - this.allTasks.push(taskData); + return new Observable((observer) => { + const unsubscribe = onSnapshot(this.getTaskRef(), (list) => { + this.allTasks = []; + list.forEach((element) => { + const taskData = { ...(element.data() as Task), id: element.id }; + this.allTasks.push(taskData); + }); + observer.next(); }); - this.tasksLoaded.emit(); + + // Cleanup function + return () => unsubscribe(); }); } @@ -56,8 +57,4 @@ export class TaskService { status: task.status, }; } - - ngOnDestroy() { - this.unsubTask; - } }