Skip to content

Commit

Permalink
Merge pull request #253 from Arquisoft/Dario-Nueva
Browse files Browse the repository at this point in the history
Arreglo de carrito para que se mantenga entre ventanas
  • Loading branch information
AnaOstio authored May 10, 2022
2 parents 70a16a7 + f703179 commit 50cceff
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
9 changes: 7 additions & 2 deletions webapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/components/Products.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {v4 as uuidv4} from 'uuid';
type ProductsProps = {
product: Product[]
onAddCart:(prod:Product) => (void);
cartItems:Product[]
cartItems:Object[]
homePage: boolean
}

Expand Down
18 changes: 17 additions & 1 deletion webapp/src/components/navbar/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions webapp/src/components/navbar/NavBarAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand All @@ -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));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Typography from '@mui/material/Typography';

type ProductsProps = {
onAddCart:(prod:Product) => (void);
cartItems:Product[]
cartItems:Object[]
}

function cargarBanner(){
Expand Down

0 comments on commit 50cceff

Please sign in to comment.