import { Component , OnInit , OnDestroy } from '@angular/core' ; import { HttpResponse } from '@angular/common/http' ; import { NgbModal } from '@ng-bootstrap/ng-bootstrap' ; import { ActivatedRoute , Router } from '@angular/router' ; import { JhiEventManager , JhiParseLinks , JhiAlertService } from 'ng-jhipster' ; import { ITEMS_PER_PAGE } from 'app/shared' ; import { AccountService , UserService , User } from 'app/core' ; import { UserMgmtDeleteDialogComponent } from './user-management-delete-dialog.component' ; @ Component ( { selector : 'jhi-user-mgmt' , templateUrl : './user-management.component.html' } ) export class UserMgmtComponent implements OnInit , OnDestroy { currentAccount : any ; users : User [ ] ; error : any ; success : any ; routeData : any ; links : any ; totalItems : any ; itemsPerPage : any ; page : any ; predicate : any ; previousPage : any ; reverse : any ; constructor ( private userService , private alertService , private accountService , private parseLinks , private activatedRoute , private router , private eventManager , private modalService ) { this . itemsPerPage = ITEMS_PER_PAGE ; this . routeData = this . activatedRoute . data . subscribe ( data => { this . page = data [ 'pagingParams' ] . page ; this . previousPage = data [ 'pagingParams' ] . page ; this . reverse = data [ 'pagingParams' ] . ascending ; this . predicate = data [ 'pagingParams' ] . predicate ; } ) ; } ngOnInit ( ) { this . accountService . identity ( ) . then ( account => { this . currentAccount = account ; this . loadAll ( ) ; this . registerChangeInUsers ( ) ; } ) ; } ngOnDestroy ( ) { this . routeData . unsubscribe ( ) ; } registerChangeInUsers ( ) { this . eventManager . subscribe ( 'userListModification' , response => this . loadAll ( ) ) ; } setActive ( user , isActivated ) { user . activated = isActivated ; this . userService . update ( user ) . subscribe ( response => { if ( response . status === 200 ) { this . error = null ; this . success = 'OK' ; this . loadAll ( ) ; } else { this . success = null ; this . error = 'ERROR' ; } } ) ; } loadAll ( ) { this . userService . query ( { page : this . page - 1 , size : this . itemsPerPage , sort : this . sort ( ) } ) . subscribe ( ( res ) => this . onSuccess ( res . body , res . headers ) , ( res ) => this . onError ( res . body ) ) ; } trackIdentity ( index , item ) { return item . id ; } sort ( ) { const result = [ this . predicate + ',' + ( this . reverse ? 'asc' : 'desc' ) ] ; if ( this . predicate !== 'id' ) { result . push ( 'id' ) ; } return result ; } loadPage ( page ) { if ( page !== this . previousPage ) { this . previousPage = page ; this . transition ( ) ; } } transition ( ) { this . router . navigate ( [ '/admin/user-management' ] , { queryParams : { page : this . page , sort : this . predicate + ',' + ( this . reverse ? 'asc' : 'desc' ) } } ) ; this . loadAll ( ) ; } deleteUser ( user ) { const modalRef = this . modalService . open ( UserMgmtDeleteDialogComponent , { size : 'lg' , backdrop : 'static' } ) ; modalRef . componentInstance . user = user ; modalRef . result . then ( result => { } , reason => { } ) ; } private onSuccess ( data , headers ) { this . links = this . parseLinks . parse ( headers . get ( 'link' ) ) ; this . totalItems = headers . get ( 'X-Total-Count' ) ; this . users = data ; } private onError ( error ) { this . alertService . error ( error . error , error . message , null ) ; } }