import {
Component,
ElementRef,
EventEmitter,
Input,
OnDestroy,
OnInit,
Output,
ViewChild,
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";
declare var UIkit;
export class NotificationConfiguration {
availableGroups: Option[] = [];
entities: string[] = [];
service: string;
}
@Component({
selector: 'notification-sidebar',
template: `
{{unreadCount}}
Notifications
No notifications
0" class="uk-list">
-
{{getDate(notification.date)}}
0" [availableGroups]="configuration.availableGroups" [entities]="configuration.entities" [service]="configuration.service" notify-form class="notify">
{{notification.title}}
{{notification.name + ' ' + notification.surname}}
{{notification.date | date:'medium'}} ({{getDate(notification.date)}})
`,
styleUrls: ['notification-sidebar.component.less'],
encapsulation: ViewEncapsulation.None
})
export class NotificationsSidebarComponent implements OnInit, OnDestroy {
@Input()
public user: User;
@Input()
public mobile: boolean = false;
@Input()
public configuration: NotificationConfiguration;
@Output()
public showNotificationsEmitter: EventEmitter = new EventEmitter();
public notifications: Notification[] = [];
public notification: Notification;
private subscriptions: any[] = [];
constructor(private notificationService: NotificationService) {
}
ngOnInit() {
this.subscriptions.push(this.notificationService.getNotifications(this.configuration.service).subscribe(notifications => {
this.notifications = notifications;
}, error => {
this.notifications = [];
UIkit.notification('An error has occurred. Please try again later', {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
}));
}
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(() => {
notification.read = true;
this.notification = notification;
}, error => {
UIkit.notification('An error has occurred. Please try again later', {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
});
}
back(event) {
event.stopPropagation();
this.notification = null;
}
closeCanvas(canvas) {
UIkit.offcanvas(canvas).hide();
}
readAll() {
this.notificationService.markAllAsRead(this.configuration.service).subscribe(() => {
this.notifications.forEach(notification => {
notification.read = true;
});
}, error => {
UIkit.notification('An error has occurred. Please try again later', {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
});
}
}