-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart-classes1.js
144 lines (130 loc) · 4.8 KB
/
cart-classes1.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//Tasks:
//1) Доделать кнопку удаления товара из корзины
//2) Убрать двойные записи в корзине -> вместо этого сделать прибавление количества товара Q-ty
//3) Выводить сумму товаров всей карзины!
//4) Подцепить каталог товаров из уделенного сервера от GB и добавить "Promise"
//**5) От нечего делать можно еще добавить поле для ввода количества товара на саму карточку товара!
let globalGoods = [];
class GoodsItem {
constructor(id_product = 'xx', product_name = 'Product Name', price = 'Price on request') {
this.id_product = id_product;
this.product_name = product_name;
this.price = price;
}
render() {
return `<div class="goods-item" id="${this.id_product}">
<h3 class="item-title">${this.product_name}</h3>
<div class="item-image"></div>
<p class="item-price">Цена: ${this.price}$</p>
<span class="add2cart">buy now</span>
<!-- <span class="add2cartId">#${this.id_product}</span> -->
</div>`;
}
}
class GoodsList {
constructor() {
this.goods = [];
this.localGoods = [2];
}
fetchGoods() {
this.goods = [
{
product_name: 'Shirt',
price: 160,
id_product: 0
},
{
product_name: 'Socks',
price: 100,
id_product: 1
},
{
product_name: 'Jacket',
price: 150,
id_product: 2
},
{
product_name: 'Shoe',
price: 120,
id_product: 3
},
{
product_name: 'Joe Socks',
price: 1500,
id_product: 4
},
{
product_name: 'Shoe3',
price: 65,
id_product: 5
}
];
globalGoods = globalGoods.concat(this.goods);
}
render() {
let listHtml = '';
globalGoods.forEach((good) => {
const goodItem = new GoodsItem(good.id_product, good.product_name, good.price);
listHtml += goodItem.render();
});
document.querySelector('.goods-list').innerHTML = listHtml;
}
//JS2 - HW2 Part 2: calculate the sum of all goods
sumGoods() {
let sum = 0;
globalGoods.forEach(
(product) => {
if (isNaN(product.price)) {
//won't be counted;
} else {
sum += product.price;
}
});
document.querySelector('.sum-goods').innerHTML = `Sum of all goods: <strong>${sum}</strong>`;
}
}
//создать новый элемент корзины
class Basket {
constructor() {
this.cart = [];
}
//1) Добавить каждому товару EventListener "add to cart" [JS2: 1:41:30 - 1:45:24] и clearCart
addToCartTriggers() {
let add2CartBtn = document.getElementsByClassName('add2cart');
for (let i = 0; i < add2CartBtn.length; i++) {
add2CartBtn[i].addEventListener('click', (i) => this.addToCart(i));
}
document.querySelector('.clear-cart').addEventListener('click', () => this.clearCart());
}
addToCart(e) {
let newCartItem = {};
let target = e.target;
let productIndex = target.parentNode.getAttribute("id");
globalGoods.forEach((good, index) => {
if (productIndex == good.id_product) {
newCartItem.product_name = good.product_name;
newCartItem.price = good.price;
newCartItem.id_product = good.id_product;
this.cart.push(newCartItem);
}
});
//display all items in the Cart
let cartInside = "";
this.cart.forEach((cartItem) => {
let cartInsideItem = `<div class="item-line"><span class="cart-item-title">${cartItem.product_name}</span> <span class="cart-item-price">$${cartItem.price}</span> <span class="delete-item">delete item</span></div>`;
cartInside += cartInsideItem;
});
document.querySelector('.cart-inside').innerHTML = cartInside;
}
clearCart() {
this.cart = [];
document.querySelector('.cart-inside').innerHTML = "Your cart is empty";
}
}
const list = new GoodsList();
list.fetchGoods();
list.render();
list.sumGoods();
//list.addToCartTriggers();
let basket = new Basket();
basket.addToCartTriggers();