I wan to send multiple objects to cart in one click. For that I'm looping an array and calling addProductToCart but it saves only one last object in CartContext state.
When sending objects one by one (from different pages, or some objects from same page if sending by one, but not all) it's save in state of CartContext.
When I test this I set delay when looping addProductToCart and saw that every object where in CartContext state, but next object overwrites previous object.
import React from "react";
import { useContext } from "react";
import { CartContext } from "../../contexts/cart.context";
import Button, { BUTTON_TYPE_CLASSES } from "../buttons/buttons.component";
import {
BuyButtonCartSymbol,
BuyButtonCartTitle,
CurrentPrice,
CurrentPriceCont,
CurrentPriceSymbol,
OldPrice,
OldPriceCont,
OldPriceSymbol,
PriceDetailCont,
QuantityOldPrice,
Quantity,
} from "./priceDetail.style";
const PriceDetail = (...props) => {
const [object] = props;
const { addItemToCart } = useContext(CartContext);
const handleOptionsFocus = () => {
object.setFocusClicked(true);
};
let oldPriceSum = 0;
let newPriceSum = 0;
// Sum old prices
for (const value of object.arrayOfObjectsSelected) {
if (value.oldPrice) {
oldPriceSum += value.oldPrice;
} else {
oldPriceSum += value.price;
}
}
// Sum current prices
for (const value of object.arrayOfObjectsSelected) {
newPriceSum += value.price;
}
//Multiple objects sending to cart
var testArr = object.arrayOfObjectsSelected;
const addProductToCart = () => {
testArr.forEach((prod, ind) => {
console.log(ind, prod);
addItemToCart(prod);
});
};
return (
<PriceDetailCont>
<QuantityOldPrice>
{oldPriceSum !== newPriceSum && (
<OldPriceCont>
<OldPrice>{oldPriceSum.toLocaleString("fr-FR")}</OldPrice>
<OldPriceSymbol> ₽</OldPriceSymbol>
</OldPriceCont>
)}
{object.selectedOptions.length > 1 && (
<Quantity onClick={handleOptionsFocus}>
[ {object.selectedOptions.length}{" "}
{object.selectedOptions.length < 5 ? "pred" : "predov"} ]
</Quantity>
)}
</QuantityOldPrice>
<CurrentPriceCont>
<CurrentPrice>{newPriceSum.toLocaleString("fr-FR")}</CurrentPrice>
<CurrentPriceSymbol> ₽</CurrentPriceSymbol>
</CurrentPriceCont>
<Button
buttonType={BUTTON_TYPE_CLASSES.buy}
type="button"
onClick={addProductToCart}
>
<BuyButtonCartSymbol />
<BuyButtonCartTitle>to Cart</BuyButtonCartTitle>
</Button>
<Button buttonType={BUTTON_TYPE_CLASSES.oneClick} type="button">
Instant buy
</Button>
</PriceDetailCont>
);
};
export default PriceDetail;
import { createContext, useState } from "react";
// Helper function to add item to cart with conditions
const addCartItem = (cartItems, productToAdd) => {
const existingCartItem = cartItems.find(
(cartItem) => cartItem.pid === productToAdd.pid
);
if (existingCartItem) {
return cartItems.map((cartItem) =>
cartItem.pid === productToAdd.pid
? { ...cartItem, quantity: cartItem.quantity + 1 }
: cartItem
);
}
return [...cartItems, { ...productToAdd, quantity: 1 }];
};
export const CartContext = createContext({
cartItems: [],
addItemToCart: () => {},
});
export const CartProvider = ({ children }) => {
const [cartItems, setCartItems] = useState([]);
const addItemToCart = (productToAdd) => {
setCartItems(addCartItem(cartItems, productToAdd));
};
const value = { cartItems, addItemToCart };
return <CartContext.Provider value={value}>{children}</CartContext.Provider>;
};