2022-04-05 18:55:01 +02:00
|
|
|
import {Component, Input, OnDestroy, OnInit, ViewEncapsulation} from "@angular/core";
|
2021-03-04 12:32:57 +01:00
|
|
|
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";
|
|
|
|
|
2021-10-05 16:27:16 +02:00
|
|
|
declare var UIkit;
|
|
|
|
|
2021-03-04 12:32:57 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'notification-sidebar',
|
|
|
|
template: `
|
2022-04-05 18:55:01 +02:00
|
|
|
<div id="notifications-switcher" uk-toggle href="#notifications" class="offcanvas-switcher uk-flex uk-flex-center uk-flex-middle">
|
|
|
|
<icon name="mail" ratio="1.5" customClass="uk-text-primary-gradient" flex="true" visuallyHidden="Notifications"></icon>
|
|
|
|
<span [class.uk-hidden]="unreadCount === 0" id="notifications-count" class="offcanvas-count uk-flex uk-flex-middle uk-flex-center">
|
2021-03-04 12:32:57 +01:00
|
|
|
{{unreadCount}}
|
|
|
|
</span>
|
|
|
|
</div>
|
2022-03-30 18:18:06 +02:00
|
|
|
<div id="notifications" class="uk-offcanvas offcanvas" uk-offcanvas="flip: true; overlay: true;">
|
2021-03-04 12:32:57 +01:00
|
|
|
<div class="uk-offcanvas-bar">
|
2022-03-30 18:18:06 +02:00
|
|
|
<button class="uk-offcanvas-close offcanvas-close uk-close uk-icon" type="button">
|
2022-04-05 18:55:01 +02:00
|
|
|
<icon name="close" ratio="1.5" visuallyHidden="close"></icon>
|
2021-03-04 12:32:57 +01:00
|
|
|
</button>
|
2022-03-30 18:18:06 +02:00
|
|
|
|
2021-03-04 12:32:57 +01:00
|
|
|
<ng-template [ngIf]="!notification">
|
|
|
|
<div class="notification-list uk-position-relative">
|
2022-03-30 18:18:06 +02:00
|
|
|
<h4>Notifications</h4>
|
2021-10-05 16:27:16 +02:00
|
|
|
<div class="uk-flex uk-flex-right@m uk-flex-center uk-padding uk-padding-remove-vertical">
|
|
|
|
<button [disabled]="unreadCount === 0" (click)="readAll()" class="uk-button uk-button-link">Mark As Read ({{unreadCount}})</button>
|
|
|
|
</div>
|
2021-03-04 12:32:57 +01:00
|
|
|
<h6 *ngIf="notifications.length == 0" class="uk-position-center uk-margin-remove">No notifications</h6>
|
|
|
|
<ul *ngIf="notifications.length > 0" class="uk-list">
|
|
|
|
<li *ngFor="let notification of notifications; let i=index" class="clickable" (click)="select(notification)">
|
|
|
|
<div class="uk-grid uk-grid-small" uk-grid>
|
2021-04-06 13:22:24 +02:00
|
|
|
<notification-user [name]="notification.name" [surname]="notification.surname" [outline]="true"
|
2021-03-04 12:32:57 +01:00
|
|
|
colorClass="uk-text-secondary"></notification-user>
|
|
|
|
<div class="uk-width-expand">
|
|
|
|
<div class="uk-width-1-1 uk-flex uk-flex-middle">
|
|
|
|
<div class="uk-width-expand multi-line-ellipsis lines-2">
|
|
|
|
<p class="uk-margin-remove">
|
|
|
|
{{notification.preview}}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div class="uk-margin-left uk-flex uk-flex-center uk-text-secondary">
|
2022-04-05 18:55:01 +02:00
|
|
|
<icon *ngIf="!notification.read" name="bullet" ratio="0.6" visuallyHidden="unread"></icon>
|
2021-03-04 12:32:57 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-03-30 18:18:06 +02:00
|
|
|
<span class="uk-text-secondary uk-text-small">{{getDate(notification.date)}}</span>
|
2021-03-04 12:32:57 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div *ngIf="availableGroups.length > 0" [availableGroups]="availableGroups" [service]="service" notify-form class="notify"></div>
|
|
|
|
</ng-template>
|
|
|
|
<div *ngIf="notification" class="notification">
|
|
|
|
<div class="uk-flex uk-flex-middle uk-margin-medium-bottom">
|
|
|
|
<span class="uk-text-secondary clickable" (click)="back($event)">
|
2022-04-05 18:55:01 +02:00
|
|
|
<icon ratio="1.5" name="arrow_left" visuallyHidden="back"></icon>
|
2021-03-04 12:32:57 +01:00
|
|
|
</span>
|
2022-03-30 18:18:06 +02:00
|
|
|
<h4 *ngIf="notification.title" class="uk-text-bold uk-margin-left">{{notification.title}}</h4>
|
2021-03-04 12:32:57 +01:00
|
|
|
</div>
|
|
|
|
<div class="uk-flex uk-flex-middle uk-margin-medium-bottom">
|
|
|
|
<notification-user [name]="notification.name" [surname]="notification.surname" colorClass="uk-text-secondary" [outline]="true"></notification-user>
|
|
|
|
<div class="uk-margin-left">
|
|
|
|
{{notification.name + ' ' + notification.surname}}<br>
|
2022-03-30 18:18:06 +02:00
|
|
|
<span style="opacity: 0.8;" class="uk-text-small uk-margin-small-top">
|
2021-03-04 12:32:57 +01:00
|
|
|
{{notification.date | date:'medium'}} ({{getDate(notification.date)}})
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div [innerHTML]="notification.message | safeHtml">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
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[] = [];
|
2022-03-30 18:18:06 +02:00
|
|
|
|
2022-04-05 18:55:01 +02:00
|
|
|
constructor(private notificationService: NotificationService) {
|
2021-03-04 12:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.subscriptions.push(this.notificationService.getNotifications(this.service).subscribe(notifications => {
|
|
|
|
this.notifications = notifications;
|
2021-10-05 16:27:16 +02:00
|
|
|
}, error => {
|
|
|
|
this.notifications = [];
|
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
2021-03-04 12:32:57 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-10-05 16:27:16 +02:00
|
|
|
this.notificationService.readNotification(notification._id).subscribe(() => {
|
2021-03-04 12:32:57 +01:00
|
|
|
notification.read = true;
|
|
|
|
this.notification = notification;
|
2021-10-05 16:27:16 +02:00
|
|
|
}, error => {
|
|
|
|
UIkit.notification('An error has occurred. Please try again later', {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
2021-03-04 12:32:57 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
back(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
this.notification = null;
|
|
|
|
}
|
2021-10-05 16:27:16 +02:00
|
|
|
|
|
|
|
readAll() {
|
|
|
|
this.notificationService.markAllAsRead(this.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'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-03-04 12:32:57 +01:00
|
|
|
}
|