import { Component, OnInit, OnDestroy, Inject } from '@angular/core'; import { ModalService, UserService, BomModel, BomService } from '@arrow/bom/core'; import { ConfigService } from '@arrow/bom/config'; import { LoggerService } from '@arrow/bom/logger'; import { WPT_DIALOG_DATA } from '@arrow/warpaint/dialog'; import { take } from 'rxjs/operators'; @Component({ selector: 'bom-revision-history', templateUrl: 'revision-history.component.html', styleUrls: [ '../../../core/src/style/modal.scss', './revision-history.component.scss' ] }) export class RevisionHistoryComponent implements OnInit { public history: Array; public error: boolean; public loading = true; constructor( private _modalService: ModalService, private _userService: UserService, private _configService: ConfigService, private _log: LoggerService, private _bomService: BomService, @Inject(WPT_DIALOG_DATA) public data: BomModel ) {} ngOnInit(): void { this._bomService .getBomHistory() .pipe(take(1)) .subscribe( (data: any) => { if (data.status.statusCode === 401) { const url = encodeURIComponent(location.href); location.replace( `${this._configService.getRoute('login')}?url=${url}` ); } else { // Grab date/time of last bom history view const bomHistoryLastView = this._userService.getBomHistoryLastViewed( this.data.id ); // Update date/time this._userService.updateBomHistoryLastViewed(this.data.id); try { this.history = data.payload.reverse(); this.loading = false; } catch (e) { this.error = true; this._log.error(e); } if (!this._userService.isAnonymous) { for (let ii = 0; ii < this.history.length; ii++) { const itemViewDate = new Date( this.history[ii].eventDate ).getTime(); if (itemViewDate > bomHistoryLastView) { const iiAuthorEmail: string = this.history[ii].authorEmail; if ( iiAuthorEmail && iiAuthorEmail !== this._userService.email && iiAuthorEmail.toLowerCase() !== 'anonymous' ) { this.history[ii].isNew = true; } } } } } }, (error: any) => this._handleError(error) ); } /** * * @param error */ private _handleError(error: Error): any { this.error = true; this.loading = false; this._log.warn(error); } }