/** Angular Imports */ import { Component , OnInit } from '@angular/core' ; import { FormGroup , FormBuilder , Validators , FormControl } from '@angular/forms' ; import { DatePipe } from '@angular/common' ; import { ActivatedRoute , Router } from '@angular/router' ; /** Custom Services */ import { CentersService } from 'app/centers/centers.service' ; import { SettingsService } from 'app/settings/settings.service' ; /**
 * Center Meetings Component
 */ @ Component ( { selector : 'mifosx-attach-center-meeting' , templateUrl : './attach-center-meeting.component.html' , styleUrls : [ './attach-center-meeting.component.scss' ] } ) export class AttachCenterMeetingComponent implements OnInit { /** Minimum date allowed. */ minDate = new Date ( 2000 , 0 , 1 ) ; /** Maximum date allowed. */ maxDate = new Date ( ) ; /** Center Meeting form. */ centerMeetingForm : FormGroup ; /** Calnedar Template Data */ calendarTemplate : any ; /** Center Id */ centerId : any ; /** Repetition Intervals */ repetitionIntervals : any [ ] ; /** Frequency Options */ frequencyOptions : any ; /** Repetition Days Data */ repeatsOnDays : any ; param { FormBuilder } formBuilder param { CentersService } centersService param { SettingsService } settingsService param { DatePipe } datePipe param { ActivatedRoute } route param { Router } router constructor ( private formBuilder , private centersService , private settingsService , private datePipe , private route , private router ) { this . route . data . subscribe ( ( data ) => { this . calendarTemplate = data . centersActionData ; this . frequencyOptions = this . calendarTemplate . frequencyOptions ; this . repeatsOnDays = this . calendarTemplate . repeatsOnDayOptions ; } ) ; this . centerId = this . route . parent . snapshot . params [ 'centerId' ] ; } ngOnInit ( ) { this . createCenterMeetingForm ( ) ; this . buildDependencies ( ) ; } /**
     * Creates the Center Meeting form.
     */ createCenterMeetingForm ( ) { this . centerMeetingForm = this . formBuilder . group ( { 'startDate' : [ '' , Validators . required ] , 'repeating' : [ false ] } ) ; } /**
     * Subscribes to value changes of controls.
     */ buildDependencies ( ) { this . centerMeetingForm . get ( 'repeating' ) . valueChanges . subscribe ( ( value ) => { if ( value ) { this . centerMeetingForm . addControl ( 'frequency' , new FormControl ( ) ) ; this . centerMeetingForm . addControl ( 'interval' , new FormControl ( ) ) ; this . centerMeetingForm . get ( 'frequency' ) . valueChanges . subscribe ( ( frequency ) => { this . centerMeetingForm . removeControl ( 'repeatsOnDay' ) ; switch ( frequency ) { case 1 : this . repetitionIntervals = [ '1' , '2' , '3' ] ; break ; case 2 : this . repetitionIntervals = [ '1' , '2' , '3' ] ; this . centerMeetingForm . addControl ( 'repeatsOnDay' , new FormControl ( '' , Validators . required ) ) ; break ; case 3 : this . repetitionIntervals = [ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' ] ; break ; case 4 : this . repetitionIntervals = [ '1' , '2' , '3' , '4' , '5' ] ; break ; } } ) ; this . centerMeetingForm . patchValue ( { 'frequency' : 1 , 'interval' : '1' } ) ; } else { this . centerMeetingForm . removeControl ( 'frequency' ) ; this . centerMeetingForm . removeControl ( 'interval' ) ; } } ) ; } /**
     * Submits the form and attatches the meeting.
     */ submit ( ) { const locale = this . settingsService . language . code ; const dateFormat = this . settingsService . dateFormat ; const title = `centers_${ this . centerId }_CollectionMeeting` ; const typeId = '1' ; const prevStartDate = this . centerMeetingForm . value . startDate ; this . centerMeetingForm . patchValue ( { startDate : this . datePipe . transform ( prevStartDate , dateFormat ) , } ) ; const data = { ... this . centerMeetingForm . value , title , typeId , dateFormat , locale } ; this . centersService . createCenterMeeting ( this . centerId , data ) . subscribe ( ( response ) => { this . router . navigate ( [ '../../' ] , { relativeTo : this . route } ) ; } ) ; } }