2023-01-20 15:08:44 +02:00
|
|
|
///
|
2025-02-25 09:39:16 +02:00
|
|
|
/// Copyright © 2016-2025 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.
|
|
|
|
|
///
|
|
|
|
|
|
2025-01-09 18:50:45 +02:00
|
|
|
import { ChangeDetectorRef, Component, Input, NgZone, OnDestroy } from '@angular/core';
|
2023-01-20 15:08:44 +02:00
|
|
|
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';
|
2025-01-09 18:50:45 +02:00
|
|
|
import { BehaviorSubject, Observable, shareReplay } from 'rxjs';
|
|
|
|
|
import { filter, 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';
|
2025-01-09 18:50:45 +02:00
|
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
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
|
|
|
})
|
2025-01-09 18:50:45 +02:00
|
|
|
export class ShowNotificationPopoverComponent extends PageComponent implements OnDestroy {
|
2023-01-20 15:08:44 +02:00
|
|
|
|
|
|
|
|
@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;
|
|
|
|
|
|
2025-01-09 18:50:45 +02:00
|
|
|
private notificationSubscriber = NotificationSubscriber.createNotificationsSubscription(this.notificationWsService, this.zone, 6);
|
2023-02-16 12:59:43 +02:00
|
|
|
|
2025-01-09 18:50:45 +02:00
|
|
|
notifications$: Observable<Notification[]> = this.notificationSubscriber.notifications$.pipe(
|
|
|
|
|
filter(value => Array.isArray(value)),
|
|
|
|
|
shareReplay(1),
|
|
|
|
|
tap(() => setTimeout(() => this.cd.markForCheck()))
|
|
|
|
|
);
|
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);
|
2025-01-09 18:50:45 +02:00
|
|
|
this.notificationSubscriber.notificationCount$.pipe(
|
2023-12-11 12:43:41 +02:00
|
|
|
skip(1),
|
2025-01-09 18:50:45 +02:00
|
|
|
takeUntilDestroyed()
|
2023-12-11 12:43:41 +02:00
|
|
|
).subscribe(value => this.counter.next(value));
|
2023-01-20 15:08:44 +02:00
|
|
|
this.notificationSubscriber.subscribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
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
|
|
|
}
|