diff --git a/src/app/core/services/product.service.ts b/src/app/core/services/product.service.ts index b9d233a..6e49481 100644 --- a/src/app/core/services/product.service.ts +++ b/src/app/core/services/product.service.ts @@ -41,6 +41,20 @@ export class ProductService { return this.http.delete(`${this.apiUrl}/${id}`, { headers }); } + deleteProductsMult(ids: number[]): Observable { + const token = localStorage.getItem('token'); + const headers = new HttpHeaders({ + Authorization: `Bearer ${token}`, + }); + + // Envia os IDs no corpo da requisição usando a opção `body` + return this.http.delete(`${this.apiUrl}`, { + headers, + body: { ids }, // Passa `ids` dentro de um objeto + }); +} + + // Método para enviar os dados do produto para a API createProducts(data: any): Observable { console.log("🚀 ~ ProductService ~ sendDataToApi ~ data:", data) diff --git a/src/app/modules/products/pages/add-product/add-product.component.ts b/src/app/modules/products/pages/add-product/add-product.component.ts index de47d9c..aa9bc3c 100644 --- a/src/app/modules/products/pages/add-product/add-product.component.ts +++ b/src/app/modules/products/pages/add-product/add-product.component.ts @@ -362,19 +362,43 @@ export class AddProductComponent implements OnInit { // this.base64Image = null; // this.imageUrl = null; // } - deleteSelectedProducts() { - this.confirmationService.confirm({ - message: 'Are you sure you want to delete the selected products?', - header: 'Confirm', - icon: 'pi pi-exclamation-triangle', - accept: () => { - this.products = this.products.filter((val) => !this.selectedProducts?.includes(val)); - this.selectedProducts = null; - this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Products Deleted', life: 3000 }); - } - }); - } + this.confirmationService.confirm({ + message: 'Are you sure you want to delete the selected products?', + header: 'Confirm', + icon: 'pi pi-exclamation-triangle', + accept: () => { + const ids :any = this.selectedProducts?.map(product => product.id); + console.log("🚀 ~ AddProductComponent ~ deleteSelectedProducts ~ ids:", ids) + + this.productService.deleteProductsMult(ids).subscribe({ + next: () => { + // Remove os produtos da lista local + this.products = this.products.filter((val) => !this.selectedProducts?.includes(val)); + this.selectedProducts = null; + this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Products Deleted', life: 3000 }); + }, + error: (error) => { + this.messageService.add({ severity: 'error', summary: 'Error', detail: error.message, life: 3000 }); + } + }); + } + }); +} + + + // deleteSelectedProducts() { + // this.confirmationService.confirm({ + // message: 'Are you sure you want to delete the selected products?', + // header: 'Confirm', + // icon: 'pi pi-exclamation-triangle', + // accept: () => { + // this.products = this.products.filter((val) => !this.selectedProducts?.includes(val)); + // this.selectedProducts = null; + // this.messageService.add({ severity: 'success', summary: 'Successful', detail: 'Products Deleted', life: 3000 }); + // } + // }); + // } // editProduct(product: Product) { // this.product = { ...product };