Skip to content

updated toggleCartItemQuantity function - #68

Open
Jared-Jianbin wants to merge 1 commit into
adrianhajdin:mainfrom
Jared-Jianbin:jaredli
Open

updated toggleCartItemQuantity function#68
Jared-Jianbin wants to merge 1 commit into
adrianhajdin:mainfrom
Jared-Jianbin:jaredli

Conversation

@Jared-Jianbin

Copy link
Copy Markdown

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

vercel Bot commented Sep 24, 2022

Copy link
Copy Markdown

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
Copy Markdown

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

kko3ch commented Oct 12, 2022

Copy link
Copy Markdown

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