import { Component , OnInit , ViewChild , ElementRef } from '@angular/core' ; import { DIDService } from './did.service' ; import { MatSort , MatPaginator } from '@angular/material' ; import { DIDDatabase } from './did-database.component' ; import { DIDDataSource } from './did-datasource.component' ; import { ModalComponent } from '../../modal.component' ; import { NgbModal , ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap' ; @ Component ( { selector : 'ngx-did-component' , templateUrl : './did-component.html' , styleUrls : [ './did-component.scss' ] , } ) export class FormsDIDComponent implements OnInit { constructor ( private did_service , private modalService ) { } aDID : DIDDataSource | null ; length : number ; closeResult : any ; displayedColumns = [ 'phone' , 'first_name' , 'Operations' ] ; @ ViewChild ( MatSort , { static : false } ) sort : MatSort ; @ ViewChild ( MatPaginator , { static : false } ) paginator : MatPaginator ; @ ViewChild ( 'filter' , { static : false } ) filter : ElementRef ; ngOnInit ( ) { this . getDIDlist ( ) ; } getDIDlist ( ) { this . did_service . get_DIDList ( ) . then ( data => { this . length = data . length ; this . aDID = new DIDDataSource ( new DIDDatabase ( data ) , this . sort , this . paginator ) ; } ) ; } deleteDID ( account_id ) { this . did_service . delete_DID ( account_id ) . then ( response => { } ) . catch ( this . handleError ) ; this . getDIDlist ( ) ; } showStaticModal ( name , account_id ) { const activeModal = this . modalService . open ( ModalComponent , { size : 'sm' , container : 'nb-layout' , } ) ; activeModal . componentInstance . modalHeader = 'Alert' ; activeModal . componentInstance . modalContent = `Are you sure you want to delete ${ name }?` ; activeModal . result . then ( ( result ) => { this . closeResult = result ; if ( this . closeResult === 'yes_click' ) { this . deleteDID ( account_id ) ; } } , ( reason ) => { this . closeResult = this . getDismissReason ( reason ) ; } ) ; } private getDismissReason ( reason ) { if ( reason === ModalDismissReasons . ESC ) { return 'by pressing ESC' ; } else if ( reason === ModalDismissReasons . BACKDROP_CLICK ) { return 'by clicking on a backdrop' ; } else { return `with: ${ reason }` ; } } private handleError ( error ) { console . error ( 'An error occurred' , error ) ; return Promise . reject ( error . message || error ) ; } }