import { Component , Input , OnChanges , OnDestroy , SimpleChanges } from '@angular/core' ; import { CmdbType } from '../../../models/cmdb-type' ; import { ObjectService } from '../../../services/object.service' ; import { ReplaySubject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { NgbModal , NgbModalRef } from '@ng-bootstrap/ng-bootstrap' ; import { CleanupModalComponent } from '../../modals/cleanup-modal/cleanup-modal.component' ; import { ToastService } from '../../../../layout/toast/toast.service' ; @ Component ( { selector : 'cmdb-clean-button' , templateUrl : './clean-button.component.html' , styleUrls : [ './clean-button.component.scss' ] } ) export class CleanButtonComponent implements OnChanges , OnDestroy { /**
     * Component un-subscriber.
     */ private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; /**
     * Type of the cleanable objects.
     */ @ Input ( ) public type : CmdbType ; /**
     * Object clean status.
     */ public clean : boolean = false ; /**
     * Is the clean status loading.
     */ public loading : boolean = true ; /**
     * Cleanup modal
     */ private modalRef : NgbModalRef ; /**
     * Constructor of `CleanButtonComponent`.
     */ constructor ( private objectService , private modalService , private toastService ) { } param changes public ngOnChanges ( changes ) { if ( changes . type && changes . type . currentValue !== changes . type . previousValue ) { this . loadObjectCleanStatus ( ) ; } } /**
     * Triggers the clean status api call.
     */ private loadObjectCleanStatus ( ) { this . loading = true ; this . objectService . getObjectCleanStatus ( this . type . public_id ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( clean ) => { this . clean = clean ; this . loading = false ; } ) ; } /**
     * Aut calls when component destroys.
     * Triggers un-subscribe from api calls.
     */ public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; if ( this . modalRef ) { this . modalRef . close ( ) ; } } /**
     * Open the clean up modal and handles the close result.
     */ public openModal ( ) { this . modalRef = this . modalService . open ( CleanupModalComponent ) ; this . modalRef . componentInstance . type = this . type ; this . modalRef . result . then ( ( result ) => { if ( result === 'Clean' ) { this . toastService . success ( `Objects of ${ this . type . label } were cleaned up!` ) ; this . loadObjectCleanStatus ( ) ; } } ) ; } }