-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoppingCartState.js
38 lines (37 loc) · 997 Bytes
/
shoppingCartState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// shopping cart state
export default {
data: {
cart: [
]
},
add (product) {
var found = _.find(this.data.cart, ['id', product.id])
if(typeof found != 'object') {
this.data.cart.push({
id: product.id,
title: product.title,
price: product.price,
image: product.image,
qty: 1
})
}
},
inc (product) {
var found = _.find(this.data.cart, ['id', product.id])
if(typeof found == 'object') {
var index = _.indexOf(this.data.cart, found)
this.data.cart[index].qty++
}
},
dec (product) {
var found = _.find(this.data.cart, ['id', product.id])
if(typeof found == 'object') {
var index = _.indexOf(this.data.cart, found)
if(this.data.cart[index].qty == 1) {
this.data.cart.splice(index, 1)
} else {
this.data.cart[index].qty--
}
}
}
}