-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
255 lines (236 loc) · 8.42 KB
/
script.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Varaibles
const shoppingCart = document.querySelector(".cart");
const modalOut = document.querySelector(".modal-out");
const modal = document.querySelector(".modal");
const productsDOM = document.querySelector(".container");
const cartTotal = document.querySelector(".total-price");
const cartContainer = document.querySelector(".card-content");
const clearBtn = document.querySelector(".clear-cart");
let cart;
//Get Products
import { productsData } from './products.js';
//Classes
//Get Products
class Products {
getProduct() {
return productsData;
}
}
//Display Products
class UI {
static displayProducts(_products) {
let result = "";
_products.forEach(item => {
result += `<div class="product">
<img class="img-product" src=${item.imageUrl} alt="">
<div>
<div>
<h3>
${item.title}
</h3>
<span class="product-price">
${item.price}$
</span>
</div>
<button class="add-btn" data-id="${item.id}">
Add to Cart
</button>
</div>
</div>`
});
productsDOM.innerHTML = result;
}
static getAddToCartBtns() {
const addToCartBtns = document.querySelectorAll(".add-btn");
const buttons = [...addToCartBtns];
buttons.forEach((btn) => {
const id = Number(btn.dataset.id);
let cart = "";
let isInCart;
if (localStorage.getItem("cart") != null) {
cart = JSON.parse(localStorage.getItem('cart'));
isInCart = cart.find(product => product.id === id);
}
if (isInCart) {
btn.innerText = 'In Cart';
btn.disabled = true;
btn.style.cursor = "not-allowed";
}
btn.addEventListener("click", (e) => {
e.target.innerText = "In Cart";
e.target.disabled = true;
e.target.style.cursor = "not-allowed";
const addedProducts = { ...Storage.getProducts(e.target.dataset.id), quantity: 1 };
if (localStorage.getItem("cart") != null) {
cart = [...Storage.getCart(), addedProducts];
} else {
cart = [addedProducts];
}
Storage.saveCart(cart);
this.setCartValue(cart);
this.addCartItem(addedProducts);
})
})
}
static setCartValue(cart) {
const totalPrice = cart.reduce((acc, curr) => {
return acc + curr.quantity * curr.price;
}, 0)
cartTotal.innerText = `${totalPrice.toFixed(2)}$`;
shoppingCart.setAttribute("cart-items", cart.length);
}
static addCartItem(cartItem) {
const div = document.createElement('div');
div.classList.add("item");
div.innerHTML = `<div>
<img class="cart-item-img" src=${cartItem.imageUrl}>
</div>
<div class="cart-item-desc">
<h4>${cartItem.title}</h4>
<h5>${cartItem.price} $</h5>
</div>
<div class="cart-item-conteoller">
<img class="caret-up" data-id="${cartItem.id}" src="./icons/CaretUp.svg" alt="">
<p class="item-quantity">${cartItem.quantity}</p>
<img class="caret-down" data-id="${cartItem.id}" src="./icons/CaretDown.svg" alt="">
</div>
<img class="trash" data-id="${cartItem.id}" src="./icons/Trash.svg" alt="">`
cartContainer.appendChild(div);
}
static checkCartItem() {
cart = Storage.getCart();
if (cart == null || cart == [] || cart.length == 0) {
cartContainer.innerHTML = '';
}
}
static setupApp() {
cart = Storage.getCart();
cart.forEach(cartItem => {
this.addCartItem(cartItem);
});
this.setCartValue(cart);
}
static cartLogic() {
clearBtn.addEventListener("click", () => {
cart = Storage.getCart();
cart.forEach(cItem => this.removeItem(cItem.id));
});
cartContainer.addEventListener("click", (e) => {
cart = Storage.getCart();
let classList = e.target.classList;
if (classList.contains("caret-up")) {
const addQuantity = e.target;
const addedItem = cart.find(cItem => cItem.id === +addQuantity.dataset.id);
addedItem.quantity++;
this.setCartValue(cart);
Storage.saveCart(cart);
addQuantity.nextElementSibling.innerText = addedItem.quantity;
} else if (classList.contains("caret-down")) {
const subQuantity = e.target;
const subStarctedItem = cart.find(cItem => cItem.id === +subQuantity.dataset.id);
if (subStarctedItem.quantity > 1) {
subStarctedItem.quantity--;
this.setCartValue(cart);
Storage.saveCart(cart);
subQuantity.previousElementSibling.innerText = subStarctedItem.quantity;
}
} else {
console.log(classList);
const removeItem = e.target;
const _removedItem = cart.find(cItem => cItem.id === +removeItem.dataset.id);
console.log(_removedItem)
this.removeItem(_removedItem.id);
removeItem.parentElement.remove();
}
})
}
static removeItem(id) {
cart = Storage.getCart();
let replaceCart = cart.filter(cItem => cItem.id != id);
this.setCartValue(replaceCart);
Storage.saveCart(replaceCart);
this.checkCartItem();
this.checkBtns();
}
static checkBtns() {
const addToCartBtns = document.querySelectorAll(".add-btn");
const buttons = [...addToCartBtns];
buttons.forEach(btn => {
const id = Number(btn.dataset.id);
let cart = "";
let isInCart;
if (localStorage.getItem("cart") != null) {
cart = JSON.parse(localStorage.getItem('cart'));
isInCart = cart.find(product => product.id === id);
};
if (isInCart) {
btn.innerText = 'In Cart';
btn.disabled = true;
btn.style.cursor = "not-allowed";
} else {
btn.innerText = 'Add to Cart';
btn.disabled = false;
btn.style.cursor = "pointer";
}
})
}
// static quantityChange() {
// cart = Storage.getCart();
// let quantityItem = '';
// let replaceCart = [];
// const caretUps = document.querySelectorAll(".cart-item-conteoller img:first-child");
// const carets = [...caretUps];
// carets.forEach((btn) => {
// btn.addEventListener("click", (e) => {
// let id = e.target.dataset.id;
// cart.forEach(cItem => {
// if (cItem.id === id) {
// quantityItem = cItem;
// }
// })
// })
// });
// }
}
//Local Storage System
class Storage {
static saveProducts(_products) {
localStorage.setItem("products", JSON.stringify(_products));
}
static getProducts(id) {
const _products = JSON.parse(localStorage.getItem('products'));
let selectedProduct = "";
_products.forEach((p) => {
if (p.id === parseInt(id)) {
selectedProduct = p;
}
})
return selectedProduct;
}
static saveCart(cart) {
localStorage.setItem("cart", JSON.stringify(cart));
}
static getCart() {
return JSON.parse(localStorage.getItem('cart'));
}
}
//Functions
//Event Listerners
shoppingCart.addEventListener("click", () => {
modalOut.classList.toggle("none");
modal.classList.toggle("appear-modal");
})
modalOut.addEventListener("click", () => {
modalOut.classList.toggle("none");
modal.classList.toggle("appear-modal");
})
document.addEventListener("DOMContentLoaded", () => {
const products = new Products();
const productsData = products.getProduct();
UI.displayProducts(productsData);
UI.getAddToCartBtns();
Storage.saveProducts(productsData);
UI.setupApp();
UI.cartLogic();
// UI.quantityChange();
});