ml-product-card
Version:
### Ejemplo de paquete para subir a npm
31 lines (24 loc) • 800 B
text/typescript
import { useState } from "react"
import { onChangeArgs, ProductInCart } from "../interfaces/ProductCard"
export const useShoppingCart = () => {
const [shopingCart, setShopingCart] = useState<{[key: string] : ProductInCart}>({})
const handleShoppingCart = ({producto, count}: onChangeArgs) => {
setShopingCart(prev => {
let newCart
if(count === 0){
const { [producto.id]: toDelete, ...rest } = prev
newCart = rest
}else{
newCart = {
...prev,
[producto.id]: { ...producto, count }
}
}
return newCart
})
}
return {
shopingCart,
handleShoppingCart
}
}