Skip to content

Commit 8609536

Browse files
ktsnyyx990803
authored andcommitted
move module specific getters and actions into each module (#399)
1 parent 1fbf9cd commit 8609536

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import shop from '../api/shop'
21
import * as types from './mutation-types'
32

43
export const addToCart = ({ commit }, product) => {
@@ -8,19 +7,3 @@ export const addToCart = ({ commit }, product) => {
87
})
98
}
109
}
11-
12-
export const checkout = ({ commit, state }, products) => {
13-
const savedCartItems = [...state.cart.added]
14-
commit(types.CHECKOUT_REQUEST)
15-
shop.buyProducts(
16-
products,
17-
() => commit(types.CHECKOUT_SUCCESS),
18-
() => commit(types.CHECKOUT_FAILURE, { savedCartItems })
19-
)
20-
}
21-
22-
export const getAllProducts = ({ commit }) => {
23-
shop.getProducts(products => {
24-
commit(types.RECEIVE_PRODUCTS, { products })
25-
})
26-
}

examples/shopping-cart/store/getters.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
export const checkoutStatus = state => state.cart.checkoutStatus
2-
3-
export const allProducts = state => state.products.all
4-
51
export const cartProducts = state => {
62
return state.cart.added.map(({ id, quantity }) => {
73
const product = state.products.all.find(p => p.id === id)

examples/shopping-cart/store/modules/cart.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import shop from '../../api/shop'
12
import * as types from '../mutation-types'
23

34
// initial state
@@ -7,6 +8,24 @@ const state = {
78
checkoutStatus: null
89
}
910

11+
// getters
12+
const getters = {
13+
checkoutStatus: state => state.checkoutStatus
14+
}
15+
16+
// actions
17+
const actions = {
18+
checkout ({ commit, state }, products) {
19+
const savedCartItems = [...state.added]
20+
commit(types.CHECKOUT_REQUEST)
21+
shop.buyProducts(
22+
products,
23+
() => commit(types.CHECKOUT_SUCCESS),
24+
() => commit(types.CHECKOUT_FAILURE, { savedCartItems })
25+
)
26+
}
27+
}
28+
1029
// mutations
1130
const mutations = {
1231
[types.ADD_TO_CART] (state, { id }) {
@@ -41,5 +60,7 @@ const mutations = {
4160

4261
export default {
4362
state,
63+
getters,
64+
actions,
4465
mutations
4566
}

examples/shopping-cart/store/modules/products.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1+
import shop from '../../api/shop'
12
import * as types from '../mutation-types'
23

34
// initial state
45
const state = {
56
all: []
67
}
78

9+
// getters
10+
const getters = {
11+
allProducts: state => state.all
12+
}
13+
14+
// actions
15+
const actions = {
16+
getAllProducts ({ commit }) {
17+
shop.getProducts(products => {
18+
commit(types.RECEIVE_PRODUCTS, { products })
19+
})
20+
}
21+
}
22+
823
// mutations
924
const mutations = {
1025
[types.RECEIVE_PRODUCTS] (state, { products }) {
@@ -18,5 +33,7 @@ const mutations = {
1833

1934
export default {
2035
state,
36+
getters,
37+
actions,
2138
mutations
2239
}

0 commit comments

Comments
 (0)