/** Angular Imports */ import { Component , OnInit , ViewChild } from '@angular/core' ; import { ActivatedRoute } from '@angular/router' ; import { MatDialog } from '@angular/material/dialog' ; import { FormBuilder , Validators } from '@angular/forms' ; /** Custom Dialogs */ import { DeleteDialogComponent } from '../../../shared/delete-dialog/delete-dialog.component' ; /** Custom Services */ import { OrganizationService } from '../../organization.service' ; /**
 * Manage Currencies component.
 */ @ Component ( { selector : 'mifosx-manage-currencies' , templateUrl : './manage-currencies.component.html' , styleUrls : [ './manage-currencies.component.scss' ] } ) export class ManageCurrenciesComponent implements OnInit { /** Selected Currencies data. */ selectedCurrencies : any [ ] ; /** Currency options data */ currencyData : any ; /** Currency form */ currencyForm : any ; /** Currency form reference */ @ ViewChild ( 'formRef' , { static : true } ) formRef : any ; param { ActivatedRoute } route param { FormBuilder } formBuilder param { OrganizationService } organizationservice param { MatDialog } dialog constructor ( private route , private formBuilder , private organizationservice , public dialog ) { this . route . parent . data . subscribe ( ( data ) => { this . selectedCurrencies = data . currencies . selectedCurrencyOptions ; this . currencyData = data . currencies . currencyOptions ; } ) ; } ngOnInit ( ) { this . createCurrencyForm ( ) ; } /**
     * Creates the currency form.
     */ createCurrencyForm ( ) { this . currencyForm = this . formBuilder . group ( { 'currency' : [ '' , Validators . required ] } ) ; } /**
     * Adds a new currency to the list.
     */ addCurrency ( ) { const newCurrency = this . currencyForm . value . currency ; const selectedCurrencyCodes = this . selectedCurrencies . map ( currency => currency . code ) ; if ( ! selectedCurrencyCodes . includes ( newCurrency . code ) ) { selectedCurrencyCodes . push ( newCurrency . code ) ; this . organizationservice . updateCurrencies ( selectedCurrencyCodes ) . subscribe ( ( response ) => { this . selectedCurrencies . push ( newCurrency ) ; this . formRef . resetForm ( ) ; } ) ; } } param { string } currencyCode param { number } index deleteCurrency ( currencyCode , index ) { const selectedCurrencyCodes = this . selectedCurrencies . map ( currency => currency . code ) ; selectedCurrencyCodes . splice ( index , 1 ) ; const deleteCurrencyDialogRef = this . dialog . open ( DeleteDialogComponent , { data : { deleteContext : `currency: ${ currencyCode }` } } ) ; deleteCurrencyDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . delete ) { this . organizationservice . updateCurrencies ( selectedCurrencyCodes ) . subscribe ( ( ) => { this . selectedCurrencies . splice ( index , 1 ) ; this . formRef . resetForm ( ) ; } ) ; } } ) ; } }