feat: add TaskUpdateService to handle task updates and optimize task state management
This commit is contained in:
parent
72ca03086d
commit
13a087cfb0
4 changed files with 31 additions and 1 deletions
|
|
@ -14,6 +14,7 @@ import { AuthService } from '../../services/auth.service';
|
|||
import { firstValueFrom, map } from 'rxjs';
|
||||
import { TaskService } from '../../services/task.service';
|
||||
import { ApiService } from '../../services/api.service';
|
||||
import { TaskUpdateService } from '../../services/task-update.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-add-task',
|
||||
|
|
@ -51,6 +52,7 @@ export class AddTaskComponent implements OnInit {
|
|||
private taskService: TaskService,
|
||||
private apiService: ApiService,
|
||||
private authService: AuthService,
|
||||
private taskUpdateService: TaskUpdateService,
|
||||
private route: ActivatedRoute,
|
||||
private router: Router
|
||||
) {}
|
||||
|
|
@ -377,6 +379,7 @@ export class AddTaskComponent implements OnInit {
|
|||
|
||||
this.apiService.saveNewTask(taskWithoutId).subscribe({
|
||||
next: (response) => {
|
||||
this.taskUpdateService.notifyTaskUpdate();
|
||||
this.removeTaskData(ngForm);
|
||||
this.closeOverlay();
|
||||
this.router.navigate(['/board']);
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import { Task } from '../../interfaces/task.interface';
|
|||
import { TaskService } from '../../services/task.service';
|
||||
import { LoadingSpinnerComponent } from '../../shared/components/loading-spinner/loading-spinner.component';
|
||||
import { finalize, Subject, takeUntil } from 'rxjs';
|
||||
import { TaskUpdateService } from '../../services/task-update.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-board',
|
||||
|
|
@ -44,6 +45,7 @@ export class BoardComponent {
|
|||
private sharedService: SharedService,
|
||||
private taskService: TaskService,
|
||||
private apiService: ApiService,
|
||||
private taskUpdateService: TaskUpdateService,
|
||||
private router: Router
|
||||
) {}
|
||||
|
||||
|
|
@ -58,6 +60,7 @@ export class BoardComponent {
|
|||
|
||||
ngOnInit() {
|
||||
this.loadAllTasks();
|
||||
this.subscribeToTaskUpdates();
|
||||
this.subscribeToDragDropEvents();
|
||||
}
|
||||
|
||||
|
|
@ -89,6 +92,14 @@ export class BoardComponent {
|
|||
}, {} as { [key: string]: Task[] });
|
||||
}
|
||||
|
||||
private subscribeToTaskUpdates() {
|
||||
this.taskUpdateService.taskUpdated$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(() => {
|
||||
this.loadAllTasks();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes to events from the DragDropService and handles them.
|
||||
* @remarks
|
||||
|
|
|
|||
14
src/app/services/task-update.service.ts
Normal file
14
src/app/services/task-update.service.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { Subject } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class TaskUpdateService {
|
||||
private taskUpdatedSubject = new Subject<void>();
|
||||
taskUpdated$ = this.taskUpdatedSubject.asObservable();
|
||||
|
||||
notifyTaskUpdate() {
|
||||
this.taskUpdatedSubject.next();
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ import { TaskService } from '../../../../services/task.service';
|
|||
import { AuthService } from '../../../../services/auth.service';
|
||||
import { map } from 'rxjs';
|
||||
import { ApiService } from '../../../../services/api.service';
|
||||
import { TaskUpdateService } from '../../../../services/task-update.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-task-overlay',
|
||||
|
|
@ -33,6 +34,7 @@ export class TaskOverlayComponent implements OnInit {
|
|||
private taskService: TaskService,
|
||||
private authService: AuthService,
|
||||
private apiService: ApiService,
|
||||
private taskUpdateService: TaskUpdateService,
|
||||
private router: Router,
|
||||
private route: ActivatedRoute
|
||||
) {}
|
||||
|
|
@ -121,7 +123,7 @@ export class TaskOverlayComponent implements OnInit {
|
|||
deleteTask(taskId: string) {
|
||||
this.apiService.deleteTaskById(taskId).subscribe({
|
||||
next: (task) => {
|
||||
console.log('Task deleted successfully:', task);
|
||||
this.taskUpdateService.notifyTaskUpdate();
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('Error deleting task', err);
|
||||
|
|
|
|||
Loading…
Reference in a new issue