File tree Expand file tree Collapse file tree 4 files changed +38
-21
lines changed
examples/shopping-cart/store Expand file tree Collapse file tree 4 files changed +38
-21
lines changed Original file line number Diff line number Diff line change 1
- import shop from '../api/shop'
2
1
import * as types from './mutation-types'
3
2
4
3
export const addToCart = ( { commit } , product ) => {
@@ -8,19 +7,3 @@ export const addToCart = ({ commit }, product) => {
8
7
} )
9
8
}
10
9
}
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
- }
Original file line number Diff line number Diff line change 1
- export const checkoutStatus = state => state . cart . checkoutStatus
2
-
3
- export const allProducts = state => state . products . all
4
-
5
1
export const cartProducts = state => {
6
2
return state . cart . added . map ( ( { id, quantity } ) => {
7
3
const product = state . products . all . find ( p => p . id === id )
Original file line number Diff line number Diff line change
1
+ import shop from '../../api/shop'
1
2
import * as types from '../mutation-types'
2
3
3
4
// initial state
@@ -7,6 +8,24 @@ const state = {
7
8
checkoutStatus : null
8
9
}
9
10
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
+
10
29
// mutations
11
30
const mutations = {
12
31
[ types . ADD_TO_CART ] ( state , { id } ) {
@@ -41,5 +60,7 @@ const mutations = {
41
60
42
61
export default {
43
62
state,
63
+ getters,
64
+ actions,
44
65
mutations
45
66
}
Original file line number Diff line number Diff line change
1
+ import shop from '../../api/shop'
1
2
import * as types from '../mutation-types'
2
3
3
4
// initial state
4
5
const state = {
5
6
all : [ ]
6
7
}
7
8
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
+
8
23
// mutations
9
24
const mutations = {
10
25
[ types . RECEIVE_PRODUCTS ] ( state , { products } ) {
@@ -18,5 +33,7 @@ const mutations = {
18
33
19
34
export default {
20
35
state,
36
+ getters,
37
+ actions,
21
38
mutations
22
39
}
You can’t perform that action at this time.
0 commit comments