/** Angular Imports */ import { Component , OnInit } from '@angular/core' ; import { ActivatedRoute , Router } from '@angular/router' ; import { FormGroup , FormBuilder , Validators , FormArray } from '@angular/forms' ; import { MatDialog } from '@angular/material/dialog' ; /** Custom Services */ import { SystemService } from 'app/system/system.service' ; /** Custom Components */ import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-dialog.component' ; /**
 * View Code Component.
 */ @ Component ( { selector : 'mifosx-view-code' , templateUrl : './view-code.component.html' , styleUrls : [ './view-code.component.scss' ] } ) export class ViewCodeComponent implements OnInit { /** Code Data */ codeData : any ; /** Code Values Data */ codeValuesData : any [ ] ; /** Code Values Form */ codeValuesForm : FormGroup ; /** Code Value Row Status */ codeValueRowStatus : string [ ] = [ ] ; param { ActivatedRoute } route param { SystemService } systemService param { Router } router param { FormBuilder } formBuilder param { MatDialog } dialog constructor ( private route , private systemService , private router , private formBuilder , private dialog ) { this . route . data . subscribe ( ( data ) => { this . codeData = data . code ; this . codeValuesData = data . codeValues ; } ) ; } /**
     * Creates and initializes the code values form.
     */ ngOnInit ( ) { this . createCodeValuesForm ( ) ; this . initCodeValuesForm ( ) ; } /**
     * Initializes the code values form.
     */ initCodeValuesForm ( ) { this . codeValuesData . forEach ( ( codeValue ) => { this . codeValues . push ( this . createCodeValuesRow ( codeValue ) ) ; this . codeValueRowStatus . push ( 'disabled' ) ; } ) ; } returns { FormArray } get codeValues ( ) : FormArray { return this . codeValuesForm . get ( 'codeValues' ) as FormArray ; } /**
     * Creates the code values form.
     */ createCodeValuesForm ( ) { this . codeValuesForm = this . formBuilder . group ( { 'codeValues' : this . formBuilder . array ( [ ] ) } ) ; } /**
     * Adds a row to the code values form.
     */ addCodeValueRow ( ) { this . codeValues . push ( this . createCodeValuesRow ( ) ) ; this . codeValues . at ( this . codeValues . length - 1 ) . enable ( ) ; this . codeValueRowStatus . push ( 'new' ) ; } param { any } codeValue createCodeValuesRow ( codeValue ? ) { return this . formBuilder . group ( { 'name' : [ { value : codeValue ? codeValue . name : '' , disabled : true } , Validators . required ] , 'description' : [ { value : codeValue ? codeValue . description : '' , disabled : true } ] , 'position' : [ { value : codeValue ? codeValue . position : 0 , disabled : true } , Validators . required ] , 'isActive' : [ { value : codeValue ? codeValue . active : false , disabled : true } ] } ) ; } param { number } index deleteCodeValue ( index ) { const codeValueId = this . codeValuesData [ index ] . id ; this . systemService . deleteCodeValue ( this . codeData . id , codeValueId ) . subscribe ( ( response ) => { this . codeValuesData . splice ( index , 1 ) ; this . codeValues . removeAt ( index ) ; this . codeValueRowStatus . splice ( index , 1 ) ; } ) ; } param { number } index removeNewCodeValue ( index ) { this . codeValues . removeAt ( index ) ; this . codeValueRowStatus . splice ( index , 1 ) ; } param { number } index updateCodeValue ( index ) { const updatedCodeValue = this . codeValues . at ( index ) . value ; this . systemService . updateCodeValue ( this . codeData . id , this . codeValuesData [ index ] . id , updatedCodeValue ) . subscribe ( ( response ) => { this . codeValues . at ( index ) . disable ( ) ; this . codeValueRowStatus [ index ] = 'disabled' ; this . codeValues . at ( index ) . markAsPristine ( ) ; } ) ; } /**
     * Deletes the code.
     */ delete ( ) { const deleteCodeDialogRef = this . dialog . open ( DeleteDialogComponent , { data : { deleteContext : `code ${ this . codeData . name }` } } ) ; deleteCodeDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . delete ) { this . systemService . deleteCode ( this . codeData . id ) . subscribe ( ( ) => { this . router . navigate ( [ '/system/codes' ] ) ; } ) ; } } ) ; } param { number } index disableRow ( index ) { this . codeValues . at ( index ) . get ( 'name' ) . setValue ( this . codeValuesData [ index ] . name ) ; this . codeValues . at ( index ) . get ( 'description' ) . setValue ( this . codeValuesData [ index ] . description ) ; this . codeValues . at ( index ) . get ( 'position' ) . setValue ( this . codeValuesData [ index ] . position ) ; this . codeValues . at ( index ) . get ( 'isActive' ) . setValue ( this . codeValuesData [ index ] . isActive ) ; this . codeValues . at ( index ) . disable ( ) ; this . codeValueRowStatus [ index ] = 'disabled' ; this . codeValues . at ( index ) . markAsPristine ( ) ; } param { number } index addCodeValue ( index ) { const newCodeValue = this . codeValues . at ( index ) . value ; this . systemService . createCodeValue ( this . codeData . id , newCodeValue ) . subscribe ( ( response ) => { this . codeValues . at ( index ) . disable ( ) ; this . codeValueRowStatus [ index ] = 'disabled' ; this . codeValuesData . push ( { id : response . subResourceId , name : this . codeValues . at ( index ) . get ( 'name' ) . value , description : this . codeValues . at ( index ) . get ( 'description' ) . value , position : this . codeValues . at ( index ) . get ( 'position' ) . value , isActive : this . codeValues . at ( index ) . get ( 'isActive' ) . value } ) ; this . codeValues . at ( index ) . markAsPristine ( ) ; } ) ; } param { number } index enableRow ( index ) { this . codeValues . at ( index ) . enable ( ) ; this . codeValueRowStatus [ index ] = 'edit' ; } }