feat: add manage currencies page (#887)

Fixes: #152
This commit is contained in:
Karan Takalkar
2020-06-23 00:04:50 +05:30
committed by GitHub
parent 686d6dddbc
commit e01897aa66
8 changed files with 203 additions and 4 deletions
@@ -1,5 +1,5 @@
<div class="container m-b-20" fxLayout="row" fxLayoutAlign="end" fxLayoutGap="20px">
<button mat-raised-button color="primary">
<button mat-raised-button color="primary" [routerLink]="'manage'">
<fa-icon icon="plus"></fa-icon>&nbsp;&nbsp;
Add/Edit
</button>
@@ -0,0 +1,49 @@
<div class="container">
<mat-card>
<div>
<form #formRef="ngForm" [formGroup]="currencyForm" fxLayout="row" fxLayoutAlign="start baseline" fxLayoutGap="20px"
fxFlex="48%" (ngSubmit)="addCurrency()">
<mat-form-field fxFlex="calc(90%-20px)">
<mat-label>Currency</mat-label>
<mat-select required formControlName="currency">
<mat-option *ngFor="let currency of currencyData" [value]="currency">
{{ currency.name }}
</mat-option>
</mat-select>
<mat-error *ngIf="currencyForm.controls.currency.hasError('required')">
Currency is <strong>required</strong>
</mat-error>
</mat-form-field>
<button mat-raised-button fxFlex color="primary"
[disabled]="!currencyForm.valid">
<fa-icon icon="plus"></fa-icon>
</button>
</form>
</div>
<mat-grid-list cols="2" rowHeight="50px">
<mat-grid-tile *ngFor="let currency of selectedCurrencies; index as i">
<input matInput [value]="currency.name">
<div fxLayout="row" fxLayoutAlign="flex-start">
<button mat-button color="warn" (click)="deleteCurrency(currency.code, i)">
<fa-icon icon="trash"></fa-icon>
</button>
</div>
</mat-grid-tile>
</mat-grid-list>
</mat-card>
</div>
@@ -0,0 +1,3 @@
.container{
width: 74rem;
}
@@ -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<ManageCurrenciesComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ManageCurrenciesComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ManageCurrenciesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -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();
});
}
});
}
}