-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
303 lines (243 loc) · 10.1 KB
/
index.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
fetch('data.json')
.then(response => response.json())
.then(data => {
for (const item in data) {
const product = data[item];
// Create HTML elements to display product info
const productName = document.createElement('h3');
productName.innerText = product.name;
productName.classList.add('product-name');
const productDescription = document.createElement('p');
productDescription.innerText = product.description;
productDescription.classList.add('product-description');
const productImage = document.createElement('img');
productImage.src = product.image;
productImage.alt = product.name;
productImage.classList.add('product-image');
const productPrice = document.createElement('p');
productPrice.innerText = `$${product.price}`;
productPrice.classList.add('product-price');
// Append product elements to a container element
const productContainer = document.createElement('div');
productContainer.classList.add('product-container');
productContainer.appendChild(productName);
productContainer.appendChild(productDescription);
productContainer.appendChild(productImage);
productContainer.appendChild(productPrice);
// Add the product container to the page
document.body.appendChild(productContainer);
}
});
//---------------//
// const queryString = window.location.search;
// const urlParams = new URLSearchParams(queryString);
// const data = {};
// fetch('data.json')
// .then(response => response.json())
// .then(jsonData => {
// Object.assign(data, jsonData);
// let cart = {};
// // function updateCart(item, quantity) {
// // if (quantity === 0) {
// // delete cart[item];
// // } else {
// // cart[item] = { quantity, price: data[item].price };
// // }
// // localStorage.setItem('cart', JSON.stringify(cart));
// // }
// loadCart();
// function updateCart(item, quantity) {
// if (quantity === 0) {
// delete cart[item];
// } else {
// cart[item] = quantity;
// }
// localStorage.setItem('cart', JSON.stringify(cart));
// }
// // function updateCart(item, quantity) {
// // if (quantity === 0) {
// // delete cart[item];
// // } else {
// // cart[item] = { quantity, price: data[item].price };
// // }
// // localStorage.setItem('cart', JSON.stringify(cart));
// // }
// function loadCart() {
// const savedCart = localStorage.getItem('cart');
// if (savedCart) {
// cart = JSON.parse(savedCart);
// }
// }
// for (const item in data) {
// const product = data[item];
// // Create HTML elements to display product info
// const productName = document.createElement('h3');
// productName.innerText = product.name;
// productName.classList.add('product-name');
// const productDescription = document.createElement('p');
// productDescription.innerText = product.description;
// productDescription.classList.add('product-description');
// const productImage = document.createElement('img');
// productImage.src = product.image;
// productImage.alt = product.name;
// productImage.classList.add('product-image');
// const productPrice = document.createElement('p');
// productPrice.innerText = `$${product.price}`;
// productPrice.classList.add('product-price');
// const productSize = document.createElement('select');
// for (const size of product.size) {
// const option = document.createElement('option');
// option.value = size;
// option.text = size;
// productSize.appendChild(option);
// }
// const quantityLabel = document.createElement('label');
// quantityLabel.innerText = 'Quantity: ';
// const quantityInput = document.createElement('input');
// quantityInput.type = 'number';
// quantityInput.min = 0;
// quantityInput.value = cart[item] || 0;
// quantityInput.addEventListener('change', event => {
// updateCart(item, event.target.valueAsNumber);
// });
// const addToCartButton = document.createElement('button');
// addToCartButton.innerText = 'Add to Cart';
// addToCartButton.addEventListener('click', () => {
// updateCart(item, (cart[item] || 0) + quantityInput.valueAsNumber);
// });
// const productContainer = document.createElement('div');
// productContainer.classList.add('product-container');
// productContainer.appendChild(productName);
// productContainer.appendChild(productDescription);
// productContainer.appendChild(productImage);
// productContainer.appendChild(productPrice);
// productContainer.appendChild(productSize);
// productContainer.appendChild(quantityLabel);
// productContainer.appendChild(quantityInput);
// productContainer.appendChild(addToCartButton);
// document.body.appendChild(productContainer);
// }
// // Create checkout button
// const checkoutButton = document.createElement('button');
// checkoutButton.innerText = 'Checkout';
// checkoutButton.addEventListener('click', function() {
// const encodedData = encodeURIComponent(JSON.stringify(data));
// window.location.href = `checkout.html?data=${encodedData}`;
// });
// // Append button to page
// document.body.appendChild(checkoutButton);
// })
// .catch(error => {
// console.error(error);
// });
//----------
// class CartItem {
// constructor(product, quantity) {
// this.product = product;
// this.quantity = quantity;
// }
// }
// const cart = [];
// fetch('data.json')
// .then(response => response.json())
// .then(data => {
// for (const item in data) {
// const product = data[item];
// // Create HTML elements to display the product info
// const productName = document.createElement('h3');
// productName.innerText = product.name;
// productName.classList.add('product-name');
// const productDescription = document.createElement('p');
// productDescription.innerText = product.description;
// productDescription.classList.add('product-description');
// const productImage = document.createElement('img');
// productImage.src = product.image;
// productImage.alt = product.name;
// productImage.classList.add('product-image');
// const productPrice = document.createElement('p');
// productPrice.innerText = `$${product.price.toFixed(2)}`;
// productPrice.classList.add('product-price');
// const addToCartButton = document.createElement('button');
// addToCartButton.innerText = 'Add to cart';
// addToCartButton.addEventListener('click', () => addToCart(product));
// // Append product elements to a container element
// const productContainer = document.createElement('div');
// productContainer.classList.add('product-container');
// productContainer.appendChild(productName);
// productContainer.appendChild(productDescription);
// productContainer.appendChild(productImage);
// productContainer.appendChild(productPrice);
// productContainer.appendChild(addToCartButton);
// // Add the product container to the page
// document.body.appendChild(productContainer);
// }
// });
// function addToCart(product) {
// // Check if the item is already in the cart
// const itemIndex = cart.findIndex(item => item.product.name === product.name);
// if (itemIndex >= 0) {
// // If the item is already in the cart, increase the quantity by 1
// cart[itemIndex].quantity++;
// } else {
// // If the item is not in the cart, add it with a quantity of 1
// const cartItem = new CartItem(product, 1);
// cart.push(cartItem);
// }
// // Update the cart in local storage and on the page
// updateCart();
// }
// function removeFromCart(product) {
// // Find the index of the item in the cart
// const itemIndex = cart.findIndex(item => item.product.name === product.name);
// if (itemIndex >= 0) {
// // Remove the item from the cart if the quantity is 1, otherwise decrease the quantity
// if (cart[itemIndex].quantity === 1) {
// cart.splice(itemIndex, 1);
// } else {
// cart[itemIndex].quantity--;
// }
// // Update the cart display
// updateCartDisplay();
// }
// }
// function updateCartDisplay() {
// const cartElement = document.getElementById('cart');
// cartElement.innerHTML = '';
// for (const item of cart) {
// const itemName = document.createElement('span');
// itemName.innerText = item.product.name;
// const itemPrice = document.createElement('span');
// itemPrice.innerText = `$${(item.product.price * item.quantity).toFixed(2)}`;
// const itemQuantity = document.createElement('span');
// itemQuantity.innerText = `x${item.quantity}`;
// const removeButton = document.createElement('button');
// removeButton.innerText = 'Remove';
// removeButton.addEventListener('click', () => removeFromCart(item.product));
// const cartItem = document.createElement('div');
// cartItem.classList.add('cart-item');
// cartItem.appendChild(itemName);
// cartItem.appendChild(itemPrice);
// cartItem.appendChild(itemQuantity);
// cartItem.appendChild(removeButton);
// cartElement.appendChild(cartItem);
// }
// }
// function updateCart() {
// localStorage.setItem('cart', JSON.stringify(cart));
// updateCartDisplay();
// }
// function showCart() {
// // Get the cart element
// var cartElement = document.getElementById("cart");
// // Swap the contents of the cart element with the checkout form
// cartElement.innerHTML = `
// <h2>Checkout</h2>
// <form>
// <label for="name">Name:</label>
// <input type="text" id="name" name="name"><br><br>
// <label for="email">Email:</label>
// <input type="email" id="email" name="email"><br><br>
// <button type="submit">Place Order</button>
// </form>
// `;
// }