import { Component , Injector , OnInit , ViewEncapsulation , NgZone } from '@angular/core' ; import { AppComponentBase } from '@shared/common/app-component-base' ; import { NotificationServiceProxy , UserNotification } from '@shared/service-proxies/service-proxies' ; import { IFormattedUserNotification , UserNotificationHelper } from './UserNotificationHelper' ; import { forEach as _forEach } from 'lodash-es' ; import { finalize } from 'rxjs/operators' ; @ Component ( { templateUrl : './header-notifications.component.html' , selector : 'header-notifications' , styles : [ ] , encapsulation : ViewEncapsulation . None } ) export class HeaderNotificationsComponent extends AppComponentBase implements OnInit { notifications : IFormattedUserNotification [ ] = [ ] ; unreadNotificationCount = 0 ; loading = true ; constructor ( injector , private _notificationService , private _userNotificationHelper , public _zone ) { super ( injector ) ; } ngOnInit ( ) { this . loadNotifications ( ) ; this . registerToEvents ( ) ; } loadNotifications ( ) { this . _notificationService . getUserNotifications ( undefined , undefined , undefined , 3 , 0 ) . pipe ( finalize ( ( ) => this . loading = false ) ) . subscribe ( result => { this . unreadNotificationCount = result . unreadCount ; this . notifications = [ ] ; _forEach ( result . items , ( item ) => { this . notifications . push ( this . _userNotificationHelper . format ( < any > item ) ) ; } ) ; } ) ; } registerToEvents ( ) { let self = this ; function onNotificationReceived ( userNotification ) { self . _userNotificationHelper . show ( userNotification ) ; self . loadNotifications ( ) ; } abp . event . on ( 'abp.notifications.received' , userNotification => { self . _zone . run ( ( ) => { onNotificationReceived ( userNotification ) ; } ) ; } ) ; function onNotificationsRefresh ( ) { self . loadNotifications ( ) ; } abp . event . on ( 'app.notifications.refresh' , ( ) => { self . _zone . run ( ( ) => { onNotificationsRefresh ( ) ; } ) ; } ) ; function onNotificationsRead ( userNotificationId ) { for ( let i = 0 ; i < self . notifications . length ; i ++ ) { if ( self . notifications [ i ] . userNotificationId === userNotificationId ) { self . notifications [ i ] . state = 'READ' ; self . notifications [ i ] . isUnread = false ; } } self . unreadNotificationCount -= 1 ; } abp . event . on ( 'app.notifications.read' , userNotificationId => { self . _zone . run ( ( ) => { onNotificationsRead ( userNotificationId ) ; } ) ; } ) ; } setAllNotificationsAsRead ( ) { this . _userNotificationHelper . setAllAsRead ( ) ; } openNotificationSettingsModal ( ) { this . _userNotificationHelper . openSettingsModal ( ) ; } setNotificationAsRead ( userNotification ) { if ( userNotification . state !== 'READ' ) { this . _userNotificationHelper . setAsRead ( userNotification . userNotificationId ) ; } } gotoUrl ( url ) { if ( url ) { location . href = url ; } } }