Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated toggleCartItemQuantity function #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 65 additions & 36 deletions context/StateContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { createContext, useContext, useState, useEffect } from 'react';
import { toast } from 'react-hot-toast';
import React, { createContext, useContext, useState, useEffect } from "react";
import { toast } from "react-hot-toast";

const Context = createContext();

Expand All @@ -14,67 +14,96 @@ export const StateContext = ({ children }) => {
let index;

const onAdd = (product, quantity) => {
const checkProductInCart = cartItems.find((item) => item._id === product._id);

setTotalPrice((prevTotalPrice) => prevTotalPrice + product.price * quantity);
const checkProductInCart = cartItems.find(
(item) => item._id === product._id
);

setTotalPrice(
(prevTotalPrice) => prevTotalPrice + product.price * quantity
);
setTotalQuantities((prevTotalQuantities) => prevTotalQuantities + quantity);
if(checkProductInCart) {

if (checkProductInCart) {
const updatedCartItems = cartItems.map((cartProduct) => {
if(cartProduct._id === product._id) return {
...cartProduct,
quantity: cartProduct.quantity + quantity
}
})
if (cartProduct._id === product._id)
return {
...cartProduct,
quantity: cartProduct.quantity + quantity,
};
});

setCartItems(updatedCartItems);
} else {
product.quantity = quantity;

setCartItems([...cartItems, { ...product }]);
}

toast.success(`${qty} ${product.name} added to the cart.`);
}
};

const onRemove = (product) => {
foundProduct = cartItems.find((item) => item._id === product._id);
const newCartItems = cartItems.filter((item) => item._id !== product._id);

setTotalPrice((prevTotalPrice) => prevTotalPrice -foundProduct.price * foundProduct.quantity);
setTotalQuantities(prevTotalQuantities => prevTotalQuantities - foundProduct.quantity);
setTotalPrice(
(prevTotalPrice) =>
prevTotalPrice - foundProduct.price * foundProduct.quantity
);
setTotalQuantities(
(prevTotalQuantities) => prevTotalQuantities - foundProduct.quantity
);
setCartItems(newCartItems);
}
};

const toggleCartItemQuanitity = (id, value) => {
foundProduct = cartItems.find((item) => item._id === id)
foundProduct = cartItems.find((item) => item._id === id);
index = cartItems.findIndex((product) => product._id === id);
const newCartItems = cartItems.filter((item) => item._id !== id)

if(value === 'inc') {
setCartItems([...newCartItems, { ...foundProduct, quantity: foundProduct.quantity + 1 } ]);
setTotalPrice((prevTotalPrice) => prevTotalPrice + foundProduct.price)
setTotalQuantities(prevTotalQuantities => prevTotalQuantities + 1)
} else if(value === 'dec') {
if (value === "inc") {
setCartItems((prev) =>
prev.map((item) => {
if (item._id === id) {
return {
...item,
quantity: item.quantity + 1,
};
}
return item;
})
);
setTotalPrice((prevTotalPrice) => prevTotalPrice + foundProduct.price);
setTotalQuantities((prevTotalQuantities) => prevTotalQuantities + 1);
} else if (value === "dec") {
if (foundProduct.quantity > 1) {
setCartItems([...newCartItems, { ...foundProduct, quantity: foundProduct.quantity - 1 } ]);
setTotalPrice((prevTotalPrice) => prevTotalPrice - foundProduct.price)
setTotalQuantities(prevTotalQuantities => prevTotalQuantities - 1)
setCartItems((prev) =>
prev.map((item) => {
if (item._id === id) {
return {
...item,
quantity: item.quantity - 1,
};
}
return item;
})
);
setTotalPrice((prevTotalPrice) => prevTotalPrice - foundProduct.price);
setTotalQuantities((prevTotalQuantities) => prevTotalQuantities - 1);
}
}
}
};

const incQty = () => {
setQty((prevQty) => prevQty + 1);
}
};

const decQty = () => {
setQty((prevQty) => {
if(prevQty - 1 < 1) return 1;
if (prevQty - 1 < 1) return 1;

return prevQty - 1;
});
}
};

return (
<Context.Provider
Expand All @@ -92,12 +121,12 @@ export const StateContext = ({ children }) => {
onRemove,
setCartItems,
setTotalPrice,
setTotalQuantities
setTotalQuantities,
}}
>
{children}
</Context.Provider>
)
}
);
};

export const useStateContext = () => useContext(Context);
export const useStateContext = () => useContext(Context);