import {Component, Input, OnDestroy, OnInit, ViewEncapsulation} from "@angular/core";
import {Notification} from "../notifications";
import {NotificationService} from "../notification.service";
import {Subscription} from "rxjs";
import {User} from "../../login/utils/helper.class";
import {Dates} from "../../utils/string-utils.class";
import {Option} from "../../sharedComponents/input/input.component";
@Component({
selector: 'notification-sidebar',
template: `
{{unreadCount}}
Notifications
No notifications
0" class="uk-list">
-
{{getDate(notification.date)}}
0" [availableGroups]="availableGroups" [service]="service" notify-form class="notify">
{{notification.title}}
{{notification.name + ' ' + notification.surname}}
{{notification.date | date:'medium'}} ({{getDate(notification.date)}})
`,
styleUrls: ['notification-sidebar.component.css'],
encapsulation: ViewEncapsulation.None
})
export class NotificationsSidebarComponent implements OnInit, OnDestroy {
@Input()
public user: User;
public notifications: Notification[] = [];
@Input()
public availableGroups: Option[] = [];
@Input()
public service: string;
public notification: Notification;
private subscriptions: any[] = [];
constructor(private notificationService: NotificationService) {
}
ngOnInit() {
this.subscriptions.push(this.notificationService.getNotifications(this.service).subscribe(notifications => {
this.notifications = notifications;
}));
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscription) {
subscription.unsubscribe();
}
})
}
get unreadCount(): number {
return this.notifications.filter(notification => !notification.read).length;
}
getDate(date: Date): string {
return Dates.timeSince(date);
}
select(notification: Notification) {
this.notificationService.readNotification(notification._id).subscribe(user => {
notification.read = true;
this.notification = notification;
});
}
back(event) {
event.stopPropagation();
this.notification = null;
}
}