From 9e2dbfb32b6fc309df5236218380e808fdb090e8 Mon Sep 17 00:00:00 2001 From: ecki-d Date: Wed, 21 Aug 2019 09:52:00 +0200 Subject: [PATCH 1/2] :tada: Alternative addToCart logic: perf --- store/index.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/store/index.js b/store/index.js index f90f36a..8577af4 100644 --- a/store/index.js +++ b/store/index.js @@ -31,14 +31,9 @@ export const mutations = { ;(state.cart = []), (state.cartUIStatus = "idle") }, addToCart: (state, payload) => { - let itemfound = false - state.cart.forEach(el => { - if (el.id === payload.id) { - el.quantity += payload.quantity - itemfound = true - } - }) - if (!itemfound) state.cart.push(payload) + let itemfound = state.cart.find(el => el.id === payload.id) + if (!itemfound) return void state.cart.push(payload) + itemfound.quantity += payload.quantity } } From f4f65590cf2136df8ac768ab4dfb205c5126f4a2 Mon Sep 17 00:00:00 2001 From: ecki-d Date: Fri, 23 Aug 2019 13:23:18 +0200 Subject: [PATCH 2/2] :sparkles: Remove void return in addToCart. --- store/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/store/index.js b/store/index.js index 8577af4..abaf333 100644 --- a/store/index.js +++ b/store/index.js @@ -32,8 +32,9 @@ export const mutations = { }, addToCart: (state, payload) => { let itemfound = state.cart.find(el => el.id === payload.id) - if (!itemfound) return void state.cart.push(payload) - itemfound.quantity += payload.quantity + itemfound + ? (itemfound.quantity += payload.quantity) + : state.cart.push(payload) } }