]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/blob
8bba8a443df8887ff92e998ec374913a9a57f131
[ceph-ci.git] /
1 import { Component, OnInit, OnDestroy } from '@angular/core';
2 import { Subscription } from 'rxjs';
3 import { NotificationService } from '../../../../shared/services/notification.service';
4 import { CdNotification } from '../../../../shared/models/cd-notification';
5 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
6
7 @Component({
8   selector: 'cd-notification-area',
9   templateUrl: './notification-area.component.html',
10   styleUrls: ['./notification-area.component.scss']
11 })
12 export class NotificationAreaComponent implements OnInit, OnDestroy {
13   todayNotifications: CdNotification[] = [];
14   previousNotifications: CdNotification[] = [];
15   private sub: Subscription;
16
17   readonly notificationIconMap = {
18     [NotificationType.success]: 'success',
19     [NotificationType.error]: 'danger',
20     [NotificationType.info]: 'info',
21     [NotificationType.warning]: 'warning'
22   } as const;
23
24   constructor(private notificationService: NotificationService) {}
25
26   ngOnInit(): void {
27     this.sub = this.notificationService.data$.subscribe((notifications: CdNotification[]) => {
28       const today: Date = new Date();
29       this.todayNotifications = [];
30       this.previousNotifications = [];
31       notifications.forEach((n: CdNotification) => {
32         const notifDate = new Date(n.timestamp);
33         if (
34           notifDate.getDate() === today.getDate() &&
35           notifDate.getMonth() === today.getMonth() &&
36           notifDate.getFullYear() === today.getFullYear()
37         ) {
38           this.todayNotifications.push(n);
39         } else {
40           this.previousNotifications.push(n);
41         }
42       });
43     });
44   }
45
46   ngOnDestroy(): void {
47     if (this.sub) {
48       this.sub.unsubscribe();
49     }
50   }
51
52   removeNotification(notification: CdNotification, event: MouseEvent) {
53     // Stop event propagation to prevent panel closing
54     event.stopPropagation();
55     event.preventDefault();
56
57     // Get the notification index from the service's data
58     const notifications = this.notificationService['dataSource'].getValue();
59     const index = notifications.findIndex(
60       (n) => n.timestamp === notification.timestamp && n.title === notification.title
61     );
62
63     if (index > -1) {
64       // Remove the notification through the service
65       this.notificationService.remove(index);
66     }
67   }
68 }