import { fromEvent, Observable, Subscription } from "rxjs"; import { HttpErrorResponse } from '@angular/common/http'; import { Component, Inject, OnDestroy, OnInit } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { Router } from '@angular/router'; import { AuthService } from '@app/core/services/auth/auth.service'; import { HttpError, HttpErrorHandlingService } from '@common/modules/errors/error-handling/http-error-handling.service'; import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service'; import { BaseComponent } from '@common/base/base.component'; import { InAppNotificationLookup } from '@notification-service/core/query/inapp-notification.lookup'; import { InAppNotificationService } from '@notification-service/services/http/inapp-notification.service'; import { takeUntil } from 'rxjs/operators'; import { nameof } from 'ts-simple-nameof'; import { IsActive } from '@notification-service/core/enum/is-active.enum'; import { NotificationInAppTracking } from "@notification-service/core/enum/notification-inapp-tracking.enum"; import { InAppNotification } from "@notification-service/core/model/inapp-notification.model"; @Component({ selector: 'app-mine-inapp-notification-listing-dialog', templateUrl: './mine-inapp-notification-listing-dialog.component.html', styleUrls: ['./mine-inapp-notification-listing-dialog.component.scss'] }) export class MineInAppNotificationListingDialogComponent extends BaseComponent implements OnInit, OnDestroy { public inappNotifications = new Array(); public notificationInAppTrackingEnum = NotificationInAppTracking; resizeObservable: Observable; resizeSubscription: Subscription; constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public dialogData: any, private inappNotificationService: InAppNotificationService, private router: Router, private uiNotificationService: UiNotificationService, private httpErrorHandlingService: HttpErrorHandlingService, public authService: AuthService, ) { super(); } ngOnInit() { const lookup = new InAppNotificationLookup(); lookup.project = { fields: [ nameof(x => x.id), nameof(x => x.subject), nameof(x => x.createdAt), nameof(x => x.trackingState), ] }; lookup.page = { offset: 0, size: 5 }; lookup.order = { items: ['-' + nameof(x => x.createdAt)] }; lookup.isActive = [IsActive.Active]; this.inappNotificationService.query(lookup) .pipe(takeUntil(this._destroyed)) .subscribe( data => { this.inappNotifications = data.items; }, error => this.onCallbackError(error), ); this.resizeObservable = fromEvent(window, 'resize'); this.resizeSubscription = this.resizeObservable .subscribe(evt =>{ this.dialogRef.close(); }); } ngOnDestroy(): void { this.resizeSubscription.unsubscribe(); } private onCallbackError(errorResponse: HttpErrorResponse) { const error: HttpError = this.httpErrorHandlingService.getError(errorResponse); this.uiNotificationService.snackBarNotification(error.getMessagesString(), SnackBarNotificationLevel.Warning); } goToNotification(item: InAppNotification) { if (item.trackingState === NotificationInAppTracking.Stored) { this.inappNotificationService.read(item.id) .pipe(takeUntil(this._destroyed)) .subscribe( data => { this.dialogRef.close(); this.router.navigate(['/mine-notifications/dialog/' + item.id]); }, error => { this.dialogRef.close(); this.router.navigate(['/mine-notifications/dialog/' + item.id]); }, ); } else { this.dialogRef.close(); this.router.navigate(['/mine-notifications/dialog/' + item.id]); } } goToNotifications() { this.router.navigate(['/mine-notifications']); this.dialogRef.close(); } readAllNotifications() { this.inappNotificationService.readAll() .pipe(takeUntil(this._destroyed)) .subscribe( data => {}, ); } }