Reputation: 35
When the product is in the cart the both conditions return empty array for the cart.
The cart has 3 objects: the product, quantity, and variants (which are color and size)
Any help please
// Cart reducer
const shopReducer = (state = INITSTATE, action) => {
switch (action.type) {
case actionTypes.ADD_TO_CART:
const product = state.products.find((p) => p.id === action.payload.id);
const inCart =
state.cart.find((item) => item)?.product.id === action.payload.id
? //state.cart.map((item) => item?.product.id === action.payload.id)
true
: false;
if (inCart)
state.cart.map((item) => {
if (
item.product.id === action.payload.id &&
item.setVar === action.payload.setVar
)
return {
...state,
cart: {...item, quantity: item.quantity+1},
};
else
return {
...state,
cart: [
...state.cart,
{ product: product, quantity: 1, setVar: action.payload.setVar },
],
}
});
else
return {
...state,
cart: [
{
product: product,
quantity: 1,
setVar: action.payload.setVar,
},
],
};
}
}
Upvotes: 0
Views: 444
Reputation: 370
You are not returning the result of the map. Try doing this
return state.cart.map((item) => ...
Upvotes: 1