-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
228 lines (206 loc) · 7.15 KB
/
index.ts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const cartMenuWrapper = document.querySelector('.shop-cart');
const cartMenuBtn = document.querySelector('.cart-button');
const cartMenu = document.querySelector('.shop-cart__container');
const closeBtn = document.querySelector('.close');
const productsDom = document.querySelector('.products__container')
const headerTotal = document.querySelector('.header-count');
const totalPrice = document.querySelector('.total-price');
const menuCartList = document.querySelector('.shop-cart__list');
const clearCartBtn = document.querySelector('.btn--total');
cartMenuBtn.addEventListener('click', () => {
cartMenuWrapper.style.display = 'flex';
cartMenu.style.transform = "translateX(0%)"
document.body.style.overflow = "hidden";
})
closeBtn.addEventListener('click', () => {
cartMenuWrapper.style.display = 'none';
cartMenu.style.transform = "translateX(101%)"
document.body.style.overflowY = "scroll";
})
let cart = [];
class Products {
async getProducts() {
try {
let result = await require('./products.json');
let data = result.items;
data = data.map(item => {
const {
title,
price
} = item.fields;
const {
id
} = item.sys;
const image = item.fields.image.fields.file.url;
return {
title,
price,
id,
image
};
})
return data;
} catch (err) {
console.log(err);
}
}
}
class UI {
showProducts(data) {
let resultItem = " ";
data.forEach(item => {
resultItem += `
<article class="products__item">
<div class="products__image">
<img src=${item.image} alt="products-image">
<button class="buy-btn" data-id=${item.id}> Add to cart</button>
</div>
<div class="products__title">
<h2>${item.title}</h2>
</div>
<div class="products__price">
${item.price}
</div>
</article>
`;
})
productsDom.innerHTML = resultItem;
}
getButtons() {
const buttonsBuy = [...document.querySelectorAll(".buy-btn")]
buttonsBuy.forEach(button => {
let id = button.dataset.id;
let inCart = cart.find(item => item.id === id);
if (inCart) {
button.innerHTML = " In cart"
button.disabled = true;
}
button.addEventListener('click', (e) => {
e.target.disabled = true;
e.target.innerHTML = " In cart"
let cartItem = {
...Storage.getFromStorage(id),
amount: 1
};
cart = [...cart, cartItem];
Storage.saveCart(cart);
this.setCartTotal(cart)
this.addToCart(cartItem);
})
})
}
setCartTotal(cart) {
let tempTotal = 0;
let itemsTotal = 0;
cart.map(item => {
tempTotal += item.price * item.amount;
itemsTotal += item.amount;
})
totalPrice.innerText = parseFloat(tempTotal.toFixed(2));
headerTotal.innerText = itemsTotal;
}
addToCart(item) {
let listItem = document.createElement('li');
listItem.innerHTML = `
<img src=${item.image} alt="small-img">
<div class="wrap">
<h4>${item.title}</h4>
<h5>$${item.price}</h5>
<p class="remove" data-id=${item.id}>remove</p>
</div>
<div class="count" >
<i class="fa fa-angle-up" aria-hidden="true" data-id=${item.id}></i>
<p class="item-amount" >${item.amount}</p>
<i class="fa fa-angle-down" aria-hidden="true" data-id=${item.id} ></i>
</div>
`
menuCartList.appendChild(listItem);
}
setupApp() {
cart = Storage.getCart();
this.setCartTotal(cart);
this.populateCart(cart);
}
populateCart(cart) {
cart.forEach(item => this.addToCart(item));
}
cartLogic() {
clearCartBtn.addEventListener('click', () => {
this.clearCart();
})
menuCartList.addEventListener('click', e => {
if (e.target.classList.contains('remove')) {
let removesItem = e.target;
let id = removesItem.dataset.id;
this.removeItem(id);
menuCartList.removeChild(e.target.parentElement.parentElement)
} else if (e.target.classList.contains('fa-angle-up')) {
let id = e.target.dataset.id;
let tempAmount = cart.find(item => item.id === id)
tempAmount.amount = tempAmount.amount + 1;
Storage.saveCart(cart);
this.setCartTotal(cart);
e.target.nextElementSibling.innerText = tempAmount.amount
} else if (e.target.classList.contains('fa-angle-down')) {
let id = e.target.dataset.id;
let lowerAmount = cart.find(item => item.id === id)
lowerAmount.amount = lowerAmount.amount - 1;
if (lowerAmount.amount > 0) {
Storage.saveCart(cart);
this.setCartTotal(cart);
e.target.previousElementSibling.innerText = lowerAmount.amount
} else {
this.removeItem(id);
menuCartList.removeChild(e.target.parentElement.parentElement)
}
}
})
}
clearCart() {
let cartIds = cart.map(item => item.id)
cartIds.forEach(id => this.removeItem(id));
while (menuCartList.children.length > 0) {
menuCartList.removeChild(menuCartList.children[0])
}
}
removeItem(id) {
cart = cart.filter(item => item.id !== id);
this.setCartTotal(cart);
Storage.saveCart(cart);
let btn = this.getSingleBtn(id);
btn.disabled = false;
btn.innerHTML = ` Add to cart`;
}
getSingleBtn(id) {
return [...document.querySelectorAll(".buy-btn")].find(button => button.dataset.id === id)
}
}
class Storage {
static saveToStorage(data) {
localStorage.setItem("products", JSON.stringify(data))
}
static getFromStorage(id) {
let product = JSON.parse(localStorage.getItem("products"));
return product.find(item => item.id === id)
}
static saveCart(cart) {
localStorage.setItem('cart', JSON.stringify(cart));
}
static getCart() {
return localStorage.getItem('cart') ?
JSON.parse(localStorage.getItem('cart')) : []
}
}
document.addEventListener('DOMContentLoaded', () => {
let products = new Products;
let layout = new UI;
layout.setupApp();
products.getProducts().then(product => {
layout.showProducts(product)
Storage.saveToStorage(product);
})
.then(() => {
layout.getButtons();
})
layout.cartLogic();
})