-
Notifications
You must be signed in to change notification settings - Fork 1
/
09_actions_and_events_2.html
122 lines (117 loc) · 3.96 KB
/
09_actions_and_events_2.html
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vue Basics</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" href="./styles/cart-page.css">
</head>
<body>
<div id="app">
<div class="mt-3 pr-5 pl-5">
<div class="card mt-5">
<h3 class="card-title card-header bg-info text-light text-center">
09 - Events part 2 - Shopping Cart Example
</h3>
<div class="card-body">
<div class="row">
<!--product list-->
<div class="col-sm-7">
<product v-for="product in products"
:key="product.id"
:product="product"
:quantity-left="quantityLeft(product.id)"
@show-modal="modalImage = product.largeImage"
@hide-modal="modalImage = null"
@add-to-cart="addToCart(product)"
></product>
</div>
<!--shopping cart-->
<div class="col-sm-5">
<shopping-cart :items="cart.items"
@remove-from-cart="(id) => removeFromCart(id)"
@update-quantity="(newQuantity, id) => updateQuantity(newQuantity, id)"
></shopping-cart>
</div>
</div>
</div>
</div>
</div>
<div class="custom-modal" v-if="modalImage">
<img :src="modalImage" width="500" height="500">
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.8/vue.js"></script>
<script src="data/products.js"></script>
<script src="../Components/ProductTwo.js"></script>
<script src="../Components/ShoppingCart.js"></script>
<script>
new Vue({
el: '#app',
methods: {
addToCart: function (product) {
if (!this.findCartItemById(product.id)) {
const item = {
id: product.id,
quantity: 1,
name: product.name,
price: product.price
};
this.cart.items.push(item)
} else {
this.cart.items.forEach(function (item) {
if (item.id === product.id) {
item.quantity = item.quantity + 1
}
})
}
},
removeFromCart: function (id) {
this.cart.items.splice(this.findCartItemById(id).index, 1);
},
updateQuantity: function (newQuantity, id) {
const quantity = parseInt(newQuantity);
const cartItem = this.findCartItemById(id);
const product = this.findProductById(id);
cartItem.quantity = quantity;
product.quantityInStock = cartItem.quantity + product.quantityInStock - quantity;
},
quantityLeft: function (id) {
if (!this.findCartItemById(id)) {
return this.findProductById(id).quantityInStock
}
return this.findProductById(id).quantityInStock - this.findCartItemById(id).quantity
},
findProductById(id) {
for (let i = 0; i < this.products.length; i++) {
if (this.products[i].id === id) {
return this.products[i];
}
}
},
findCartItemById(id) {
for (let i = 0; i < this.cart.items.length; i++) {
if (this.cart.items[i].id === id) {
this.cart.items[i].index = i;
return this.cart.items[i];
}
}
return null
}
},
data: {
products: products,
cart: {
items: []
},
modalImage: null
},
components: {
'product': ProductTwo,
'shopping-cart': ShoppingCart
}
})
</script>
</body>
</html>