thingsboard/ui-ngx/src/app/modules/home/components/notification/show-notification-popover.component.ts

112 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-01-20 15:08:44 +02:00
///
2024-01-09 10:46:16 +02:00
/// Copyright © 2016-2024 The Thingsboard Authors
2023-01-20 15:08:44 +02:00
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { ChangeDetectorRef, Component, Input, NgZone, OnDestroy, OnInit } from '@angular/core';
import { PageComponent } from '@shared/components/page.component';
import { TbPopoverComponent } from '@shared/components/popover.component';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
2023-02-16 12:59:43 +02:00
import { Notification, NotificationRequest } from '@shared/models/notification.models';
2023-01-20 15:08:44 +02:00
import { NotificationWebsocketService } from '@core/ws/notification-websocket.service';
2023-02-16 12:59:43 +02:00
import { BehaviorSubject, Observable, ReplaySubject, Subscription } from 'rxjs';
import { map, share, skip, tap } from 'rxjs/operators';
2023-01-20 15:08:44 +02:00
import { Router } from '@angular/router';
2023-12-11 13:30:34 +02:00
import { NotificationSubscriber } from '@shared/models/telemetry/telemetry.models';
2023-01-20 15:08:44 +02:00
@Component({
selector: 'tb-show-notification-popover',
templateUrl: './show-notification-popover.component.html',
2024-07-05 15:53:48 +03:00
styleUrls: ['show-notification-popover.component.scss']
2023-01-20 15:08:44 +02:00
})
export class ShowNotificationPopoverComponent extends PageComponent implements OnDestroy, OnInit {
@Input()
onClose: () => void;
2023-02-16 12:59:43 +02:00
@Input()
counter: BehaviorSubject<number>;
2023-01-20 15:08:44 +02:00
@Input()
popoverComponent: TbPopoverComponent;
private notificationSubscriber: NotificationSubscriber;
2023-02-16 12:59:43 +02:00
private notificationCountSubscriber: Subscription;
2023-01-20 15:08:44 +02:00
notifications$: Observable<Notification[]>;
loadNotification = false;
2023-01-20 15:08:44 +02:00
constructor(protected store: Store<AppState>,
private notificationWsService: NotificationWebsocketService,
private zone: NgZone,
private cd: ChangeDetectorRef,
private router: Router) {
super(store);
}
ngOnInit() {
this.notificationSubscriber = NotificationSubscriber.createNotificationsSubscription(this.notificationWsService, this.zone, 6);
2023-01-20 15:08:44 +02:00
this.notifications$ = this.notificationSubscriber.notifications$.pipe(
map(value => {
if (Array.isArray(value)) {
this.loadNotification = true;
return value;
}
return [];
}),
2023-02-16 12:59:43 +02:00
share({
connector: () => new ReplaySubject(1)
}),
tap(() => setTimeout(() => this.cd.markForCheck()))
2023-01-20 15:08:44 +02:00
);
this.notificationCountSubscriber = this.notificationSubscriber.notificationCount$.pipe(
skip(1),
).subscribe(value => this.counter.next(value));
2023-01-20 15:08:44 +02:00
this.notificationSubscriber.subscribe();
}
ngOnDestroy() {
super.ngOnDestroy();
2023-02-16 12:59:43 +02:00
this.notificationCountSubscriber.unsubscribe();
2023-01-20 15:08:44 +02:00
this.notificationSubscriber.unsubscribe();
2023-02-16 12:59:43 +02:00
this.onClose();
2023-01-20 15:08:44 +02:00
}
markAsRead(id: string) {
const cmd = NotificationSubscriber.createMarkAsReadCommand(this.notificationWsService, [id]);
cmd.subscribe();
}
markAsAllRead($event: Event) {
if ($event) {
$event.stopPropagation();
}
const cmd = NotificationSubscriber.createMarkAllAsReadCommand(this.notificationWsService);
cmd.subscribe();
}
viewAll($event: Event) {
if ($event) {
$event.stopPropagation();
}
this.onClose();
2023-03-21 17:56:12 +02:00
this.router.navigateByUrl(this.router.parseUrl('/notification/inbox')).then(() => {});
2023-01-20 15:08:44 +02:00
}
2023-02-16 12:59:43 +02:00
2023-03-20 16:51:42 +02:00
trackById(index: number, item: NotificationRequest): string {
2023-02-16 12:59:43 +02:00
return item.id.id;
}
2023-01-20 15:08:44 +02:00
}