Redux increase quantity in a shopping cart












-1














I am struggling with the function of my reducer to add an item and increase its quantity if already present in cart.
What my code does so far is adding another "quantity" with 1 instead of updating the quantity already present in my state.



Here's my code :



reducer :



import { ADD_TO_CART } from "../actions/types";

export default function(state = , action) {
switch (action.type) {
case ADD_TO_CART:
if (state.findIndex(el => el.item.title === action.item.title) === -1) {
return [...state, { item: action.item, quantity: action.quantity + 1 }];
} else {
return [...state, { quantity: action.quantity + 1 }];
}
default:
return state;
}
}


action :



 import { ADD_TO_CART } from "./types";
import axios from "axios";

export const addToCart = id => dispatch => {
axios
.get(`https://api.itbook.store/1.0/search/${id}`)
.then(items =>
items.map((item, quantity) =>
dispatch({
type: ADD_TO_CART,
item,
quantity
})
)
);
};


Thanks










share|improve this question



























    -1














    I am struggling with the function of my reducer to add an item and increase its quantity if already present in cart.
    What my code does so far is adding another "quantity" with 1 instead of updating the quantity already present in my state.



    Here's my code :



    reducer :



    import { ADD_TO_CART } from "../actions/types";

    export default function(state = , action) {
    switch (action.type) {
    case ADD_TO_CART:
    if (state.findIndex(el => el.item.title === action.item.title) === -1) {
    return [...state, { item: action.item, quantity: action.quantity + 1 }];
    } else {
    return [...state, { quantity: action.quantity + 1 }];
    }
    default:
    return state;
    }
    }


    action :



     import { ADD_TO_CART } from "./types";
    import axios from "axios";

    export const addToCart = id => dispatch => {
    axios
    .get(`https://api.itbook.store/1.0/search/${id}`)
    .then(items =>
    items.map((item, quantity) =>
    dispatch({
    type: ADD_TO_CART,
    item,
    quantity
    })
    )
    );
    };


    Thanks










    share|improve this question

























      -1












      -1








      -1







      I am struggling with the function of my reducer to add an item and increase its quantity if already present in cart.
      What my code does so far is adding another "quantity" with 1 instead of updating the quantity already present in my state.



      Here's my code :



      reducer :



      import { ADD_TO_CART } from "../actions/types";

      export default function(state = , action) {
      switch (action.type) {
      case ADD_TO_CART:
      if (state.findIndex(el => el.item.title === action.item.title) === -1) {
      return [...state, { item: action.item, quantity: action.quantity + 1 }];
      } else {
      return [...state, { quantity: action.quantity + 1 }];
      }
      default:
      return state;
      }
      }


      action :



       import { ADD_TO_CART } from "./types";
      import axios from "axios";

      export const addToCart = id => dispatch => {
      axios
      .get(`https://api.itbook.store/1.0/search/${id}`)
      .then(items =>
      items.map((item, quantity) =>
      dispatch({
      type: ADD_TO_CART,
      item,
      quantity
      })
      )
      );
      };


      Thanks










      share|improve this question













      I am struggling with the function of my reducer to add an item and increase its quantity if already present in cart.
      What my code does so far is adding another "quantity" with 1 instead of updating the quantity already present in my state.



      Here's my code :



      reducer :



      import { ADD_TO_CART } from "../actions/types";

      export default function(state = , action) {
      switch (action.type) {
      case ADD_TO_CART:
      if (state.findIndex(el => el.item.title === action.item.title) === -1) {
      return [...state, { item: action.item, quantity: action.quantity + 1 }];
      } else {
      return [...state, { quantity: action.quantity + 1 }];
      }
      default:
      return state;
      }
      }


      action :



       import { ADD_TO_CART } from "./types";
      import axios from "axios";

      export const addToCart = id => dispatch => {
      axios
      .get(`https://api.itbook.store/1.0/search/${id}`)
      .then(items =>
      items.map((item, quantity) =>
      dispatch({
      type: ADD_TO_CART,
      item,
      quantity
      })
      )
      );
      };


      Thanks







      javascript reactjs redux






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 27 '18 at 16:13









      Charlote22

      204




      204
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You are finding the index (which is great) but not doing anything with it (which is not so great):



          import { ADD_TO_CART } from "../actions/types";

          export default function(state = , action) {
          switch (action.type) {
          case ADD_TO_CART:
          const index = state.findIndex(el => el.item.title === action.item.title);
          if (index === -1) {
          return [...state, { item: action.item, quantity: action.quantity + 1 }];
          } else {
          // Use map to create a new state object
          return state.map((item, i) =>
          index === i //Only modify the found index
          ? { ...item, quantity: item.quantity + action.quantity } //Add the required quantity to the current quantity (not too sure about this)
          : item //Don't modify other items
          );
          }
          default:
          return state;
          }
          }





          share|improve this answer





















          • Thanks it works (well I had to add another "+1" after "action.quantity inside the ternary so it adds 1 each time). Thanks for the explanation. I didn't think about reusing the index.
            – Charlote22
            Dec 28 '18 at 9:33



















          0














          import { ADD_TO_CART } from "../actions/types";

          export default function (state = , action) {
          switch (action.type) {
          case ADD_TO_CART:
          const index = state.findIndex(el => el.item.title === action.item.title);
          if (index > -1) {
          const newState = [...state];
          newState[index] = { ...newState[index], quantity: action.quantity + 1 };
          return newState;
          } else {
          return [...state, { ...action.item, quantity: action.quantity + 1 }];
          }
          default:
          return state;
          }
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947869%2fredux-increase-quantity-in-a-shopping-cart%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You are finding the index (which is great) but not doing anything with it (which is not so great):



            import { ADD_TO_CART } from "../actions/types";

            export default function(state = , action) {
            switch (action.type) {
            case ADD_TO_CART:
            const index = state.findIndex(el => el.item.title === action.item.title);
            if (index === -1) {
            return [...state, { item: action.item, quantity: action.quantity + 1 }];
            } else {
            // Use map to create a new state object
            return state.map((item, i) =>
            index === i //Only modify the found index
            ? { ...item, quantity: item.quantity + action.quantity } //Add the required quantity to the current quantity (not too sure about this)
            : item //Don't modify other items
            );
            }
            default:
            return state;
            }
            }





            share|improve this answer





















            • Thanks it works (well I had to add another "+1" after "action.quantity inside the ternary so it adds 1 each time). Thanks for the explanation. I didn't think about reusing the index.
              – Charlote22
              Dec 28 '18 at 9:33
















            1














            You are finding the index (which is great) but not doing anything with it (which is not so great):



            import { ADD_TO_CART } from "../actions/types";

            export default function(state = , action) {
            switch (action.type) {
            case ADD_TO_CART:
            const index = state.findIndex(el => el.item.title === action.item.title);
            if (index === -1) {
            return [...state, { item: action.item, quantity: action.quantity + 1 }];
            } else {
            // Use map to create a new state object
            return state.map((item, i) =>
            index === i //Only modify the found index
            ? { ...item, quantity: item.quantity + action.quantity } //Add the required quantity to the current quantity (not too sure about this)
            : item //Don't modify other items
            );
            }
            default:
            return state;
            }
            }





            share|improve this answer





















            • Thanks it works (well I had to add another "+1" after "action.quantity inside the ternary so it adds 1 each time). Thanks for the explanation. I didn't think about reusing the index.
              – Charlote22
              Dec 28 '18 at 9:33














            1












            1








            1






            You are finding the index (which is great) but not doing anything with it (which is not so great):



            import { ADD_TO_CART } from "../actions/types";

            export default function(state = , action) {
            switch (action.type) {
            case ADD_TO_CART:
            const index = state.findIndex(el => el.item.title === action.item.title);
            if (index === -1) {
            return [...state, { item: action.item, quantity: action.quantity + 1 }];
            } else {
            // Use map to create a new state object
            return state.map((item, i) =>
            index === i //Only modify the found index
            ? { ...item, quantity: item.quantity + action.quantity } //Add the required quantity to the current quantity (not too sure about this)
            : item //Don't modify other items
            );
            }
            default:
            return state;
            }
            }





            share|improve this answer












            You are finding the index (which is great) but not doing anything with it (which is not so great):



            import { ADD_TO_CART } from "../actions/types";

            export default function(state = , action) {
            switch (action.type) {
            case ADD_TO_CART:
            const index = state.findIndex(el => el.item.title === action.item.title);
            if (index === -1) {
            return [...state, { item: action.item, quantity: action.quantity + 1 }];
            } else {
            // Use map to create a new state object
            return state.map((item, i) =>
            index === i //Only modify the found index
            ? { ...item, quantity: item.quantity + action.quantity } //Add the required quantity to the current quantity (not too sure about this)
            : item //Don't modify other items
            );
            }
            default:
            return state;
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 27 '18 at 16:25









            apokryfos

            18.2k42955




            18.2k42955












            • Thanks it works (well I had to add another "+1" after "action.quantity inside the ternary so it adds 1 each time). Thanks for the explanation. I didn't think about reusing the index.
              – Charlote22
              Dec 28 '18 at 9:33


















            • Thanks it works (well I had to add another "+1" after "action.quantity inside the ternary so it adds 1 each time). Thanks for the explanation. I didn't think about reusing the index.
              – Charlote22
              Dec 28 '18 at 9:33
















            Thanks it works (well I had to add another "+1" after "action.quantity inside the ternary so it adds 1 each time). Thanks for the explanation. I didn't think about reusing the index.
            – Charlote22
            Dec 28 '18 at 9:33




            Thanks it works (well I had to add another "+1" after "action.quantity inside the ternary so it adds 1 each time). Thanks for the explanation. I didn't think about reusing the index.
            – Charlote22
            Dec 28 '18 at 9:33













            0














            import { ADD_TO_CART } from "../actions/types";

            export default function (state = , action) {
            switch (action.type) {
            case ADD_TO_CART:
            const index = state.findIndex(el => el.item.title === action.item.title);
            if (index > -1) {
            const newState = [...state];
            newState[index] = { ...newState[index], quantity: action.quantity + 1 };
            return newState;
            } else {
            return [...state, { ...action.item, quantity: action.quantity + 1 }];
            }
            default:
            return state;
            }
            }





            share|improve this answer


























              0














              import { ADD_TO_CART } from "../actions/types";

              export default function (state = , action) {
              switch (action.type) {
              case ADD_TO_CART:
              const index = state.findIndex(el => el.item.title === action.item.title);
              if (index > -1) {
              const newState = [...state];
              newState[index] = { ...newState[index], quantity: action.quantity + 1 };
              return newState;
              } else {
              return [...state, { ...action.item, quantity: action.quantity + 1 }];
              }
              default:
              return state;
              }
              }





              share|improve this answer
























                0












                0








                0






                import { ADD_TO_CART } from "../actions/types";

                export default function (state = , action) {
                switch (action.type) {
                case ADD_TO_CART:
                const index = state.findIndex(el => el.item.title === action.item.title);
                if (index > -1) {
                const newState = [...state];
                newState[index] = { ...newState[index], quantity: action.quantity + 1 };
                return newState;
                } else {
                return [...state, { ...action.item, quantity: action.quantity + 1 }];
                }
                default:
                return state;
                }
                }





                share|improve this answer












                import { ADD_TO_CART } from "../actions/types";

                export default function (state = , action) {
                switch (action.type) {
                case ADD_TO_CART:
                const index = state.findIndex(el => el.item.title === action.item.title);
                if (index > -1) {
                const newState = [...state];
                newState[index] = { ...newState[index], quantity: action.quantity + 1 };
                return newState;
                } else {
                return [...state, { ...action.item, quantity: action.quantity + 1 }];
                }
                default:
                return state;
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 27 '18 at 16:27









                Alfred Ayi-bonte

                13916




                13916






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947869%2fredux-increase-quantity-in-a-shopping-cart%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas

                    Can't read property showImagePicker of undefined in react native iOS