-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
263 lines (231 loc) · 8.06 KB
/
script.js
File metadata and controls
263 lines (231 loc) · 8.06 KB
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
256
257
258
259
260
261
262
263
const bar = document.getElementById('bar');
const close1 = document.getElementById('close');
const nav = document.getElementById('navbar');
if (bar) {
bar.addEventListener('click', () => {
nav.classList.add('active');
})
}
if (close1) {
close1.addEventListener('click', () => {
nav.classList.remove('active');
})
}
// section for cart Test ...........................................................................................................
let iconCart = document.querySelector('.iconCart');
let cart = document.querySelector('.cartTest');
let container = document.querySelector('.containerTest');
let close = document.querySelector('.close');
iconCart.addEventListener('click', function(){
if(cart.style.right == '-100%'){
cart.style.right = '0';
container.style.transform = 'translateX(-400px)';
}else{
cart.style.right = '-100%';
container.style.transform = 'translateX(0)';
}
})
/* close BUTTON */
close.addEventListener('click', function (){
cart.style.right = '-100%';
container.style.transform = 'translateX(0)';
})
/*
let products = null;
// get data from file json
fetch('product.json')
.then(response => response.json())
.then(data => {
products = data;
addDataToHTML();
})
//show dates in list html
function addDataToHTML(){
// remove datas default from HTML
let listProductHTML = document.querySelector('.listProduct');
listProductHTML.innerHTML = '';
// add new datas
if(products != null) // if has data
{
products.forEach(product => {
let newProduct = document.createElement('div');
newProduct.classList.add('item');
newProduct.innerHTML =
`<img src="${product.image}" alt="">
<h2>${product.name}</h2>
<div class="price">$${product.price}</div>
<button onclick="addCart(${product.id})">Add To Cart</button>`;
listProductHTML.appendChild(newProduct);
});
}
}
let listcart = [];
// get cookie data cart
function checkCart() {
let cookieValue = document.cookie
.split('; ')
.find(row => row.startsWith('listCart='));
if(cookieValue){
listCart = JSON.parse(cookieValue.split('=')[1]);
}
}
checkCart()
function addCart($idProduct){
let productCopy = JSON.parse(JSON.stringify(products));
//if this product is not in the cart
if(!listCart[$idProduct]){
let dataProduct = productCopy.filter(
product => product.id == $idProduct
)[0];
//add data product in cart
listCart[$idProduct] = dataProduct;
listcart[$idProduct].quantity = 1;
} else {
// if yhis product is alread in the cart
// just will increased the quantity
listCart[$idProduct].quantity++;
}
// this will save datas cart in cookie
// to save this datas cart when we turn of the computer
let timeSave = "expires=Thu, 31 Dec 2025 23:59:59 UTC";
document.cookie = "listCart="+JSON.stringify(listCart)+"; "+timeSave+"; path=/;";
addCartToHTML();
}
addCartToHTML();
function addCartToHTML() {
//clear data default;
let listCartHTML = document.querySelector('.listCart');
listCartHTML.innerHTML = '';
let totalHTML = document.querySelector('totalQuantity');
let toalQuantity = 0;
if(listCart){
listCart.forEach(product => {
if(product){
let newCart = document.createElement('div');
newCart.classList.add('item');
newCart.innerHTML =
`<img src="${product.image}">
<div class="content">
<div class="name">${product.name}</div>
<div class="price">$${product.price}/ 1 product</div>
</div>
<div class="quantity">
<button>-</button>
<span class="value">${product.quantity}</span>
<button>+</button>
</div>`;
listCartHTML.appendChild(newCart);
toalQuantity = toalQuantity + product.quantity;
}
})
}
totalHTML.innerText = toalQuantity;
}
*/
let products = null;
// get data from file json
fetch('product.json')
.then(response => response.json())
.then(data => {
products = data;
addDataToHTML();
})
//show datas product in list
function addDataToHTML(){
// remove datas default from HTML
let listProductHTML = document.querySelector('.listProduct');
listProductHTML.innerHTML = '';
// add new datas
if(products != null) // if has data
{
products.forEach(product => {
let newProduct = document.createElement('div');
newProduct.classList.add('item');
newProduct.innerHTML =
`<img src="${product.image}" alt="">
<h2>${product.name}</h2>
<div class="price">$${product.price}</div>
<button onclick="addCart(${product.id})">Add To Cart</button>`;
listProductHTML.appendChild(newProduct);
});
}
}
//use cookie so the cart doesn't get lost on refresh page
let listCart = [];
function checkCart(){
var cookieValue = document.cookie
.split('; ')
.find(row => row.startsWith('listCart='));
if(cookieValue){
listCart = JSON.parse(cookieValue.split('=')[1]);
}else{
listCart = [];
}
}
checkCart();
function addCart($idProduct){
let productsCopy = JSON.parse(JSON.stringify(products));
//// If this product is not in the cart
if(!listCart[$idProduct])
{
listCart[$idProduct] = productsCopy.filter(product => product.id == $idProduct)[0];
listCart[$idProduct].quantity = 1;
}else{
//If this product is already in the cart.
//I just increased the quantity
listCart[$idProduct].quantity++;
}
document.cookie = "listCart=" + JSON.stringify(listCart) + "; expires=Thu, 31 Dec 2025 23:59:59 UTC; path=/;";
addCartToHTML();
}
addCartToHTML();
function addCartToHTML(){
// clear data default
let listCartHTML = document.querySelector('.listCart');
listCartHTML.innerHTML = '';
let totalHTML = document.querySelector('.totalQuantity');
let totalQuantity = 0;
// if has product in Cart
if(listCart){
listCart.forEach(product => {
if(product){
let newCart = document.createElement('div');
newCart.classList.add('item');
newCart.innerHTML =
`<img src="${product.image}">
<div class="content">
<div class="name">${product.name}</div>
<div class="price">$${product.price} / 1 product</div>
</div>
<div class="quantity">
<button onclick="changeQuantity(${product.id}, '-')">-</button>
<span class="value">${product.quantity}</span>
<button onclick="changeQuantity(${product.id}, '+')">+</button>
</div>`;
listCartHTML.appendChild(newCart);
totalQuantity = totalQuantity + product.quantity;
}
})
}
totalHTML.innerText = totalQuantity;
}
function changeQuantity($idProduct, $type){
switch ($type) {
case '+':
listCart[$idProduct].quantity++;
break;
case '-':
listCart[$idProduct].quantity--;
// if quantity <= 0 then remove product in cart
if(listCart[$idProduct].quantity <= 0){
delete listCart[$idProduct];
}
break;
default:
break;
}
// save new data in cookie
document.cookie = "listCart=" + JSON.stringify(listCart) + "; expires=Thu, 31 Dec 2025 23:59:59 UTC; path=/;";
// reload html view cart
addCartToHTML();
}