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

updated toggleCartItemQuantity function #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Jared-Jianbin
Copy link

Fixed the quantity change position issue in the cart, the position is changed because the index of product changed in the array,
I used map to return them in the same order.

@vercel
Copy link

vercel bot commented Sep 24, 2022

Someone is attempting to deploy a commit to a Personal Account owned by @adrianhajdin on Vercel.

@adrianhajdin first needs to authorize it.

@mcjosh-sys
Copy link

Fix for toggleCartItemQuantity changing the order of product in cart

const toggleCartItemQuantity = (id, action) => {
foundProduct = cartItems.find(item => item._id === id)
index = cartItems.findIndex(product => product._id === id)
const currCartItem = cartItems.filter(item => item._id !== id)

  if(action === 'inc'){
        currCartItem.splice(index, 0, {...foundProduct, quantity: foundProduct.quantity+1})
        setCartItems(currCartItem)
        setTotalPrice(prevTotalPrice => prevTotalPrice + foundProduct.price)
        setTotalQuantities(prevTotalQuantities => prevTotalQuantities + 1)
  }else if(action === 'dec'){
      if(foundProduct.quantity > 1){
        currCartItem.splice(index, 0, {...foundProduct, quantity: foundProduct.quantity-1})
        setCartItems(currCartItem)
        setTotalPrice (prevTotalPrice => prevTotalPrice - foundProduct.price)
        setTotalQuantities(prevTotalQuantities => prevTotalQuantities - 1)
      }
  }

}

@kko3ch
Copy link

kko3ch commented Oct 12, 2022

Merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants