Browse Source

[ADD] another method for find reapeated item in cart

Gogs 7 years ago
parent
commit
5bfcd617be
2 changed files with 6 additions and 9 deletions
  1. 5 1
      src/store/modules/cart.js
  2. 1 8
      src/store/modules/products.js

+ 5 - 1
src/store/modules/cart.js

@@ -13,7 +13,11 @@ const getters = {
 
 const mutations = {
     addToCart(state, payload) {
-        state.cart.push(payload.data)
+        let finded = state.cart.find(item => item.id == payload.data.id)
+
+        if (!finded) {
+            state.cart = [payload.data, ...state.cart]
+        }
     }
 }
 

+ 1 - 8
src/store/modules/products.js

@@ -14,14 +14,7 @@ const getters = {
 
 const mutations = {
     pushProducts(state, payload) {
-        state.products = payload.products
-    },
-    selectProducts (state, payload) {
-        state.selectedProducts = []
-
-        payload.products.forEach(item => {
-            state.products.push(item)
-        });
+        state.products = [...payload.products]
     }
 }