Explorar o código

move module specific getters and actions into each module (#399)

katashin %!s(int64=8) %!d(string=hai) anos
pai
achega
86095364c3

+ 0 - 17
examples/shopping-cart/store/actions.js

@@ -1,4 +1,3 @@
-import shop from '../api/shop'
 import * as types from './mutation-types'
 
 export const addToCart = ({ commit }, product) => {
@@ -8,19 +7,3 @@ export const addToCart = ({ commit }, product) => {
     })
   }
 }
-
-export const checkout = ({ commit, state }, products) => {
-  const savedCartItems = [...state.cart.added]
-  commit(types.CHECKOUT_REQUEST)
-  shop.buyProducts(
-    products,
-    () => commit(types.CHECKOUT_SUCCESS),
-    () => commit(types.CHECKOUT_FAILURE, { savedCartItems })
-  )
-}
-
-export const getAllProducts = ({ commit }) => {
-  shop.getProducts(products => {
-    commit(types.RECEIVE_PRODUCTS, { products })
-  })
-}

+ 0 - 4
examples/shopping-cart/store/getters.js

@@ -1,7 +1,3 @@
-export const checkoutStatus = state => state.cart.checkoutStatus
-
-export const allProducts = state => state.products.all
-
 export const cartProducts = state => {
   return state.cart.added.map(({ id, quantity }) => {
     const product = state.products.all.find(p => p.id === id)

+ 21 - 0
examples/shopping-cart/store/modules/cart.js

@@ -1,3 +1,4 @@
+import shop from '../../api/shop'
 import * as types from '../mutation-types'
 
 // initial state
@@ -7,6 +8,24 @@ const state = {
   checkoutStatus: null
 }
 
+// getters
+const getters = {
+  checkoutStatus: state => state.checkoutStatus
+}
+
+// actions
+const actions = {
+  checkout ({ commit, state }, products) {
+    const savedCartItems = [...state.added]
+    commit(types.CHECKOUT_REQUEST)
+    shop.buyProducts(
+      products,
+      () => commit(types.CHECKOUT_SUCCESS),
+      () => commit(types.CHECKOUT_FAILURE, { savedCartItems })
+    )
+  }
+}
+
 // mutations
 const mutations = {
   [types.ADD_TO_CART] (state, { id }) {
@@ -41,5 +60,7 @@ const mutations = {
 
 export default {
   state,
+  getters,
+  actions,
   mutations
 }

+ 17 - 0
examples/shopping-cart/store/modules/products.js

@@ -1,3 +1,4 @@
+import shop from '../../api/shop'
 import * as types from '../mutation-types'
 
 // initial state
@@ -5,6 +6,20 @@ const state = {
   all: []
 }
 
+// getters
+const getters = {
+  allProducts: state => state.all
+}
+
+// actions
+const actions = {
+  getAllProducts ({ commit }) {
+    shop.getProducts(products => {
+      commit(types.RECEIVE_PRODUCTS, { products })
+    })
+  }
+}
+
 // mutations
 const mutations = {
   [types.RECEIVE_PRODUCTS] (state, { products }) {
@@ -18,5 +33,7 @@ const mutations = {
 
 export default {
   state,
+  getters,
+  actions,
   mutations
 }