Acme.BookStore/angular/src/app/customer/customer.component.ts
2024-06-10 22:15:20 +05:30

93 lines
2.6 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { ListService, PagedResultDto } from '@abp/ng.core';
import { CustomerDto, CustomerService } from '@proxy/customers';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ConfirmationService, Confirmation } from '@abp/ng.theme.shared';
@Component({
selector: 'app-customer',
templateUrl: './customer.component.html',
styleUrl: './customer.component.scss',
providers: [ListService]
})
export class CustomerComponent implements OnInit {
customer = { items: [], totalCount: 0 } as PagedResultDto<CustomerDto>;
isModalOpen = false;
form: FormGroup;
selectedcustomer = {} as CustomerDto;
constructor(public readonly list: ListService,
private customerService: CustomerService,
private fb: FormBuilder,
private confirmation: ConfirmationService ) {}
ngOnInit() {
debugger;
const bookStreamCreator = (query) => this.customerService.getList(query);
this.list.hookToQuery(bookStreamCreator).subscribe((response) => {
this.customer = response;
});
}
// add new method
createCustomer() {
this.selectedcustomer = {} as CustomerDto;
this.buildForm();
this.isModalOpen = true;
}
editCustomer(id: number) {
this.customerService.get(id).subscribe((customer) => {
debugger;
this.selectedcustomer = customer;
this.buildForm();
this.isModalOpen = true;
});
}
buildForm() {
this.form = this.fb.group({
firstName: [this.selectedcustomer.firstName || '', Validators.required],
lastName: [this.selectedcustomer.lastName, Validators.required],
phone: [this.selectedcustomer.phone, Validators.required],
address: [this.selectedcustomer.address, Validators.required],
});
}
// add save method
save() {
if (this.form.invalid) {
return;
}
debugger;
const request = this.selectedcustomer.id
? this.customerService.update(this.selectedcustomer.id, this.form.value)
: this.customerService.create(this.form.value);
if(this.selectedcustomer.id){
this.customerService.update(this.selectedcustomer.id,this.form.value).subscribe(() => {
this.isModalOpen = false;
this.form.reset();
this.list.get();
});
}
else{
this.customerService.create(this.form.value).subscribe(() => {
this.isModalOpen = false;
this.form.reset();
this.list.get();
});
}
}
delete(id: number) {
this.confirmation.warn('::AreYouSureToDelete', '::AreYouSure').subscribe((status) => {
if (status === Confirmation.Status.confirm) {
this.customerService.delete(id).subscribe(() => this.list.get());
}
});
}
}