Skip to content

Commit

Permalink
adding support to delete products
Browse files Browse the repository at this point in the history
  • Loading branch information
edmdz committed Jun 21, 2024
1 parent d97a37f commit 78e1568
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/models/Product.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Products/Forms/RadiatorForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
19 changes: 18 additions & 1 deletion src/pages/Products/ProductViewer/ProductContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand All @@ -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);

Expand Down Expand Up @@ -73,7 +90,7 @@ const ProductContainer = () => {
unmountOnExit
>
<div>
<ProductList products={productCarModels} onProductSelect={handleProductSelect} />
<ProductList products={productCarModels} onProductSelect={handleProductSelect} handleOnDelete={handleOnDelete}/>
</div>
</CSSTransition>
);
Expand Down
7 changes: 2 additions & 5 deletions src/pages/Products/ProductViewer/ProductList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
<ItemsCardList rows={products.map(productCarModel => ({
Expand All @@ -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
}
]}} />
</div>
Expand Down
8 changes: 7 additions & 1 deletion src/services/ProductService.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ const getProductById = async (productId) => {
return new Product(result.response);
}

export { createProductVehicles, createProductPrices, getProductPrices, getProductVehicleModels, createProductFiles, createProduct, getProductById }
const deleteProduct = async (productId) => {
const response = await axiosInstance.delete(`/product/${productId}`);
return response.statusCode === 204;
}

export { createProductVehicles, createProductPrices, getProductPrices,
getProductVehicleModels, createProductFiles, createProduct, getProductById, deleteProduct }

0 comments on commit 78e1568

Please sign in to comment.