From e01897aa66f20489d88d6009a2545d48f3653173 Mon Sep 17 00:00:00 2001 From: Karan Takalkar Date: Tue, 23 Jun 2020 00:04:50 +0530 Subject: [PATCH] feat: add manage currencies page (#887) Fixes: #152 --- .../currencies/currencies.component.html | 2 +- .../manage-currencies.component.html | 49 +++++++++ .../manage-currencies.component.scss | 3 + .../manage-currencies.component.spec.ts | 25 +++++ .../manage-currencies.component.ts | 101 ++++++++++++++++++ .../organization-routing.module.ts | 15 ++- src/app/organization/organization.module.ts | 4 +- src/app/organization/organization.service.ts | 8 ++ 8 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 src/app/organization/currencies/manage-currencies/manage-currencies.component.html create mode 100644 src/app/organization/currencies/manage-currencies/manage-currencies.component.scss create mode 100644 src/app/organization/currencies/manage-currencies/manage-currencies.component.spec.ts create mode 100644 src/app/organization/currencies/manage-currencies/manage-currencies.component.ts diff --git a/src/app/organization/currencies/currencies.component.html b/src/app/organization/currencies/currencies.component.html index c116a7065..571946c19 100644 --- a/src/app/organization/currencies/currencies.component.html +++ b/src/app/organization/currencies/currencies.component.html @@ -1,5 +1,5 @@
- diff --git a/src/app/organization/currencies/manage-currencies/manage-currencies.component.html b/src/app/organization/currencies/manage-currencies/manage-currencies.component.html new file mode 100644 index 000000000..b53b77d69 --- /dev/null +++ b/src/app/organization/currencies/manage-currencies/manage-currencies.component.html @@ -0,0 +1,49 @@ +
+ + + +
+ +
+ + + Currency + + + {{ currency.name }} + + + + Currency is required + + + + + +
+ +
+ + + + + + + +
+ +
+ +
+ +
+ +
+ +
diff --git a/src/app/organization/currencies/manage-currencies/manage-currencies.component.scss b/src/app/organization/currencies/manage-currencies/manage-currencies.component.scss new file mode 100644 index 000000000..5911b9d88 --- /dev/null +++ b/src/app/organization/currencies/manage-currencies/manage-currencies.component.scss @@ -0,0 +1,3 @@ +.container{ + width: 74rem; +} diff --git a/src/app/organization/currencies/manage-currencies/manage-currencies.component.spec.ts b/src/app/organization/currencies/manage-currencies/manage-currencies.component.spec.ts new file mode 100644 index 000000000..fbae113a6 --- /dev/null +++ b/src/app/organization/currencies/manage-currencies/manage-currencies.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ManageCurrenciesComponent } from './manage-currencies.component'; + +describe('ManageCurrenciesComponent', () => { + let component: ManageCurrenciesComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ManageCurrenciesComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ManageCurrenciesComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/organization/currencies/manage-currencies/manage-currencies.component.ts b/src/app/organization/currencies/manage-currencies/manage-currencies.component.ts new file mode 100644 index 000000000..58069806a --- /dev/null +++ b/src/app/organization/currencies/manage-currencies/manage-currencies.component.ts @@ -0,0 +1,101 @@ +/** Angular Imports */ +import { Component, OnInit, ViewChild } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { MatDialog } from '@angular/material'; +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') formRef: any; + + /** + * Retrieves the currency data from `resolve`. + * @param {ActivatedRoute} route Activated Route + * @param {FormBuilder} formBuilder Form Builder + * @param {OrganizationService} organizationservice Organization Service + * @param {MatDialog} dialog Mat Dialog + */ + constructor(private route: ActivatedRoute, + private formBuilder: FormBuilder, + private organizationservice: OrganizationService, + public dialog: MatDialog) { + this.route.parent.data.subscribe(( data: { currencies: any }) => { + 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: any[] = this.selectedCurrencies.map(currency => currency.code); + if (!selectedCurrencyCodes.includes(newCurrency.code)) { + selectedCurrencyCodes.push(newCurrency.code); + this.organizationservice.updateCurrencies(selectedCurrencyCodes) + .subscribe((response: any) => { + this.selectedCurrencies.push(newCurrency); + this.formRef.resetForm(); + }); + } + } + + /** + * Deletes Currency + * @param {string} currencyCode Currency Code + * @param {number} index Index + */ + deleteCurrency(currencyCode: string, index: number) { + const selectedCurrencyCodes: any[] = this.selectedCurrencies.map(currency => currency.code); + selectedCurrencyCodes.splice(index, 1); + const deleteCurrencyDialogRef = this.dialog.open(DeleteDialogComponent, { + data: { deleteContext: `currency: ${currencyCode}` } + }); + deleteCurrencyDialogRef.afterClosed().subscribe((response: any) => { + if (response.delete) { + this.organizationservice.updateCurrencies(selectedCurrencyCodes) + .subscribe(() => { + this.selectedCurrencies.splice(index, 1); + this.formRef.resetForm(); + }); + } + }); + } + +} diff --git a/src/app/organization/organization-routing.module.ts b/src/app/organization/organization-routing.module.ts index 12b051ccb..c6e9c3d82 100644 --- a/src/app/organization/organization-routing.module.ts +++ b/src/app/organization/organization-routing.module.ts @@ -40,6 +40,7 @@ import { GeneralTabComponent } from './offices/view-office/general-tab/general-t import { DatatableTabsComponent } from './offices/view-office/datatable-tabs/datatable-tabs.component'; import { ViewCampaignComponent } from './sms-campaigns/view-campaign/view-campaign.component'; import { ManageFundsComponent } from './manage-funds/manage-funds.component'; +import { ManageCurrenciesComponent } from './currencies/manage-currencies/manage-currencies.component'; /** Custom Resolvers */ import { LoanProvisioningCriteriaResolver } from './loan-provisioning-criteria/loan-provisioning-criteria.resolver'; @@ -218,11 +219,21 @@ const routes: Routes = [ }, { path: 'currencies', - component: CurrenciesComponent, data: { title: extract('Currency Configuration'), breadcrumb: 'Currency Configuration' }, resolve: { currencies: CurrenciesResolver - } + }, + children: [ + { + path: '', + component: CurrenciesComponent, + }, + { + path: 'manage', + data: { title: extract('Manage Currencies'), breadcrumb: 'Manage Currencies' }, + component: ManageCurrenciesComponent + } + ] }, { path: 'sms-campaigns', diff --git a/src/app/organization/organization.module.ts b/src/app/organization/organization.module.ts index 09b820c95..73c53b577 100644 --- a/src/app/organization/organization.module.ts +++ b/src/app/organization/organization.module.ts @@ -43,6 +43,7 @@ import { SingleRowComponent } from './offices/view-office/datatable-tabs/single- import { MultiRowComponent } from './offices/view-office/datatable-tabs/multi-row/multi-row.component'; import { ViewCampaignComponent } from './sms-campaigns/view-campaign/view-campaign.component'; import { ManageFundsComponent } from './manage-funds/manage-funds.component'; +import { ManageCurrenciesComponent } from './currencies/manage-currencies/manage-currencies.component'; /** * Organization Module @@ -91,7 +92,8 @@ import { ManageFundsComponent } from './manage-funds/manage-funds.component'; SingleRowComponent, MultiRowComponent, ViewCampaignComponent, - ManageFundsComponent + ManageFundsComponent, + ManageCurrenciesComponent ] }) export class OrganizationModule { } diff --git a/src/app/organization/organization.service.ts b/src/app/organization/organization.service.ts index b6805edc5..f3ff55218 100644 --- a/src/app/organization/organization.service.ts +++ b/src/app/organization/organization.service.ts @@ -166,6 +166,14 @@ export class OrganizationService { return this.http.get('/currencies'); } + /** + * @param {any[]} currencies + * @returns {Observable} Currencies data + */ + updateCurrencies(currencies: any[]): Observable { + return this.http.put('/currencies', {currencies}); + } + /** * @returns {Observable} SMS Campaigns data */