/** Angular Imports */ import { Component , OnInit } from '@angular/core' ; import { FormGroup , FormBuilder , FormControl , Validators } from '@angular/forms' ; import { Router , ActivatedRoute } from '@angular/router' ; import { DatePipe } from '@angular/common' ; /** Custom Services */ import { GroupsService } from '../groups.service' ; import { SettingsService } from 'app/settings/settings.service' ; /**
 * Edit Group component.
 */ @ Component ( { selector : 'mifosx-edit-group' , templateUrl : './edit-group.component.html' , styleUrls : [ './edit-group.component.scss' ] } ) export class EditGroupComponent implements OnInit { /** Minimum date allowed. */ minDate = new Date ( 2000 , 0 , 1 ) ; /** Maximum date allowed. */ maxDate = new Date ( ) ; /** Edit Group form. */ editGroupForm : FormGroup ; /** Staff data. */ staffData : any ; /** Group Data */ groupData : any ; /** Submitted On Date */ submittedOnDate : any ; param { FormBuilder } formBuilder param { ActivatedRoute } route param { Router } router param { GroupsService } groupService param { DatePipe } datePipe param { SettingsService } settingsService constructor ( private formBuilder , private route , private router , private groupService , private datePipe , private settingsService ) { this . route . data . subscribe ( ( data ) => { this . staffData = data . groupAndTemplateData . staffOptions ; this . groupData = data . groupAndTemplateData ; this . submittedOnDate = data . groupViewData . timeline . submittedOnDate && new Date ( data . groupViewData . timeline . submittedOnDate ) ; } ) ; } /**
     * Creates and sets the edit group form.
     */ ngOnInit ( ) { this . createEditGroupForm ( ) ; this . editGroupForm . patchValue ( { 'name' : this . groupData . name , 'submittedOnDate' : this . submittedOnDate , 'staffId' : this . groupData . staffId , 'externalId' : this . groupData . externalId } ) ; } /**
     * Creates the edit group form.
     */ createEditGroupForm ( ) { this . editGroupForm = this . formBuilder . group ( { 'name' : [ '' , [ Validators . required , Validators . pattern ( '(^[A-z]).*' ) ] ] , 'submittedOnDate' : [ '' , Validators . required ] , 'staffId' : [ '' ] , 'externalId' : [ '' ] } ) ; this . buildDependencies ( ) ; } /**
     * Adds form control Activation Date if group is active.
     */ buildDependencies ( ) { if ( this . groupData . active ) { this . editGroupForm . addControl ( 'activationDate' , new FormControl ( '' , Validators . required ) ) ; this . editGroupForm . get ( 'activationDate' ) . patchValue ( this . groupData . activationDate && new Date ( this . groupData . activationDate ) ) ; } else { this . editGroupForm . removeControl ( 'activationDate' ) ; } } /**
     * Submits the group form and edits group,
     * if successful redirects to groups.
     */ submit ( ) { const submittedOnDate = this . editGroupForm . value . submittedOnDate ; const activationDate = this . editGroupForm . value . activationDate ; const dateFormat = this . settingsService . dateFormat ; this . editGroupForm . patchValue ( { submittedOnDate : this . datePipe . transform ( submittedOnDate , dateFormat ) , activationDate : activationDate && this . datePipe . transform ( activationDate , dateFormat ) } ) ; const group = this . editGroupForm . value ; group . locale = this . settingsService . language . code ; group . dateFormat = dateFormat ; this . groupService . updateGroup ( group , this . groupData . id ) . subscribe ( ( response ) => { this . router . navigate ( [ '../' ] , { relativeTo : this . route } ) ; } ) ; } }