Skip to content

Commit 59c39b6

Browse files
hootlexktsn
authored andcommitted
Update shopping cart example (#1107)
* use `product` instead of abbreviation * move `cartProducts` getter to cart module * create `cartTotalPrice` getter instead of calculating the total in the Cart component * separate mutations that update the checkoutStatus and the cart items
1 parent 49f6c35 commit 59c39b6

File tree

7 files changed

+44
-46
lines changed

7 files changed

+44
-46
lines changed

examples/shopping-cart/components/Cart.vue

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<h2>Your Cart</h2>
44
<p v-show="!products.length"><i>Please add some products to cart.</i></p>
55
<ul>
6-
<li v-for="p in products">
7-
{{ p.title }} - {{ p.price | currency }} x {{ p.quantity }}
6+
<li v-for="product in products">
7+
{{ product.title }} - {{ product.price | currency }} x {{ product.quantity }}
88
</li>
99
</ul>
1010
<p>Total: {{ total | currency }}</p>
@@ -20,13 +20,9 @@ export default {
2020
computed: {
2121
...mapGetters({
2222
products: 'cartProducts',
23-
checkoutStatus: 'checkoutStatus'
24-
}),
25-
total () {
26-
return this.products.reduce((total, p) => {
27-
return total + p.price * p.quantity
28-
}, 0)
29-
}
23+
checkoutStatus: 'checkoutStatus',
24+
total: 'cartTotalPrice'
25+
})
3026
},
3127
methods: {
3228
checkout (products) {

examples/shopping-cart/components/ProductList.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<template>
22
<ul>
3-
<li v-for="p in products">
4-
{{ p.title }} - {{ p.price | currency }}
3+
<li v-for="product in products">
4+
{{ product.title }} - {{ product.price | currency }}
55
<br>
66
<button
7-
:disabled="!p.inventory"
8-
@click="addToCart(p)">
7+
:disabled="!product.inventory"
8+
@click="addToCart(product)">
99
Add to cart
1010
</button>
1111
</li>

examples/shopping-cart/store/getters.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/shopping-cart/store/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
33
import * as actions from './actions'
4-
import * as getters from './getters'
54
import cart from './modules/cart'
65
import products from './modules/products'
76
import createLogger from '../../../src/plugins/logger'
@@ -12,7 +11,6 @@ const debug = process.env.NODE_ENV !== 'production'
1211

1312
export default new Vuex.Store({
1413
actions,
15-
getters,
1614
modules: {
1715
cart,
1816
products

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

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,41 @@ const state = {
1010

1111
// getters
1212
const getters = {
13-
checkoutStatus: state => state.checkoutStatus
13+
checkoutStatus: state => state.checkoutStatus,
14+
15+
cartProducts: (state, getters, rootState) => {
16+
return state.added.map(({ id, quantity }) => {
17+
const product = rootState.products.all.find(product => product.id === id)
18+
return {
19+
title: product.title,
20+
price: product.price,
21+
quantity
22+
}
23+
})
24+
},
25+
26+
cartTotalPrice: (state, getters) => {
27+
return getters.cartProducts.reduce((total, product) => {
28+
return total + product.price * product.quantity
29+
}, 0)
30+
}
1431
}
1532

1633
// actions
1734
const actions = {
1835
checkout ({ commit, state }, products) {
1936
const savedCartItems = [...state.added]
20-
commit(types.CHECKOUT_REQUEST)
37+
commit(types.SET_CHECKOUT_STATUS, null)
38+
// empty cart
39+
commit(types.SET_CART_ITEMS, { items: [] })
2140
shop.buyProducts(
2241
products,
23-
() => commit(types.CHECKOUT_SUCCESS),
24-
() => commit(types.CHECKOUT_FAILURE, { savedCartItems })
42+
() => commit(types.SET_CHECKOUT_STATUS, 'successful'),
43+
() => {
44+
commit(types.SET_CHECKOUT_STATUS, 'failed')
45+
// rollback to the cart saved before sending the request
46+
commit(types.SET_CART_ITEMS, { items: savedCartItems })
47+
}
2548
)
2649
}
2750
}
@@ -30,7 +53,7 @@ const actions = {
3053
const mutations = {
3154
[types.ADD_TO_CART] (state, { id }) {
3255
state.checkoutStatus = null
33-
const record = state.added.find(p => p.id === id)
56+
const record = state.added.find(product => product.id === id)
3457
if (!record) {
3558
state.added.push({
3659
id,
@@ -41,20 +64,12 @@ const mutations = {
4164
}
4265
},
4366

44-
[types.CHECKOUT_REQUEST] (state) {
45-
// clear cart
46-
state.added = []
47-
state.checkoutStatus = null
48-
},
49-
50-
[types.CHECKOUT_SUCCESS] (state) {
51-
state.checkoutStatus = 'successful'
67+
[types.SET_CART_ITEMS] (state, { items }) {
68+
state.added = items
5269
},
5370

54-
[types.CHECKOUT_FAILURE] (state, { savedCartItems }) {
55-
// rollback to the cart saved before sending the request
56-
state.added = savedCartItems
57-
state.checkoutStatus = 'failed'
71+
[types.SET_CHECKOUT_STATUS] (state, status) {
72+
state.checkoutStatus = status
5873
}
5974
}
6075

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const mutations = {
2727
},
2828

2929
[types.ADD_TO_CART] (state, { id }) {
30-
state.all.find(p => p.id === id).inventory--
30+
state.all.find(product => product.id === id).inventory--
3131
}
3232
}
3333

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export const ADD_TO_CART = 'ADD_TO_CART'
2-
export const CHECKOUT_REQUEST = 'CHECKOUT_REQUEST'
3-
export const CHECKOUT_SUCCESS = 'CHECKOUT_SUCCESS'
4-
export const CHECKOUT_FAILURE = 'CHECKOUT_FAILURE'
2+
export const SET_CART_ITEMS = 'SET_CART_ITEMS'
3+
export const SET_CHECKOUT_STATUS = 'SET_CHECKOUT_STATUS'
54
export const RECEIVE_PRODUCTS = 'RECEIVE_PRODUCTS'

0 commit comments

Comments
 (0)