فهرست منبع

Update shopping cart example (#1225)

* remove getters that return a piece of the state as is

* make namespaced modules

* rename cart's `added` prop to `items`
Alex Kyriakidis 7 سال پیش
والد
کامیت
fe5f25215f

+ 5 - 5
examples/shopping-cart/components/ProductList.vue

@@ -13,17 +13,17 @@
 </template>
 </template>
 
 
 <script>
 <script>
-import { mapGetters, mapActions } from 'vuex'
+import { mapState, mapActions } from 'vuex'
 
 
 export default {
 export default {
-  computed: mapGetters({
-    products: 'allProducts'
+  computed: mapState({
+    products: state => state.products.all
   }),
   }),
-  methods: mapActions([
+  methods: mapActions('cart', [
     'addProductToCart'
     'addProductToCart'
   ]),
   ]),
   created () {
   created () {
-    this.$store.dispatch('getAllProducts')
+    this.$store.dispatch('products/getAllProducts')
   }
   }
 }
 }
 </script>
 </script>

+ 6 - 4
examples/shopping-cart/components/ShoppingCart.vue

@@ -14,19 +14,21 @@
 </template>
 </template>
 
 
 <script>
 <script>
-import { mapGetters } from 'vuex'
+import { mapGetters, mapState } from 'vuex'
 
 
 export default {
 export default {
   computed: {
   computed: {
-    ...mapGetters({
+    ...mapState({
+      checkoutStatus: state => state.cart.checkoutStatus
+    }),
+    ...mapGetters('cart', {
       products: 'cartProducts',
       products: 'cartProducts',
-      checkoutStatus: 'checkoutStatus',
       total: 'cartTotalPrice'
       total: 'cartTotalPrice'
     })
     })
   },
   },
   methods: {
   methods: {
     checkout (products) {
     checkout (products) {
-      this.$store.dispatch('checkout', products)
+      this.$store.dispatch('cart/checkout', products)
     }
     }
   }
   }
 }
 }

+ 9 - 10
examples/shopping-cart/store/modules/cart.js

@@ -3,16 +3,14 @@ import shop from '../../api/shop'
 // initial state
 // initial state
 // shape: [{ id, quantity }]
 // shape: [{ id, quantity }]
 const state = {
 const state = {
-  added: [],
+  items: [],
   checkoutStatus: null
   checkoutStatus: null
 }
 }
 
 
 // getters
 // getters
 const getters = {
 const getters = {
-  checkoutStatus: state => state.checkoutStatus,
-
   cartProducts: (state, getters, rootState) => {
   cartProducts: (state, getters, rootState) => {
-    return state.added.map(({ id, quantity }) => {
+    return state.items.map(({ id, quantity }) => {
       const product = rootState.products.all.find(product => product.id === id)
       const product = rootState.products.all.find(product => product.id === id)
       return {
       return {
         title: product.title,
         title: product.title,
@@ -32,7 +30,7 @@ const getters = {
 // actions
 // actions
 const actions = {
 const actions = {
   checkout ({ commit, state }, products) {
   checkout ({ commit, state }, products) {
-    const savedCartItems = [...state.added]
+    const savedCartItems = [...state.items]
     commit('setCheckoutStatus', null)
     commit('setCheckoutStatus', null)
     // empty cart
     // empty cart
     commit('setCartItems', { items: [] })
     commit('setCartItems', { items: [] })
@@ -50,14 +48,14 @@ const actions = {
   addProductToCart ({ state, commit }, product) {
   addProductToCart ({ state, commit }, product) {
     commit('setCheckoutStatus', null)
     commit('setCheckoutStatus', null)
     if (product.inventory > 0) {
     if (product.inventory > 0) {
-      const cartItem = state.added.find(item => item.id === product.id)
+      const cartItem = state.items.find(item => item.id === product.id)
       if (!cartItem) {
       if (!cartItem) {
         commit('pushProductToCart', { id: product.id })
         commit('pushProductToCart', { id: product.id })
       } else {
       } else {
         commit('incrementItemQuantity', cartItem)
         commit('incrementItemQuantity', cartItem)
       }
       }
       // remove 1 item from stock
       // remove 1 item from stock
-      commit('decrementProductInventory', { id: product.id })
+      commit('products/decrementProductInventory', { id: product.id }, { root: true })
     }
     }
   }
   }
 }
 }
@@ -65,19 +63,19 @@ const actions = {
 // mutations
 // mutations
 const mutations = {
 const mutations = {
   pushProductToCart (state, { id }) {
   pushProductToCart (state, { id }) {
-    state.added.push({
+    state.items.push({
       id,
       id,
       quantity: 1
       quantity: 1
     })
     })
   },
   },
 
 
   incrementItemQuantity (state, { id }) {
   incrementItemQuantity (state, { id }) {
-    const cartItem = state.added.find(item => item.id === id)
+    const cartItem = state.items.find(item => item.id === id)
     cartItem.quantity++
     cartItem.quantity++
   },
   },
 
 
   setCartItems (state, { items }) {
   setCartItems (state, { items }) {
-    state.added = items
+    state.items = items
   },
   },
 
 
   setCheckoutStatus (state, status) {
   setCheckoutStatus (state, status) {
@@ -86,6 +84,7 @@ const mutations = {
 }
 }
 
 
 export default {
 export default {
+  namespaced: true,
   state,
   state,
   getters,
   getters,
   actions,
   actions,

+ 2 - 3
examples/shopping-cart/store/modules/products.js

@@ -6,9 +6,7 @@ const state = {
 }
 }
 
 
 // getters
 // getters
-const getters = {
-  allProducts: state => state.all
-}
+const getters = {}
 
 
 // actions
 // actions
 const actions = {
 const actions = {
@@ -32,6 +30,7 @@ const mutations = {
 }
 }
 
 
 export default {
 export default {
+  namespaced: true,
   state,
   state,
   getters,
   getters,
   actions,
   actions,