import { Component , OnInit } from '@angular/core' ; import { HttpHeaders , HttpResponse } from '@angular/common/http' ; import { NgbModal } from '@ng-bootstrap/ng-bootstrap' ; import { IOperation } from '../operation.model' ; import { ASC , DESC , ITEMS_PER_PAGE } from 'app/config/pagination.constants' ; import { OperationService } from '../service/operation.service' ; import { OperationDeleteDialogComponent } from '../delete/operation-delete-dialog.component' ; import { ParseLinks } from 'app/core/util/parse-links.service' ; @ Component ( { selector : 'jhi-operation' , templateUrl : './operation.component.html' , } ) export class OperationComponent implements OnInit { operations : IOperation [ ] ; isLoading = false ; itemsPerPage : number ; links : { [ key ] : number ; } ; page : number ; predicate : string ; ascending : boolean ; constructor ( protected operationService , protected modalService , protected parseLinks ) { this . operations = [ ] ; this . itemsPerPage = ITEMS_PER_PAGE ; this . page = 0 ; this . links = { last : 0 , } ; this . predicate = 'id' ; this . ascending = true ; } loadAll ( ) { this . isLoading = true ; this . operationService . query ( { page : this . page , size : this . itemsPerPage , sort : this . sort ( ) , } ) . subscribe ( { next : ( res ) => { this . isLoading = false ; this . paginateOperations ( res . body , res . headers ) ; } , error : ( ) => { this . isLoading = false ; } , } ) ; } reset ( ) { this . page = 0 ; this . operations = [ ] ; this . loadAll ( ) ; } loadPage ( page ) { this . page = page ; this . loadAll ( ) ; } ngOnInit ( ) { this . loadAll ( ) ; } trackId ( index , item ) { return item . id ! ; } delete ( operation ) { const modalRef = this . modalService . open ( OperationDeleteDialogComponent , { size : 'lg' , backdrop : 'static' } ) ; modalRef . componentInstance . operation = operation ; modalRef . closed . subscribe ( reason => { if ( reason === 'deleted' ) { this . reset ( ) ; } } ) ; } protected sort ( ) { const result = [ this . predicate + ',' + ( this . ascending ? ASC : DESC ) ] ; if ( this . predicate !== 'id' ) { result . push ( 'id' ) ; } return result ; } protected paginateOperations ( data , headers ) { const linkHeader = headers . get ( 'link' ) ; if ( linkHeader ) { this . links = this . parseLinks . parse ( linkHeader ) ; } else { this . links = { last : 0 , } ; } if ( data ) { for ( const d of data ) { this . operations . push ( d ) ; } } } }