Files
brewman/overlord/src/app/account/account-detail/account-detail.component.ts
T

131 lines
3.8 KiB
TypeScript

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import { Account } from '../../core/account';
import { AccountType } from '../../core/account-type';
import { AccountService } from '../../core/account.service';
import { CostCentre } from '../../core/cost-centre';
import { ToasterService } from '../../core/toaster.service';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
@Component({
selector: 'app-account-detail',
templateUrl: './account-detail.component.html',
styleUrls: ['./account-detail.component.css'],
})
export class AccountDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
form: FormGroup<{
code: FormControl<number | string>;
name: FormControl<string | null>;
type: FormControl<any>;
isActive: FormControl<boolean>;
isReconcilable: FormControl<boolean>;
costCentre: FormControl<string | null>;
}>;
accountTypes: AccountType[] = [];
costCentres: CostCentre[] = [];
item: Account = new Account();
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private toaster: ToasterService,
private ser: AccountService,
) {
this.form = new FormGroup({
code: new FormControl<string | number>({ value: 0, disabled: true }, { nonNullable: true }),
name: new FormControl<string | null>(null),
type: new FormControl<any>(''),
isActive: new FormControl<boolean>(true, { nonNullable: true }),
isReconcilable: new FormControl<boolean>(false, { nonNullable: true }),
costCentre: new FormControl<string | null>(null),
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as {
item: Account;
accountTypes: AccountType[];
costCentres: CostCentre[];
};
this.accountTypes = data.accountTypes;
this.costCentres = data.costCentres;
this.showItem(data.item);
});
}
showItem(item: Account) {
this.item = item;
this.form.setValue({
code: this.item.code || '(Auto)',
name: this.item.name || '',
type: this.item.type,
isActive: this.item.isActive,
isReconcilable: this.item.isReconcilable,
costCentre: this.item.costCentre.id!,
});
}
ngAfterViewInit() {
setTimeout(() => {
if (this.nameElement) {
this.nameElement.nativeElement.focus();
}
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem()).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/accounts');
},
(error) => {
this.toaster.show('Danger', error);
},
);
}
delete() {
this.ser.delete(this.item.id as string).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/accounts');
},
(error) => {
this.toaster.show('Danger', error);
},
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Delete Account?', content: 'Are you sure? This cannot be undone.' },
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): Account {
const formModel = this.form.value;
this.item.name = formModel.name!;
this.item.type = formModel.type;
this.item.isActive = formModel.isActive!;
this.item.isReconcilable = formModel.isReconcilable!;
this.item.costCentre.id = formModel.costCentre!;
return this.item;
}
}