diff --git a/webapp/src/App.tsx b/webapp/src/App.tsx index 29e1ceb..5e0ad07 100644 --- a/webapp/src/App.tsx +++ b/webapp/src/App.tsx @@ -33,10 +33,15 @@ const App: FC = () => { if(user){ const exist = cartItems.find(x=> x.codigo === prod.codigo); if(exist){ - setCartItems(cartItems.map(x=> x.codigo === prod.codigo ? {...exist, cantidad : exist.cantidad +1} : x)) + exist.cantidad+=1 + setCartItems(cartItems.map(x=> x.codigo === prod.codigo ?{ ...x, exist } : x)) } else { - setCartItems([...cartItems,{...prod,cantidad:1}]) + prod.cantidad = 1; + cartItems.push(prod) + setCartItems([...cartItems]) } + console.log("CARRO ---> " + JSON.stringify(cartItems)) + localStorage.setItem('carrito',JSON.stringify(cartItems)) }else{ Swal.fire({ title: "Debes iniciar sesiĆ³n", diff --git a/webapp/src/components/Products.tsx b/webapp/src/components/Products.tsx index c3bdaf2..5a84a55 100644 --- a/webapp/src/components/Products.tsx +++ b/webapp/src/components/Products.tsx @@ -9,7 +9,7 @@ import {v4 as uuidv4} from 'uuid'; type ProductsProps = { product: Product[] onAddCart:(prod:Product) => (void); - cartItems:Product[] + cartItems:Object[] homePage: boolean } diff --git a/webapp/src/components/navbar/NavBar.tsx b/webapp/src/components/navbar/NavBar.tsx index 4c17e15..cb19d5b 100644 --- a/webapp/src/components/navbar/NavBar.tsx +++ b/webapp/src/components/navbar/NavBar.tsx @@ -32,7 +32,21 @@ const NavBar=(cart:ProductsProps) =>{ var numOfProducts = 0 cart.cartItems.map(x => numOfProducts+= x.cantidad); cart.cartItems.map(x => totalPrice+= x.cantidad * x.precio); - console.log( cart.cartItems) + const carrt = localStorage.getItem("carrito"); + if(carrt !== null){ + for(let i =0; i < JSON.parse(carrt).length; i++){ + cart.cartItems[i] = { + nombre: JSON.parse(carrt)[i]['nombre'], + codigo: JSON.parse(carrt)[i]['codigo'], + descripcion: JSON.parse(carrt)[i]['descripcion'], + precio: JSON.parse(carrt)[i]['precio'], + cantidad: JSON.parse(carrt)[i]['cantidad'], + url: JSON.parse(carrt)[i]['url'], + stock: JSON.parse(carrt)[i]['stock'], + categoria: JSON.parse(carrt)[i]['categoria'], + } + } + } function mover(){ localStorage.setItem("carrito", JSON.stringify(cart.cartItems)); @@ -44,8 +58,10 @@ const NavBar=(cart:ProductsProps) =>{ if(index > -1){ if(cart.cartItems[index].cantidad > 1){ cart.cartItems[index].cantidad = cart.cartItems[index].cantidad -1 ; + localStorage.setItem("carrito",JSON.stringify(cart.cartItems)); } else{ cart.cartItems.splice(index); + localStorage.setItem("carrito",JSON.stringify(cart.cartItems)); } } } diff --git a/webapp/src/components/navbar/NavBarAdmin.tsx b/webapp/src/components/navbar/NavBarAdmin.tsx index 1e45bf0..9bcf536 100644 --- a/webapp/src/components/navbar/NavBarAdmin.tsx +++ b/webapp/src/components/navbar/NavBarAdmin.tsx @@ -30,7 +30,7 @@ const NavBarAdmin = (cart:ProductsProps) => { cart.cartItems.map(x => numOfProducts+= x.cantidad); cart.cartItems.map(x => totalPrice+= x.cantidad * x.precio); - const carrt = localStorage.getItem("cart"); + const carrt = localStorage.getItem("carrito"); if(carrt !== null){ for(let i =0; i < JSON.parse(carrt).length; i++){ cart.cartItems[i] = { @@ -51,8 +51,10 @@ const NavBarAdmin = (cart:ProductsProps) => { if(index > -1){ if(cart.cartItems[index].cantidad > 1){ cart.cartItems[index].cantidad = cart.cartItems[index].cantidad -1 ; + localStorage.setItem("carrito",JSON.stringify(cart.cartItems)); } else{ - cart.cartItems.splice(index); + cart.cartItems.splice(index,1); + localStorage.setItem("carrito",JSON.stringify(cart.cartItems)); } } } diff --git a/webapp/src/pages/Home.tsx b/webapp/src/pages/Home.tsx index 590fe49..42d3e7f 100644 --- a/webapp/src/pages/Home.tsx +++ b/webapp/src/pages/Home.tsx @@ -9,7 +9,7 @@ import Typography from '@mui/material/Typography'; type ProductsProps = { onAddCart:(prod:Product) => (void); - cartItems:Product[] + cartItems:Object[] } function cargarBanner(){