Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Redux Reducer - Add to Cart Action Functionality with Conditions

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,
            },
          ],
        };
   }
}

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You are not returning the result of the map. Try doing this

  return state.cart.map((item) => ...
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!