Skip to content

Commit fe55af5

Browse files
committed
update cart example for new API
1 parent e3183d6 commit fe55af5

File tree

8 files changed

+65
-57
lines changed

8 files changed

+65
-57
lines changed

examples/shopping-cart/components/Cart.vue

+11-9
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@
1414
</template>
1515

1616
<script>
17-
import store from '../store'
18-
const { checkout } = store.actions
17+
import { checkout } from '../store/actions'
18+
import { cartProducts } from '../store/getters'
1919
2020
export default {
21-
computed: {
22-
products: store.getters.cartProducts,
23-
checkoutStatus () {
24-
return store.state.cart.lastCheckout
21+
vuex: {
22+
state: {
23+
products: cartProducts,
24+
checkoutStatus: ({ cart }) => cart.lastCheckout
2525
},
26+
actions: {
27+
checkout
28+
}
29+
},
30+
computed: {
2631
total () {
2732
return this.products.reduce((total, p) => {
2833
return total + p.price * p.quantity
2934
}, 0)
3035
}
31-
},
32-
methods: {
33-
checkout
3436
}
3537
}
3638
</script>

examples/shopping-cart/components/ProductList.vue

+9-13
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,20 @@
1313
</template>
1414

1515
<script>
16-
import store from '../store'
17-
const { getAllProducts, addToCart } = store.actions
16+
import { getAllProducts, addToCart } from '../store/actions'
1817
1918
export default {
20-
computed: {
21-
products () {
22-
return store.state.products
19+
vuex: {
20+
state: {
21+
products: ({ products }) => products.all
22+
},
23+
actions: {
24+
getAllProducts,
25+
addToCart
2326
}
2427
},
2528
created () {
26-
getAllProducts()
27-
},
28-
methods: {
29-
addToCart (product) {
30-
if (product.inventory > 0) {
31-
addToCart(product.id)
32-
}
33-
}
29+
this.getAllProducts()
3430
}
3531
}
3632
</script>

examples/shopping-cart/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import 'babel-polyfill'
22
import Vue from 'vue'
33
import App from './components/App.vue'
4+
import store from './store'
45

56
new Vue({
67
el: 'body',
8+
store,
79
components: { App }
810
})

examples/shopping-cart/store/actions.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import shop from '../api/shop'
22
import * as types from './mutation-types'
33

4-
export const addToCart = types.ADD_TO_CART
4+
export const addToCart = ({ dispatch }, product) => {
5+
if (product.inventory > 0) {
6+
dispatch(types.ADD_TO_CART, product.id)
7+
}
8+
}
59

610
export const checkout = ({ dispatch, state }, products) => {
711
const savedCartItems = [...state.cart.added]

examples/shopping-cart/store/getters.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export function cartProducts (state) {
1+
export const cartProducts = state => {
22
return state.cart.added.map(({ id, quantity }) => {
3-
const product = state.products.find(p => p.id === id)
3+
const product = state.products.all.find(p => p.id === id)
44
return {
55
title: product.title,
66
price: product.price,

examples/shopping-cart/store/index.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import Vue from 'vue'
22
import Vuex from '../../../src'
3-
import * as actions from './actions'
4-
import * as getters from './getters'
5-
import { cartInitialState, cartMutations } from './modules/cart'
6-
import { productsInitialState, productsMutations } from './modules/products'
3+
import cart from './modules/cart'
4+
import products from './modules/products'
75

86
Vue.use(Vuex)
97
Vue.config.debug = true
108

119
const debug = process.env.NODE_ENV !== 'production'
1210

1311
export default new Vuex.Store({
14-
state: {
15-
cart: cartInitialState,
16-
products: productsInitialState
12+
modules: {
13+
cart,
14+
products
1715
},
18-
actions,
19-
getters,
20-
mutations: [cartMutations, productsMutations],
2116
strict: debug,
2217
middlewares: debug ? [Vuex.createLogger()] : []
2318
})

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

+19-14
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import {
77

88
// initial state
99
// shape: [{ id, quantity }]
10-
export const cartInitialState = {
10+
const state = {
1111
added: [],
1212
lastCheckout: null
1313
}
1414

1515
// mutations
16-
export const cartMutations = {
17-
[ADD_TO_CART] ({ cart }, productId) {
18-
cart.lastCheckout = null
19-
const record = cart.added.find(p => p.id === productId)
16+
const mutations = {
17+
[ADD_TO_CART] (state, productId) {
18+
state.lastCheckout = null
19+
const record = state.added.find(p => p.id === productId)
2020
if (!record) {
21-
cart.added.push({
21+
state.added.push({
2222
id: productId,
2323
quantity: 1
2424
})
@@ -27,19 +27,24 @@ export const cartMutations = {
2727
}
2828
},
2929

30-
[CHECKOUT_REQUEST] ({ cart }) {
30+
[CHECKOUT_REQUEST] (state) {
3131
// clear cart
32-
cart.added = []
33-
cart.lastCheckout = null
32+
state.added = []
33+
state.lastCheckout = null
3434
},
3535

36-
[CHECKOUT_SUCCESS] ({ cart }) {
37-
cart.lastCheckout = 'successful'
36+
[CHECKOUT_SUCCESS] (state) {
37+
state.lastCheckout = 'successful'
3838
},
3939

40-
[CHECKOUT_FAILURE] ({ cart }, savedCartItems) {
40+
[CHECKOUT_FAILURE] (state, savedCartItems) {
4141
// rollback to the cart saved before sending the request
42-
cart.added = savedCartItems
43-
cart.lastCheckout = 'failed'
42+
state.added = savedCartItems
43+
state.lastCheckout = 'failed'
4444
}
4545
}
46+
47+
export default {
48+
state,
49+
mutations
50+
}

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ import {
44
} from '../mutation-types'
55

66
// initial state
7-
export const productsInitialState = []
7+
const state = {
8+
all: []
9+
}
810

911
// mutations
10-
export const productsMutations = {
12+
const mutations = {
1113
[RECEIVE_PRODUCTS] (state, products) {
12-
state.products = products
14+
state.all = products
1315
},
1416

15-
[ADD_TO_CART] ({ products }, productId) {
16-
const product = products.find(p => p.id === productId)
17-
if (product.inventory > 0) {
18-
product.inventory--
19-
}
17+
[ADD_TO_CART] (state, productId) {
18+
state.all.find(p => p.id === productId).inventory--
2019
}
2120
}
21+
22+
export default {
23+
state,
24+
mutations
25+
}

0 commit comments

Comments
 (0)