diff --git a/src/app/components/add-task/add-task.component.ts b/src/app/components/add-task/add-task.component.ts index 58231df..ad36988 100644 --- a/src/app/components/add-task/add-task.component.ts +++ b/src/app/components/add-task/add-task.component.ts @@ -13,7 +13,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'; +import { UpdateNotifierService } from '../../services/update-notifier.service'; import { ToastNotificationService } from '../../services/toast-notification.servic'; @Component({ @@ -51,7 +51,7 @@ export class AddTaskComponent implements OnInit { private taskService: TaskService, private apiService: ApiService, private authService: AuthService, - private taskUpdateService: TaskUpdateService, + private updateNotifierService: UpdateNotifierService, private toastNotificationService: ToastNotificationService, private route: ActivatedRoute, private router: Router @@ -380,7 +380,7 @@ export class AddTaskComponent implements OnInit { this.apiService.saveNewTask(taskWithoutId).subscribe({ next: (response) => { this.toastNotificationService.createTaskSuccessToast(); - this.taskUpdateService.notifyTaskUpdate(); + this.updateNotifierService.notifyUpdate('task'); this.removeTaskData(ngForm); this.closeOverlay(); this.router.navigate(['/board']); diff --git a/src/app/components/board/board.component.ts b/src/app/components/board/board.component.ts index 09dccb8..51c5ba5 100644 --- a/src/app/components/board/board.component.ts +++ b/src/app/components/board/board.component.ts @@ -13,7 +13,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'; +import { UpdateNotifierService } from '../../services/update-notifier.service'; import { ToastNotificationService } from '../../services/toast-notification.servic'; import { ResizeService } from '../../services/resize.service'; @@ -46,7 +46,7 @@ export class BoardComponent { private resizeService: ResizeService, private taskService: TaskService, private apiService: ApiService, - private taskUpdateService: TaskUpdateService, + private updateNotifierService: UpdateNotifierService, private toastNotificationService: ToastNotificationService, private router: Router ) {} @@ -95,7 +95,7 @@ export class BoardComponent { } private subscribeToTaskUpdates() { - this.taskUpdateService.taskUpdated$ + this.updateNotifierService.taskUpdated$ .pipe(takeUntil(this.destroy$)) .subscribe(() => { this.loadAllTasks(); diff --git a/src/app/components/contacts/contact-detail/contact-detail.component.ts b/src/app/components/contacts/contact-detail/contact-detail.component.ts index 49165ac..c0273a9 100644 --- a/src/app/components/contacts/contact-detail/contact-detail.component.ts +++ b/src/app/components/contacts/contact-detail/contact-detail.component.ts @@ -14,6 +14,7 @@ import { User } from '../../../interfaces/user.interface'; import { OverlayService } from '../../../services/overlay.service'; import { ApiService } from '../../../services/api.service'; import { ToastNotificationService } from '../../../services/toast-notification.servic'; +import { UpdateNotifierService } from '../../../services/update-notifier.service'; @Component({ selector: 'app-contact-detail', @@ -36,7 +37,8 @@ export class ContactDetailComponent { private overlayService: OverlayService, private userService: UserService, private apiService: ApiService, - private toastNotificationService: ToastNotificationService + private toastNotificationService: ToastNotificationService, + private updateNotifierService: UpdateNotifierService ) {} ngOnChanges(changes: SimpleChanges) { @@ -97,14 +99,11 @@ export class ContactDetailComponent { this.isMobileNavbarOpen = false; } - /** - * Opens the delete contact dialog by setting the appropriate flags in the shared service. - * This method is used by the contact details component to open the delete contact dialog when the user clicks the delete button. - */ async deleteContact(userId: string) { try { await lastValueFrom(this.apiService.deleteUserById(userId)); this.toastNotificationService.deleteContactSuccessToast(); + this.updateNotifierService.notifyUpdate('contact'); this.closeUserDetails(); } catch (error) { console.error(error); diff --git a/src/app/components/contacts/contacts.component.html b/src/app/components/contacts/contacts.component.html index a677a6a..8025ab4 100644 --- a/src/app/components/contacts/contacts.component.html +++ b/src/app/components/contacts/contacts.component.html @@ -18,8 +18,8 @@ @for (sortLetter of this.getUniqueFirstLetters(); track sortLetter) {
{{ sortLetter }}
- @for (user of this.filterUsersByFirstLetter(sortLetter); track user; let - index = $index) { + @for (user of this.filterUsersByFirstLetter(sortLetter); track user.id; + let index = $index) {
(); + allUsers: User[] = []; currentUser: User | null = null; selectedUserId: string | null = null; @@ -25,7 +28,8 @@ export class ContactsComponent { constructor( public firebaseService: FirebaseService, private userService: UserService, - private overlayService: OverlayService + private overlayService: OverlayService, + private updateNotifierService: UpdateNotifierService ) {} /** @@ -35,6 +39,7 @@ export class ContactsComponent { this.onResize(); this.loadAllUsers(); this.loadCurrentUser(); + this.subscribeToUserUpdates(); } loadAllUsers(): void { @@ -53,6 +58,14 @@ export class ContactsComponent { }); } + private subscribeToUserUpdates() { + this.updateNotifierService.contactUpdated$ + .pipe(takeUntil(this.destroy$)) + .subscribe(() => { + this.loadAllUsers(); + }); + } + loadCurrentUser(): void { this.userService.getCurrentUser().subscribe((userData) => { this.currentUser = userData; diff --git a/src/app/services/task-update.service.ts b/src/app/services/task-update.service.ts deleted file mode 100644 index 3f64f68..0000000 --- a/src/app/services/task-update.service.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Injectable } from '@angular/core'; -import { Subject } from 'rxjs'; - -@Injectable({ - providedIn: 'root', -}) -export class TaskUpdateService { - private taskUpdatedSubject = new Subject(); - taskUpdated$ = this.taskUpdatedSubject.asObservable(); - - notifyTaskUpdate() { - this.taskUpdatedSubject.next(); - } -} diff --git a/src/app/services/update-notifier.service.ts b/src/app/services/update-notifier.service.ts new file mode 100644 index 0000000..6f2fc99 --- /dev/null +++ b/src/app/services/update-notifier.service.ts @@ -0,0 +1,19 @@ +import { Injectable } from '@angular/core'; +import { Subject } from 'rxjs'; + +@Injectable({ + providedIn: 'root', +}) +export class UpdateNotifierService { + private updateSubjects: Record> = { + task: new Subject(), + contact: new Subject(), + }; + + taskUpdated$ = this.updateSubjects['task'].asObservable(); + contactUpdated$ = this.updateSubjects['contact'].asObservable(); + + notifyUpdate(type: 'task' | 'contact') { + this.updateSubjects[type].next(); + } +} diff --git a/src/app/shared/components/overlay/task-overlay/task-overlay.component.ts b/src/app/shared/components/overlay/task-overlay/task-overlay.component.ts index 8905943..c77946d 100644 --- a/src/app/shared/components/overlay/task-overlay/task-overlay.component.ts +++ b/src/app/shared/components/overlay/task-overlay/task-overlay.component.ts @@ -11,7 +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'; +import { UpdateNotifierService } from '../../../../services/update-notifier.service'; import { ToastNotificationService } from '../../../../services/toast-notification.servic'; @Component({ @@ -35,7 +35,7 @@ export class TaskOverlayComponent implements OnInit { private taskService: TaskService, private authService: AuthService, private apiService: ApiService, - private taskUpdateService: TaskUpdateService, + private updateNotifierService: UpdateNotifierService, private toastNotificationService: ToastNotificationService, private router: Router, private route: ActivatedRoute @@ -126,7 +126,7 @@ export class TaskOverlayComponent implements OnInit { this.apiService.deleteTaskById(taskId).subscribe({ next: (task) => { this.toastNotificationService.deleteTaskSuccessToast(); - this.taskUpdateService.notifyTaskUpdate(); + this.updateNotifierService.notifyUpdate('task'); }, error: (err) => { console.error('Error deleting task', err);