-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
234 lines (216 loc) · 7.27 KB
/
app.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
import { productsData } from "./products.js";
const cartBtn = document.querySelector(".cart-btn");
const cartModal = document.querySelector(".cart");
const backDrop = document.querySelector(".backdrop");
const closeModal = document.querySelector(".cart-item-confirm");
const cartItems = document.querySelector(".cart-items");
const cartTotal = document.querySelector(".cart-total");
const cartContent = document.querySelector(".cart-content");
const productsDOM = document.querySelector(".products-center");
const clearCartBtn = document.querySelector(".clear-cart");
// products == main section for present products
// cart == modal of cart
let cart = [];
//1.get products from producs.js
const buttonsDOM = [];
class Products{
getProduct(){
return productsData;
}
}
let buttsDOM = [];
//2.display products
class UI{
displayProducts(products){
let result = "";
products.forEach(item => {
result +=`<div class="product">
<div class="img-container">
<img src="${item.imageUrl}" class="product-img"/>
</div>
<div class="product-desc">
<p class="product-price">${item.price}</p>
<p class="product-title">${item.title}</p>
</div>
<button class="btn add-to-cart" data-id=${item.id}>
<i class="fas fa-shopping-cart"></i>
add to cart
</button>
</div>`;
productsDOM.innerHTML = result;
});
}
getAddToCartBtns(){
const addToCartBtns = [...document.querySelectorAll(".add-to-cart")];
buttsDOM = addToCartBtns;
addToCartBtns.forEach((btn) =>{
const id = btn.dataset.id;
//check this product id is in cart or not
const isInCart = cart.find((p) => p.id === id);
if (isInCart) {
btn.innerText = "In Cart";
btn.disabled = true;
}
btn.addEventListener("click", (event)=>{
event.target.innerText = "In Cart";
event.target.disabled = true;
//get product from products:
const addedProduct = {...Storage.getProduct(id) , quantity: 1};
//add to cart
cart = [...cart , addedProduct];
//save cart to local storage
Storage.saveCart(cart);
//update cart value
this.setCartValue(cart);
//add to cart item
this.addCartItem(addedProduct);
//get cart value from local storage
});
});
}
setCartValue(cart){
//1. cart items
//2. cart total price
let tempCartItems = 0;
const totalPrice = cart.reduce((acc,curr) => {
tempCartItems += curr.quantity;
return acc + curr.quantity + curr.price;
},0);
cartTotal.innerText = `total price : ${parseFloat(totalPrice).toFixed(2)} $`;
cartItems.innerText = tempCartItems;
}
addCartItem(cartItem){
const div = document.createElement("div");
div.classList.add("cart-item");
div.innerHTML = `<img class="cart-item-img" src=${cartItem.imageUrl}>
<div class="cart-item-desc">
<h4>${cartItem.title}</h4>
<h5>$ ${cartItem.price}</h5>
</div>
<div class="cart-item-conteoller">
<i class="fas fa-chevron-up" data-id=${cartItem.id}></i>
<p>${cartItem.quantity}</p>
<i class="fas fa-chevron-down" data-id=${cartItem.id}></i>
</div>
<i class="far fa-trash-alt" data-id=${cartItem.id}></i>
`;
cartContent.appendChild(div);
}
setupApp(){
//get cart from storage
cart = Storage.getCart() || [];
// addCartItem
cart.forEach((cartItem)=>this.addCartItem(cartItem));
//setValue : price + items
this.setCartValue(cart);
}
cartLogic(){
//clear cart
clearCartBtn.addEventListener("click", () => this.clearCart());
//cart functionality (chevron-up and down)
cartContent.addEventListener("click" , (event)=>{
// console.log(event.target);
if(event.target.classList.contains("fa-chevron-up")){
//console.log(event.target.dataset.id);
const addQuantity = event.target;
//get item from cart
const addedItem = cart.find(cItem =>cItem.id == addQuantity.dataset.id );
addedItem.quantity++;
//update cart value
this.setCartValue(cart);
//save cart
Storage.saveCart(cart);
//update cart item in UI(number in modal)
addQuantity.nextElementSibling.innerText = addedItem.quantity;
}else if(event.target.classList.contains("fa-trash-alt")){
const removeItem = event.target;
const _removedItem = cart.find((c)=> c.id == removeItem.dataset.id);
this.removeItem(_removedItem.id);
Storage.saveCart(cart);
cartContent.removeChild(removeItem.parentElement);
//remove from cartitem
//call remove
}else if(event.target.classList.contains("fa-chevron-down")){
const subQuantity = event.target;
const substractedItem = cart.find((c)=> c.id == subQuantity.dataset.id);
if (substractedItem.quantity === 1){
this.removeItem(substractedItem.id);
cartContent.removeChild(subQuantity.parentElement.parentElement);
return;
}
substractedItem.quantity--;
this.setCartValue(cart);
//save cart
Storage.saveCart(cart);
//update cart item in UI(number in modal)
subQuantity.previousElementSibling.innerText = substractedItem.quantity;
}
});
}
clearCart(){
//remove :
cart.forEach((cItem) => this.removeItem(cItem.id));
//remove cart content children :
//console.log(cartContent.children);
while(cartContent.children.length){
cartContent.removeChild(cartContent.children[0]);
}
closeModalFunction();
}
removeItem(id) {
//update cart
cart = cart.filter((cItem) => cItem.id !== id);
//total price and cart items
this.setCartValue(cart);
//update storage :
Storage.saveCart(cart);
//get add to cart btns => update text and disabled
this.getSingleButton(id);
}
getSingleButton(id) {
const button = buttsDOM.find( btn => btn.dataset.id == id);
button.innerText = "add to cart";
buttsDOM.disabled = false;
}
}
//3.storage
class Storage{
static saveProducts(products){
localStorage.setItem("products", JSON.stringify(products));
}
static getProduct(id){
const _products = JSON.parse(localStorage.getItem("products"));
return _products.find((p) => p.id === parseInt(id));
}
static saveCart(cart){
localStorage.setItem("cart", JSON.stringify(cart));
}
static getCart(){
return JSON.parse(localStorage.getItem("cart"));
}
}
document.addEventListener("DOMContentLoaded",()=>{
const products = new Products();
const productsData = products.getProduct();
//set up : get cart and set up app
const ui = new UI();
ui.setupApp();
ui.displayProducts(productsData);
ui.getAddToCartBtns();
Storage.saveProducts(productsData);
ui.cartLogic();
})
//////////////////////////////////////////
function showModalFunction() {
backDrop.style.display = "block";
cartModal.style.opacity = "1";
cartModal.style.top = "20%";
}
function closeModalFunction() {
backDrop.style.display = "none";
cartModal.style.opacity = "0";
cartModal.style.top = "-100%";
}
cartBtn.addEventListener("click", showModalFunction);
closeModal.addEventListener("click", closeModalFunction);
backDrop.addEventListener("click", closeModalFunction);