Skip to content

Commit

Permalink
cart bug fix (important)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iskandar Kurbanov committed Nov 15, 2021
1 parent df48568 commit f947269
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion components/MiniCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default function MiniCart({ cart }) {

<ul role="list" className="-my-6 divide-y divide-gray-200">
{cart.map((product) => (
<li key={product.id} className="py-6 flex">
<li key={product.id + Math.random()} className="py-6 flex">
<div className="relative flex-shrink-0 w-24 h-24 border border-gray-200 rounded-md overflow-hidden">
<Image
src={product.image}
Expand Down
15 changes: 10 additions & 5 deletions context/shopContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ export default function ShopProvider({ children }) {

localStorage.setItem("checkout_id", JSON.stringify([newItem, checkout]))
} else {
let newCart = [...cart]

let newCart = []
let added = false

cart.map(item => {
if (item.id === newItem.id) {
item.variantQuantity++
newCart = [...cart]
} else {
newCart = [...cart, newItem]
}
added = true
}
})

if(!added) {
newCart = [...cart, newItem]
}


setCart(newCart)
const newCheckout = await updateCheckout(checkoutId, newCart)
localStorage.setItem("checkout_id", JSON.stringify([newCart, newCheckout]))
Expand Down

0 comments on commit f947269

Please sign in to comment.