/** Angular Imports */ import { Component , OnInit , AfterViewInit } from '@angular/core' ; import { FormGroup , FormBuilder , FormControl , Validators } from '@angular/forms' ; import { Router , ActivatedRoute } from '@angular/router' ; /** Custom Dialogs */ import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-dialog.component' ; /** Custom Services */ import { GroupsService } from 'app/groups/groups.service' ; import { ClientsService } from 'app/clients/clients.service' ; import { MatDialog } from '@angular/material/dialog' ; /**
 * Manage Group Members Component
 */ @ Component ( { selector : 'mifosx-manage-group-members' , templateUrl : './manage-group-members.component.html' , styleUrls : [ './manage-group-members.component.scss' ] } ) export class ManageGroupMembersComponent implements AfterViewInit { /** Group Data */ groupData : any ; /** Client data. */ clientsData : any = [ ] ; /** Client Members. */ clientMembers : any [ ] = [ ] ; /** Client Choice. */ clientChoice = new FormControl ( '' ) ; param { ActivatedRoute } route param { GroupsService } groupsService param { ClientsService } clientsService param { MatDialog } dialog constructor ( private route , private groupsService , private clientsService , public dialog ) { this . route . data . subscribe ( ( data ) => { this . groupData = data . groupActionData ; this . clientMembers = data . groupActionData . clientMembers ; } ) ; } /**
     * Subscribes to Clients search filter:
     */ ngAfterViewInit ( ) { this . clientChoice . valueChanges . subscribe ( ( value ) => { if ( value . length >= 2 ) { this . clientsService . getFilteredClients ( 'displayName' , 'ASC' , true , value , this . groupData . officeId ) . subscribe ( ( data ) => { this . clientsData = data . pageItems ; } ) ; } } ) ; } /**
     * Add client.
     */ addClient ( ) { if ( ! this . clientMembers . includes ( this . clientChoice . value ) ) { this . groupsService . executeGroupCommand ( this . groupData . id , 'associateClients' , { clientMembers : [ this . clientChoice . value . id ] } ) . subscribe ( ( ) => { this . clientMembers . push ( this . clientChoice . value ) ; } ) ; } } param { number } index param { any } client removeClient ( index , client ) { const removeMemberDialogRef = this . dialog . open ( DeleteDialogComponent , { data : { deleteContext : `client member: ${ client . displayName }` } } ) ; removeMemberDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . delete ) { this . groupsService . executeGroupCommand ( this . groupData . id , 'disassociateClients' , { clientMembers : [ client . id ] } ) . subscribe ( ( ) => { this . clientMembers . splice ( index , 1 ) ; } ) ; } } ) ; } param { any } client returns { string } displayClient ( client ) { return client ? client . displayName : undefined ; } }