11import { HttpResponse } from '@angular/common/http' ;
2- import { ChangeDetectionStrategy , Component , OnDestroy , OnInit } from '@angular/core' ;
2+ import { ChangeDetectionStrategy , ChangeDetectorRef , Component , OnDestroy , OnInit } from '@angular/core' ;
33import { Router } from '@angular/router' ;
44import { NgbModal } from '@ng-bootstrap/ng-bootstrap' ;
55import { ResizeEvent } from 'angular-resizable-element' ;
66import * as moment from 'moment' ;
77import { NgxSpinnerService } from 'ngx-spinner' ;
8- import { Observable } from 'rxjs' ;
9- import { filter , map , switchMap , tap } from 'rxjs/operators' ;
8+ import { Observable , Subject } from 'rxjs' ;
9+ import { filter , finalize , map , switchMap , take , takeUntil , tap } from 'rxjs/operators' ;
1010import { AccountService } from '../../core/auth/account.service' ;
1111import { UtmToastService } from '../../shared/alert/utm-toast.service' ;
1212import {
@@ -53,7 +53,7 @@ export class AssetsViewComponent implements OnInit, OnDestroy {
5353 sortEvent : any ;
5454 totalItems : any ;
5555 page = 0 ;
56- loading = true ;
56+ loading = false ;
5757 itemsPerPage = ITEMS_PER_PAGE ;
5858 viewAssetDetail : NetScanType ;
5959 sortBy = AssetFieldEnum . ASSET_ID + ',asc' ;
@@ -85,6 +85,7 @@ export class AssetsViewComponent implements OnInit, OnDestroy {
8585 reasonRun : IncidentCommandType ;
8686 agent : string ;
8787 noData = false ;
88+ destroy$ = new Subject < void > ( ) ;
8889
8990 constructor ( private utmNetScanService : UtmNetScanService ,
9091 private modalService : NgbModal ,
@@ -94,7 +95,7 @@ export class AssetsViewComponent implements OnInit, OnDestroy {
9495 private spinner : NgxSpinnerService ,
9596 private accountService : AccountService ,
9697 private assetFiltersBehavior : AssetFiltersBehavior ,
97- private datePipe : UtmDatePipe ) {
98+ private cdr : ChangeDetectorRef ) {
9899 }
99100
100101 ngOnInit ( ) {
@@ -111,11 +112,14 @@ export class AssetsViewComponent implements OnInit, OnDestroy {
111112
112113 this . utmNetScanService . onRefresh$
113114 . pipe (
115+ takeUntil ( this . destroy$ ) ,
114116 filter ( refresh => ! ! refresh ) ,
115- switchMap ( ( ) => this . utmNetScanService . fetchData ( this . requestParam ) ) ,
117+ switchMap ( ( ) => {
118+ this . loading = true ;
119+ return this . utmNetScanService . fetchData ( this . requestParam ) ;
120+ } ) ,
116121 tap ( ( response : HttpResponse < NetScanType [ ] > ) => {
117122 this . totalItems = Number ( response . headers . get ( 'X-Total-Count' ) ) ;
118- this . loading = false ;
119123 this . noData = response . body . length === 0 ;
120124 } ) ,
121125 map ( ( response ) => {
@@ -136,7 +140,21 @@ export class AssetsViewComponent implements OnInit, OnDestroy {
136140 return { ...asset , displayName, sortKey } ;
137141 } ) ;
138142 } )
139- ) . subscribe ( assets => this . assets = assets ) ;
143+ )
144+ . subscribe ( {
145+ next : ( assets : NetScanType [ ] ) => {
146+ this . assets = assets ;
147+ this . loading = false ;
148+ this . cdr . markForCheck ( ) ;
149+ } ,
150+ error : ( ) => {
151+ this . assets = [ ] ;
152+ this . loading = false ;
153+ this . cdr . markForCheck ( ) ;
154+ this . utmToastService . showError ( 'Error loading assets' ,
155+ 'There was an error while trying to load assets, please try again' ) ;
156+ }
157+ } ) ;
140158
141159 this . utmNetScanService . notifyRefresh ( true ) ;
142160 // this.starInterval();
@@ -371,6 +389,7 @@ export class AssetsViewComponent implements OnInit, OnDestroy {
371389 }
372390
373391 getLastInput ( asset : NetScanType ) {
392+ console . log ( 'getLastInput' , asset . lastInput ) ;
374393 if ( asset . dataInputList . length > 0 ) {
375394 const lastInput = asset . dataInputList [ asset . dataInputList . length - 1 ] . timestamp ;
376395 asset . lastInputTimestamp = lastInput ;
@@ -454,6 +473,9 @@ export class AssetsViewComponent implements OnInit, OnDestroy {
454473 }
455474
456475 ngOnDestroy ( ) : void {
476+ this . destroy$ . next ( ) ;
477+ this . destroy$ . complete ( ) ;
478+ this . utmNetScanService . notifyRefresh ( false ) ;
457479 this . stopInterval ( true ) ;
458480 this . assetFiltersBehavior . $assetFilter . next ( null ) ;
459481 }
0 commit comments