Skip to content

Commit

Permalink
ISSUE storefront-foundation#202 Update quantity on click of add to ca…
Browse files Browse the repository at this point in the history
…rt if product already exist in cart
  • Loading branch information
developeryashraj committed Jan 17, 2022
1 parent 489f632 commit 54e3988
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/mock-connector/utils/cartStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import createProduct from './createProduct'
const CART_COOKIE = 'rsf_mock_cart'

const initialStore = [
{ id: 1, quantity: 1 },
{ id: 2, quantity: 1 },
{ id: '1', quantity: 1 },
{ id: '2', quantity: 1 },
]

function getStore(req, res) {
Expand Down Expand Up @@ -43,7 +43,12 @@ export function removeItem(id, req, res) {
}

export function addItem(id, quantity, req, res) {
const newStore = [{ id, quantity }, ...getStore(req, res)]
const currentStore = getStore(req, res);
const isItemExist = currentStore.find((item) => item.id === id);
if (isItemExist !== undefined) {
return updateItem(id, isItemExist.quantity + 1, req, res);
}
const newStore = [{ id, quantity }, ...currentStore]
res.setHeader('Set-Cookie', `${CART_COOKIE}=${JSON.stringify(newStore)}; Path=/`)
return newStore.map(toProduct)
}

0 comments on commit 54e3988

Please sign in to comment.