Browse Source

[ADD] add to cart feature

Gogs 7 years ago
parent
commit
dea7a7e8e9

+ 2 - 2
src/components/CartItem.vue

@@ -3,9 +3,9 @@
         h3.item-name {{ data.name }}
         input.item-quantity(type="text" value="1")
         span.item-x x
-        span.item-price Gs {{ data.price }}
+        span.item-price Gs {{ data.list_price }}
         span.item-equals =
-        span.item-subtotal Gs {{ data.price }}
+        span.item-subtotal Gs {{ data.list_price }}
 </template>
 
 <script>

+ 7 - 2
src/components/ProductCard.vue

@@ -1,5 +1,5 @@
 <template lang="pug">
-    .product-card
+    .product-card(@click="selectProduct({ data })")
         h2.product-title {{ data.name }}
         img.product-image(src="/web/static/src/img/placeholder.png")
         .product-price
@@ -8,6 +8,8 @@
 </template>
 
 <script>
+    import { mapActions } from 'vuex'
+
     export default {
         props: {
             data: {
@@ -16,7 +18,10 @@
                     return {}
                 }
             }
-        }
+        },
+        methods: mapActions([
+            'selectProduct'
+        ])
     }
 </script>
 

+ 7 - 45
src/store/modules/cart.js

@@ -1,42 +1,5 @@
 const state = {
-    cart: [
-        {
-            id: 1,
-            name: "Product 001",
-            price: 3500,
-            qty: 2,
-            subtotal: 7000
-        },
-        {
-            id: 2,
-            name: "Product 002",
-            price: 4000,
-            qty: 1,
-            subtotal: 4000
-        },
-        {
-            id: 3,
-            name: "Product 003",
-            price: 3500,
-            qty: 1,
-            subtotal: 3500
-        },
-        {
-            id: 4,
-            name: "Product 004",
-            price: 3800,
-            qty: 2,
-            subtotal: 7600
-        },
-        {
-            id: 5,
-            name: "Product 005",
-            price: 1000,
-            qty: 3,
-            subtotal: 3000
-        }
-    ],
-    selectedCart: []
+    cart: []
 }
 
 const getters = {
@@ -44,20 +7,19 @@ const getters = {
         return state.cart
     },
     isEmpty(state) {
-        return state.cart.legth !== 0
+        return state.cart.length !== 0
     }
 }
 
 const mutations = {
-
+    addToCart(state, payload) {
+        state.cart.push(payload.data)
+    }
 }
 
 const actions = {
-    addToCart(state) {
-
-    },
-    removeFromCart(state) {
-
+    addToCart({ commit }, payload) {
+        commit('addToCart', payload)
     }
 }
 

+ 2 - 2
src/store/modules/products.js

@@ -36,8 +36,8 @@ const actions = {
             console.log(error)
         })
     },
-    selectProduct({ commit }) {
-
+    selectProduct({ dispatch }, payload) {
+        dispatch('addToCart', payload)
     }
 }