/** Angular Imports */ import { Component , OnInit } from '@angular/core' ; import { ActivatedRoute , Router } from '@angular/router' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; /** Custom Services */ import { UsersService } from '../users.service' ; /**
 * Edit User Component.
 */ @ Component ( { selector : 'mifosx-edit-user' , templateUrl : './edit-user.component.html' , styleUrls : [ './edit-user.component.scss' ] } ) export class EditUserComponent implements OnInit { /** User Data */ userData : any ; /** Offices Data */ officesData : any ; /** Staff Data */ staffData : any ; /** Roles Data */ rolesData : any ; /** Edit User form. */ editUserForm : FormGroup ; param { FormBuilder } formBuilder param { UsersService } UsersService param { ActivatedRoute } route param { Router } router constructor ( private formBuilder , private usersService , private route , private router ) { this . route . data . subscribe ( ( data ) => { this . userData = data . user ; this . officesData = data . usersTemplate . allowedOffices ; this . rolesData = data . usersTemplate . availableRoles ; } ) ; } ngOnInit ( ) { this . createEditUserForm ( ) ; this . officeChanged ( this . userData . officeId ) ; } /**
     * Creates the edit user form.
     */ createEditUserForm ( ) { const staffId = this . userData . staff ? this . userData . staff . id : null ; this . editUserForm = this . formBuilder . group ( { 'username' : [ this . userData . username , Validators . required ] , 'email' : [ this . userData . email , [ Validators . required , Validators . email ] ] , 'firstname' : [ this . userData . firstname , [ Validators . required , Validators . pattern ( '(^[A-z]).*' ) ] ] , 'lastname' : [ this . userData . lastname , [ Validators . required , Validators . pattern ( '(^[A-z]).*' ) ] ] , 'passwordNeverExpires' : [ this . userData . passwordNeverExpires ] , 'officeId' : [ this . userData . officeId , Validators . required ] , 'staffId' : [ staffId ] , 'roles' : [ this . userData . selectedRoles . map ( ( role ) => role . id ) , Validators . required ] } ) ; } param officeId officeChanged ( officeId ) { this . staffData = [ ] ; this . usersService . getStaff ( officeId ) . subscribe ( ( staff ) => { this . staffData = staff ; } ) ; } /**
     * Submits the user form and edits the user,
     * if successful redirects to the updated user.
     */ submit ( ) { const editedUser = this . editUserForm . value ; this . usersService . editUser ( this . userData . id , editedUser ) . subscribe ( ( response ) => { this . router . navigate ( [ '../../' , response . resourceId ] , { relativeTo : this . route } ) ; } ) ; } }