diff --git a/src/models/Product.js b/src/models/Product.js index f29cbe4..c874cd3 100644 --- a/src/models/Product.js +++ b/src/models/Product.js @@ -5,7 +5,7 @@ import ProductPrice from "./ProductPrice"; import { ProviderProduct } from "./ProviderProduct"; export default class Product { - constructor({ id, name, brand = {}, comments, stockCount, images, files = [], providers = [], prices = [], carModels = [], dpi}) { + constructor({ id, name, brand = {}, comments, stockCount, images, files = [], providers = [], prices = [], carModels = [], dpi = ''}) { this.id = id; this.name = name; this.brand = new Brand(brand); diff --git a/src/pages/Products/Forms/RadiatorForm.jsx b/src/pages/Products/Forms/RadiatorForm.jsx index ce110c3..fe091f1 100644 --- a/src/pages/Products/Forms/RadiatorForm.jsx +++ b/src/pages/Products/Forms/RadiatorForm.jsx @@ -53,7 +53,7 @@ const RadiatorFormContainer = ({ setIsFormValid }) => { const { product, handleSetProduct } = useProductDialogContext(); useEffect(() => { - setIsFormValid(product.dpi && product.stockCount); + setIsFormValid(product.stockCount); }, [product.dpi, product.stockCount, setIsFormValid]); const handleChange = (event, field) => { diff --git a/src/pages/Products/ProductViewer/ProductContainer.jsx b/src/pages/Products/ProductViewer/ProductContainer.jsx index c4a4f69..d144bbb 100644 --- a/src/pages/Products/ProductViewer/ProductContainer.jsx +++ b/src/pages/Products/ProductViewer/ProductContainer.jsx @@ -8,6 +8,7 @@ import { useProductsContext } from '../ProductsContext'; import { Screens } from '../ProductsConstants'; import ProductList from './ProductList'; import { ProductCarModel } from '../../../models/ProductCarModel'; +import { deleteProduct } from '../../../services/ProductService'; const ProductContainer = () => { const [productCarModels, setProductCarModels] = useState([]); @@ -19,6 +20,22 @@ const ProductContainer = () => { handleItemSelect(productCarModel.product, Screens.PRODUCTS); } + const handleOnDelete = async (productCarModel) => { + try { + console.log(productCarModel) + let result = await deleteProduct(productCarModel.id); + if (result) { + const products = productCarModels.filter(pcm => pcm.product.id !== productCarModel.id); + setProductCarModels(products); + openSnackbar('Producto eliminado correctamente', 'success'); + } else { + openSnackbar('Error al eliminar el producto', 'error'); + } + } catch (error) { + openSnackbar(`Error al eliminar el producto: ${error.errorMessage}`, 'error'); + } + } + useEffect(() => { setLoading(true); @@ -73,7 +90,7 @@ const ProductContainer = () => { unmountOnExit >
- +
); diff --git a/src/pages/Products/ProductViewer/ProductList.jsx b/src/pages/Products/ProductViewer/ProductList.jsx index 56eb711..d81d7b1 100644 --- a/src/pages/Products/ProductViewer/ProductList.jsx +++ b/src/pages/Products/ProductViewer/ProductList.jsx @@ -5,7 +5,7 @@ const columns = [ { field: 'name', headerName: 'Nombre', showLabel: false, valueStyle: { fontSize: '16px' } } ]; -const ProductList = ({ products, onProductSelect }) => { +const ProductList = ({ products, onProductSelect, handleOnDelete }) => { return (
({ @@ -16,10 +16,7 @@ const ProductList = ({ products, onProductSelect }) => { }))} columns={columns} itemCardProps={{onClick: onProductSelect, menuOptions: [ { label: 'Eliminar', - onClick: (item) => { - // Lógica para eliminar el item - console.log('Eliminar', item); - } + onClick: handleOnDelete } ]}} />
diff --git a/src/services/ProductService.js b/src/services/ProductService.js index b635b16..fbf8b2c 100644 --- a/src/services/ProductService.js +++ b/src/services/ProductService.js @@ -36,4 +36,10 @@ const getProductById = async (productId) => { return new Product(result.response); } -export { createProductVehicles, createProductPrices, getProductPrices, getProductVehicleModels, createProductFiles, createProduct, getProductById } \ No newline at end of file +const deleteProduct = async (productId) => { + const response = await axiosInstance.delete(`/product/${productId}`); + return response.statusCode === 204; +} + +export { createProductVehicles, createProductPrices, getProductPrices, + getProductVehicleModels, createProductFiles, createProduct, getProductById, deleteProduct } \ No newline at end of file