Files
brewman/overlord/src/app/account/account-detail/account-detail.component.ts
tanshu 8996516978 Added loading bar
Fixed product, account and employee details for add new.
2018-06-10 13:58:01 +05:30

122 lines
3.3 KiB
TypeScript

import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {ToasterService} from '../../core/toaster.service';
import {ActivatedRoute, Router} from '@angular/router';
import {AccountService} from '../account.service';
import {Account} from '../account';
import {AccountType} from '../account-type';
import {CostCentre} from '../../cost-centre/cost-centre';
import {ConfirmDialogComponent} from '../../shared/confirm-dialog/confirm-dialog.component';
import {MatDialog} from '@angular/material';
import {FormBuilder, FormGroup} from '@angular/forms';
@Component({
selector: 'app-account-detail',
templateUrl: './account-detail.component.html',
styleUrls: ['./account-detail.component.css']
})
export class AccountDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement') nameElement: ElementRef;
form: FormGroup;
accountTypes: AccountType[];
costCentres: CostCentre[];
item: Account;
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private fb: FormBuilder,
private toaster: ToasterService,
private ser: AccountService
) {
this.createForm();
}
createForm() {
this.form = this.fb.group({
code: {value: '', disabled: true},
name: '',
type: '',
isActive: '',
isReconcilable: '',
costCentre: ''
});
}
ngOnInit() {
this.route.data
.subscribe((data: { 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(() => {
this.nameElement.nativeElement.focus();
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem())
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/Accounts');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
delete() {
this.ser.delete(this.item.id)
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/Accounts');
},
(error) => {
this.toaster.show('Danger', error.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.id = formModel.type;
this.item.isActive = formModel.isActive;
this.item.isReconcilable = formModel.isReconcilable;
this.item.costCentre.id = formModel.costCentre;
return this.item;
}
}