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

added fixation to the bucket list #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
90 changes: 56 additions & 34 deletions context/StateContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,89 @@ 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)
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(
cartItems.map((item, i) =>
i === index
? { ...foundProduct, quantity: foundProduct.quantity + 1 }
: 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(
cartItems.map((item, i) =>
i === index
? { ...foundProduct, quantity: foundProduct.quantity - 1 }
: 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 +114,12 @@ export const StateContext = ({ children }) => {
onRemove,
setCartItems,
setTotalPrice,
setTotalQuantities
setTotalQuantities,
}}
>
{children}
</Context.Provider>
)
}
);
};

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