From 01c4ba4ccf34c65764e060a4f0071c45560f2a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Mart=C3=ADnez=20Bajo?= Date: Sat, 7 May 2022 03:17:21 +0200 Subject: [PATCH 1/2] Carrito de admin arreglado --- webapp/src/App.tsx | 9 +++++++-- webapp/src/api/ApiUsers.ts | 2 +- webapp/src/components/Products.tsx | 2 +- webapp/src/components/navbar/NavBarAdmin.tsx | 6 ++++-- webapp/src/pages/Home.tsx | 2 +- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/webapp/src/App.tsx b/webapp/src/App.tsx index e2b8608..f687414 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/api/ApiUsers.ts b/webapp/src/api/ApiUsers.ts index b8cf59c..e8f5d7a 100644 --- a/webapp/src/api/ApiUsers.ts +++ b/webapp/src/api/ApiUsers.ts @@ -81,7 +81,7 @@ export const foundEmail = async (email: String):Promise => { } export async function getUserById(id: string) { - const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/' + const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000' let response = await fetch(apiEndPoint+'/user/findById/' + id); //The objects returned by the api are directly convertible to User objects return response.json() 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/NavBarAdmin.tsx b/webapp/src/components/navbar/NavBarAdmin.tsx index 82ce9d2..600b236 100644 --- a/webapp/src/components/navbar/NavBarAdmin.tsx +++ b/webapp/src/components/navbar/NavBarAdmin.tsx @@ -41,7 +41,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] = { @@ -62,8 +62,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(){ From 04cf941bbbddfc0ec5087670f78a6ee174af8acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Mart=C3=ADnez=20Bajo?= Date: Sat, 7 May 2022 03:20:21 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Carrito=20de=20usuario=20est=C3=A1ndar=20ar?= =?UTF-8?q?reglado?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webapp/src/components/navbar/NavBar.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/webapp/src/components/navbar/NavBar.tsx b/webapp/src/components/navbar/NavBar.tsx index c771be4..1c64f71 100644 --- a/webapp/src/components/navbar/NavBar.tsx +++ b/webapp/src/components/navbar/NavBar.tsx @@ -42,7 +42,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)); @@ -54,8 +68,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)); } } }