141 lines
4.1 KiB
TypeScript
141 lines
4.1 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormArray, FormControl, FormGroup } from '@angular/forms';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { round } from 'mathjs';
|
|
|
|
import { Customer } from '../../core/customer';
|
|
import { ToasterService } from '../../core/toaster.service';
|
|
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
|
|
import { CustomerService } from '../customer.service';
|
|
|
|
@Component({
|
|
selector: 'app-customer-detail',
|
|
templateUrl: './customer-detail.component.html',
|
|
styleUrls: ['./customer-detail.component.css'],
|
|
})
|
|
export class CustomerDetailComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
|
|
form: FormGroup<{
|
|
name: FormControl<string>;
|
|
phone: FormControl<string>;
|
|
address: FormControl<string | null>;
|
|
printInBill: FormControl<boolean>;
|
|
discounts: FormArray<
|
|
FormGroup<{
|
|
discount: FormControl<number | null>;
|
|
}>
|
|
>;
|
|
}>;
|
|
|
|
item: Customer = new Customer();
|
|
hide: boolean;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private toaster: ToasterService,
|
|
private dialog: MatDialog,
|
|
private ser: CustomerService,
|
|
) {
|
|
this.hide = true;
|
|
// Create form
|
|
this.form = new FormGroup({
|
|
name: new FormControl<string>('', { nonNullable: true }),
|
|
phone: new FormControl<string>('', { nonNullable: true }),
|
|
address: new FormControl<string | null>(null),
|
|
printInBill: new FormControl<boolean>(false, { nonNullable: true }),
|
|
discounts: new FormArray<FormGroup<{ discount: FormControl<number | null> }>>([]),
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { item: Customer };
|
|
this.showItem(data.item);
|
|
});
|
|
}
|
|
|
|
showItem(item: Customer) {
|
|
this.item = item;
|
|
this.form.controls.name.setValue(item.name);
|
|
this.form.controls.phone.setValue(item.phone);
|
|
this.form.controls.address.setValue(item.address);
|
|
this.form.controls.printInBill.setValue(item.printInBill);
|
|
this.form.controls.discounts.clear();
|
|
this.item.discounts.forEach((x) =>
|
|
this.form.controls.discounts.push(
|
|
new FormGroup({
|
|
discount: new FormControl<number | null>(x.discount ? x.discount * 100 : null),
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.nameElement !== undefined) {
|
|
this.nameElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem()).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/customers');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Error', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.item.id as string).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/customers');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Error', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
confirmDelete(): void {
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: { title: 'Delete Customer?', content: 'Are you sure? This cannot be undone.' },
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
getItem(): Customer {
|
|
const formModel = this.form.value;
|
|
this.item.name = formModel.name ?? '';
|
|
this.item.phone = formModel.phone ?? '';
|
|
this.item.address = formModel.address ?? null;
|
|
this.item.printInBill = formModel.printInBill ?? false;
|
|
|
|
const array = formModel.discounts;
|
|
if (array) {
|
|
this.item.discounts.forEach((item, index) => {
|
|
const array_item = array.at(index);
|
|
if (array_item && array_item?.discount) {
|
|
item.discount = Math.max(Math.min(round(array_item.discount / 100, 5), 100), 0);
|
|
} else {
|
|
item.discount = 0;
|
|
}
|
|
});
|
|
}
|
|
return this.item;
|
|
}
|
|
}
|