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';
8 selector: 'cd-notification-area',
9 templateUrl: './notification-area.component.html',
10 styleUrls: ['./notification-area.component.scss']
12 export class NotificationAreaComponent implements OnInit, OnDestroy {
13 todayNotifications: CdNotification[] = [];
14 previousNotifications: CdNotification[] = [];
15 private sub: Subscription;
17 readonly notificationIconMap = {
18 [NotificationType.success]: 'success',
19 [NotificationType.error]: 'danger',
20 [NotificationType.info]: 'info',
21 [NotificationType.warning]: 'warning'
24 constructor(private notificationService: NotificationService) {}
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);
34 notifDate.getDate() === today.getDate() &&
35 notifDate.getMonth() === today.getMonth() &&
36 notifDate.getFullYear() === today.getFullYear()
38 this.todayNotifications.push(n);
40 this.previousNotifications.push(n);
48 this.sub.unsubscribe();
52 removeNotification(notification: CdNotification, event: MouseEvent) {
53 // Stop event propagation to prevent panel closing
54 event.stopPropagation();
55 event.preventDefault();
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
64 // Remove the notification through the service
65 this.notificationService.remove(index);