2021-12-29 09:28:51 +03:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
2022-07-22 13:13:50 +03:00
|
|
|
import { Product } from 'src/app/demo/api/product';
|
|
|
|
|
import { MessageService } from 'primeng/api';
|
|
|
|
|
import { Table } from 'primeng/table';
|
|
|
|
|
import { ProductService } from 'src/app/demo/service/product.service';
|
2021-12-09 17:24:42 +03:00
|
|
|
|
|
|
|
|
@Component({
|
2021-12-28 13:29:25 +03:00
|
|
|
templateUrl: './crud.component.html',
|
2022-07-22 13:13:50 +03:00
|
|
|
providers: [MessageService]
|
2021-12-09 17:24:42 +03:00
|
|
|
})
|
2021-12-28 13:29:25 +03:00
|
|
|
export class CrudComponent implements OnInit {
|
2021-12-09 17:24:42 +03:00
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
productDialog: boolean = false;
|
2021-12-09 17:24:42 +03:00
|
|
|
|
2021-12-23 09:08:09 +03:00
|
|
|
deleteProductDialog: boolean = false;
|
|
|
|
|
|
|
|
|
|
deleteProductsDialog: boolean = false;
|
|
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
products: Product[] = [];
|
2021-12-09 17:24:42 +03:00
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
product: Product = {};
|
2021-12-09 17:24:42 +03:00
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
selectedProducts: Product[] = [];
|
2021-12-09 17:24:42 +03:00
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
submitted: boolean = false;
|
2021-12-09 17:24:42 +03:00
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
cols: any[] = [];
|
2021-12-09 17:24:42 +03:00
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
statuses: any[] = [];
|
2021-12-14 16:31:40 +03:00
|
|
|
|
2021-12-09 17:24:42 +03:00
|
|
|
rowsPerPageOptions = [5, 10, 20];
|
|
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
constructor(private productService: ProductService, private messageService: MessageService) { }
|
2021-12-09 17:24:42 +03:00
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
this.productService.getProducts().then(data => this.products = data);
|
|
|
|
|
|
|
|
|
|
this.cols = [
|
2022-07-22 13:13:50 +03:00
|
|
|
{ field: 'product', header: 'Product' },
|
|
|
|
|
{ field: 'price', header: 'Price' },
|
|
|
|
|
{ field: 'category', header: 'Category' },
|
|
|
|
|
{ field: 'rating', header: 'Reviews' },
|
|
|
|
|
{ field: 'inventoryStatus', header: 'Status' }
|
2021-12-09 17:24:42 +03:00
|
|
|
];
|
2021-12-14 16:31:40 +03:00
|
|
|
|
|
|
|
|
this.statuses = [
|
2022-07-22 13:13:50 +03:00
|
|
|
{ label: 'INSTOCK', value: 'instock' },
|
|
|
|
|
{ label: 'LOWSTOCK', value: 'lowstock' },
|
|
|
|
|
{ label: 'OUTOFSTOCK', value: 'outofstock' }
|
2021-12-14 16:31:40 +03:00
|
|
|
];
|
2021-12-09 17:24:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openNew() {
|
|
|
|
|
this.product = {};
|
|
|
|
|
this.submitted = false;
|
|
|
|
|
this.productDialog = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteSelectedProducts() {
|
2021-12-23 09:08:09 +03:00
|
|
|
this.deleteProductsDialog = true;
|
2021-12-09 17:24:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
editProduct(product: Product) {
|
2022-07-22 13:13:50 +03:00
|
|
|
this.product = { ...product };
|
2021-12-09 17:24:42 +03:00
|
|
|
this.productDialog = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteProduct(product: Product) {
|
2021-12-23 09:08:09 +03:00
|
|
|
this.deleteProductDialog = true;
|
2022-07-22 13:13:50 +03:00
|
|
|
this.product = { ...product };
|
2021-12-23 09:08:09 +03:00
|
|
|
}
|
|
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
confirmDeleteSelected() {
|
2021-12-23 09:08:09 +03:00
|
|
|
this.deleteProductsDialog = false;
|
|
|
|
|
this.products = this.products.filter(val => !this.selectedProducts.includes(val));
|
2022-07-22 13:13:50 +03:00
|
|
|
this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Products Deleted', life: 3000 });
|
|
|
|
|
this.selectedProducts = [];
|
2021-12-23 09:08:09 +03:00
|
|
|
}
|
|
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
confirmDelete() {
|
2021-12-23 09:08:09 +03:00
|
|
|
this.deleteProductDialog = false;
|
|
|
|
|
this.products = this.products.filter(val => val.id !== this.product.id);
|
2022-07-22 13:13:50 +03:00
|
|
|
this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Product Deleted', life: 3000 });
|
2021-12-23 09:08:09 +03:00
|
|
|
this.product = {};
|
2021-12-09 17:24:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hideDialog() {
|
|
|
|
|
this.productDialog = false;
|
|
|
|
|
this.submitted = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveProduct() {
|
|
|
|
|
this.submitted = true;
|
|
|
|
|
|
2022-07-22 13:13:50 +03:00
|
|
|
if (this.product.name?.trim()) {
|
2021-12-09 17:24:42 +03:00
|
|
|
if (this.product.id) {
|
2021-12-23 09:08:09 +03:00
|
|
|
// @ts-ignore
|
2022-07-22 13:13:50 +03:00
|
|
|
this.product.inventoryStatus = this.product.inventoryStatus.value ? this.product.inventoryStatus.value : this.product.inventoryStatus;
|
2021-12-09 17:24:42 +03:00
|
|
|
this.products[this.findIndexById(this.product.id)] = this.product;
|
2022-07-22 13:13:50 +03:00
|
|
|
this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Product Updated', life: 3000 });
|
2021-12-09 17:24:42 +03:00
|
|
|
} else {
|
|
|
|
|
this.product.id = this.createId();
|
2021-12-23 09:08:09 +03:00
|
|
|
this.product.code = this.createId();
|
2021-12-09 17:24:42 +03:00
|
|
|
this.product.image = 'product-placeholder.svg';
|
2021-12-23 09:08:09 +03:00
|
|
|
// @ts-ignore
|
|
|
|
|
this.product.inventoryStatus = this.product.inventoryStatus ? this.product.inventoryStatus.value : 'INSTOCK';
|
2021-12-09 17:24:42 +03:00
|
|
|
this.products.push(this.product);
|
2022-07-22 13:13:50 +03:00
|
|
|
this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Product Created', life: 3000 });
|
2021-12-09 17:24:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.products = [...this.products];
|
|
|
|
|
this.productDialog = false;
|
|
|
|
|
this.product = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findIndexById(id: string): number {
|
|
|
|
|
let index = -1;
|
|
|
|
|
for (let i = 0; i < this.products.length; i++) {
|
|
|
|
|
if (this.products[i].id === id) {
|
|
|
|
|
index = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createId(): string {
|
|
|
|
|
let id = '';
|
|
|
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
|
id += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
}
|
2022-07-22 13:13:50 +03:00
|
|
|
|
|
|
|
|
onGlobalFilter(table: Table, event: Event) {
|
|
|
|
|
table.filterGlobal((event.target as HTMLInputElement).value, 'contains');
|
|
|
|
|
}
|
2021-12-09 17:24:42 +03:00
|
|
|
}
|