import { Component , OnInit , ViewChild , AfterViewInit } from '@angular/core' ; import { Route , ActivatedRoute , Router } from '@angular/router' ; import { User , BusinessAccount } from '../../../models/data.models' ; import { DataService } from '../../../services/data.service' ; import { InfoDialogService } from '../../../services/info-dialog.service' ; import { MatDialog } from '@angular/material' ; import { CreateUserComponent , UserDialogParam , UserDialogMode } from '../../shared/create-user/create-user.component' ; import { AppHeaderBarComponent } from '../../shared/app-header-bar/app-header-bar.component' ; import { UpdatePasswordComponent } from '../../shared/update-password/update-password.component' ; @ Component ( { selector : 'app-users' , templateUrl : './users.component.html' , styleUrls : [ './users.component.css' ] } ) export class UsersComponent implements OnInit , AfterViewInit { ngAfterViewInit ( ) { this . appHeader . afterInit = ( ) => { this . route . queryParamMap . subscribe ( x => { let bizId = x . get ( 'bizId' ) ; if ( bizId ) { this . bizId = bizId ; this . loadUsers ( ) ; this . loadBusinessDetails ( ) ; } } ) ; } ; this . appHeader . goBack = ( ) => { if ( this . dataService . isSuperAdmin ( ) ) { this . router . navigateByUrl ( '/manage-users' ) ; } else { this . router . navigateByUrl ( '/' ) ; } } ; } @ ViewChild ( AppHeaderBarComponent ) appHeader : AppHeaderBarComponent ; businessAccount : BusinessAccount ; bizId : string ; constructor ( private route , private router , private infoDialog , private dialog , private dataService ) { } ngOnInit ( ) { } search : string = "" ; searchStart ( ) { this . page = 0 ; this . loadUsers ( ) ; } loadBusinessDetails ( ) { this . infoDialog . showSpinner ( ) ; this . dataService . getBusinessDetails ( this . bizId ) . subscribe ( x => { this . infoDialog . hideSpinner ( ) ; this . businessAccount = x . data ; } , err => { this . infoDialog . hideSpinner ( ) ; this . dataService . handleError ( err , "Unable to load business details" , "Something went wrong while trying to load business account details. Please try again." ) ; } ) ; } createUserDialog ( ) { let d = this . dialog . open ( CreateUserComponent , { width : '60%' , data : < UserDialogParam > { bizId : this . bizId , mode : UserDialogMode . Create , } } ) ; d . afterClosed ( ) . subscribe ( x => { if ( x == true ) this . loadUsers ( ) ; } ) ; } users : User [ ] = [ ] ; page : number = 0 ; totalPages : number = 0 ; prevPage ( ) { if ( this . page > 0 ) { this . page -- ; this . loadUsers ( ) ; } } nextPage ( ) { if ( this . page < this . totalPages ) { this . page ++ ; this . loadUsers ( ) ; } } view ( user ) { this . dialog . open ( CreateUserComponent , { width : '60%' , data : < UserDialogParam > { mode : UserDialogMode . View , user : user } } ) ; } loadUsers ( ) { if ( this . bizId ) { this . infoDialog . showSpinner ( ) ; this . dataService . getUsers ( this . bizId , this . search , this . page ) . subscribe ( x => { this . infoDialog . hideSpinner ( ) ; this . users = x . content ; this . totalPages = x . totalPages ; } , err => { this . infoDialog . hideSpinner ( ) ; this . dataService . handleError ( err , "Unable to load users" , "Something went wrong while loading the users. Please try again." ) ; } ) ; } } updateUserPassword ( user ) { this . dialog . open ( UpdatePasswordComponent , { width : '40%' , data : user } ) ; } userRole ( user ) { if ( user . roles ) { return user . roles . map ( x => x . label ) . join ( ', ' ) ; } return "" ; } }