Skip to content

Commit

Permalink
feat: add delete by selected mult
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Brasileiro authored and Fabio Brasileiro committed Oct 28, 2024
1 parent 9ddae3f commit 3196a79
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/app/core/services/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ export class ProductService {
return this.http.delete<any>(`${this.apiUrl}/${id}`, { headers });
}

deleteProductsMult(ids: number[]): Observable<any> {
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<any>(`${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<any> {
console.log("🚀 ~ ProductService ~ sendDataToApi ~ data:", data)
Expand Down
48 changes: 36 additions & 12 deletions src/app/modules/products/pages/add-product/add-product.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit 3196a79

Please sign in to comment.