Files
barker/bookie/src/app/product/product-detail/product-detail.component.ts
Amritanshu 010e9a84db Chore: Upgrade to Angular v18
Chore: Upgrade to Python 3.12
Chore: Upgrade to psycopg3
2024-06-03 13:22:56 +05:30

169 lines
5.4 KiB
TypeScript

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { MatButton } from '@angular/material/button';
import { MatCard, MatCardHeader, MatCardTitle, MatCardContent, MatCardActions } from '@angular/material/card';
import { MatCheckbox } from '@angular/material/checkbox';
import { MatOption } from '@angular/material/core';
import { MatDialog } from '@angular/material/dialog';
import { MatFormField, MatLabel } from '@angular/material/form-field';
import { MatInput } from '@angular/material/input';
import { MatSelect } from '@angular/material/select';
import { ActivatedRoute, Router } from '@angular/router';
import { MenuCategory } from '../../core/menu-category';
import { Product } from '../../core/product';
import { SaleCategory } from '../../core/sale-category';
import { ToasterService } from '../../core/toaster.service';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
import { ProductService } from '../product.service';
@Component({
selector: 'app-product-detail',
templateUrl: './product-detail.component.html',
styleUrls: ['./product-detail.component.css'],
standalone: true,
imports: [
MatCard,
MatCardHeader,
MatCardTitle,
MatCardContent,
ReactiveFormsModule,
MatFormField,
MatLabel,
MatInput,
MatCheckbox,
MatSelect,
MatOption,
MatCardActions,
MatButton,
],
})
export class ProductDetailComponent implements OnInit, AfterViewInit {
@ViewChild('name', { static: true }) nameElement?: ElementRef;
form: FormGroup<{
name: FormControl<string>;
units: FormControl<string>;
menuCategory: FormControl<string>;
saleCategory: FormControl<string>;
price: FormControl<number>;
hasHappyHour: FormControl<boolean>;
isNotAvailable: FormControl<boolean>;
quantity: FormControl<number>;
}>;
menuCategories: MenuCategory[] = [];
saleCategories: SaleCategory[] = [];
item: Product = new Product();
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private toaster: ToasterService,
private ser: ProductService,
) {
// Create form
this.form = new FormGroup({
name: new FormControl<string>('', { nonNullable: true }),
units: new FormControl<string>('', { nonNullable: true }),
menuCategory: new FormControl<string>('', { nonNullable: true }),
saleCategory: new FormControl<string>('', { nonNullable: true }),
price: new FormControl<number>(0, { nonNullable: true }),
hasHappyHour: new FormControl<boolean>(false, { nonNullable: true }),
isNotAvailable: new FormControl<boolean>(false, { nonNullable: true }),
quantity: new FormControl<number>(0, { nonNullable: true }),
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as {
item: Product;
menuCategories: MenuCategory[];
saleCategories: SaleCategory[];
};
this.menuCategories = data.menuCategories;
this.saleCategories = data.saleCategories;
this.showItem(data.item);
});
}
showItem(item: Product) {
this.item = item;
this.form.setValue({
name: this.item.name ?? '',
units: this.item.units ?? '',
menuCategory: this.item.menuCategory?.id ?? '',
saleCategory: this.item.saleCategory?.id ?? '',
price: this.item.price ?? 0,
hasHappyHour: this.item.hasHappyHour,
isNotAvailable: this.item.isNotAvailable,
quantity: this.item.quantity ?? 0,
});
}
ngAfterViewInit() {
setTimeout(() => {
if (this.nameElement !== undefined) {
this.nameElement.nativeElement.focus();
}
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem()).subscribe({
next: () => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/products');
},
error: (error) => {
this.toaster.show('Error', error);
},
});
}
delete() {
this.ser.delete(this.item.id as string).subscribe({
next: () => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/products');
},
error: (error) => {
this.toaster.show('Error', error);
},
});
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Delete Product?', content: 'Are you sure? This cannot be undone.' },
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): Product {
const formModel = this.form.value;
this.item.name = formModel.name ?? '';
this.item.units = formModel.units ?? '';
if (this.item.menuCategory === null || this.item.menuCategory === undefined) {
this.item.menuCategory = new MenuCategory();
}
this.item.menuCategory.id = formModel.menuCategory;
if (this.item.saleCategory === null || this.item.saleCategory === undefined) {
this.item.saleCategory = new SaleCategory();
}
this.item.saleCategory.id = formModel.saleCategory;
this.item.price = formModel.price ?? 0;
this.item.hasHappyHour = formModel.hasHappyHour ?? false;
this.item.isNotAvailable = formModel.isNotAvailable ?? false;
this.item.quantity = formModel.quantity ?? 0;
return this.item;
}
}