-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
400 lines (349 loc) · 18.5 KB
/
Copy pathindex.html
File metadata and controls
400 lines (349 loc) · 18.5 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Shop</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" />
<link href="https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css" rel="stylesheet" />
<style>
/* Custom CSS for the QR Modal */
#qr-modal {
z-index: 100;
/* Ensure it's above the purchase modal */
}
/* Custom CSS for the loading */
#loading-indicator {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 1000;
/* Ensure it's on top of everything */
}
/* Custom CSS for the empty message */
#empty-message {
text-align: center;
font-size: 1.2rem;
margin-top: 20px;
color: #777;
}
/* Custom CSS for the autocomplete */
#autocomplete-suggestions {
position: absolute;
top: 100%;
left: 0;
width: 100%;
font-size: 15px;
background-color: white;
border: 1px solid #ccc;
border-top: none;
border-radius: 30px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
z-index: 10;
display: none;
max-height: 200px;
overflow-y: auto;
}
#autocomplete-suggestions div {
padding: 8px 12px;
border-radius: 50px;
cursor: pointer;
border-bottom: 1px solid #eee;
}
#autocomplete-suggestions div:hover {
background-color: #e6e6e6;
}
/* Custom scrollbar styles */
::-webkit-scrollbar {
width: 10px;
height: 10px;
}
::-webkit-scrollbar-thumb {
background-color: #ccc;
border-radius: 10px;
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}
::-webkit-scrollbar-thumb:hover {
background-color: #aaa;
}
::-webkit-scrollbar-track {
background-color: #eee;
border-radius: 10px;
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}
#autocomplete-suggestions::-webkit-scrollbar {
display: none;
}
</style>
</head>
<body class="bg-white">
<h1
class="text-4xl font-bold sticky top-0 bg-white/10 backdrop-blur-md py-4 px-6 z-10 rounded-bl-2xl rounded-br-2xl w-full text-center">
<div class="flex flex-col items-center space-y-2">
<span class="text-2xl font-normal text-gray-800">Shop for your daily needs</span>
<span class="border-b-4 border-black rounded-2xl w-12"></span>
<!-- Search Bar -->
<div class="relative w-full max-w-lg mt-2">
<input type="text" id="search-input"
class="w-full py-3 pl-4 pr-16 text-gray-700 text-lg font-normal rounded-full border border-gray-300 shadow-sm focus:outline-none focus:ring-2 focus:ring-gray-600/50 focus:border-gray-500 transition-all"
placeholder="Search products...">
<div id="autocomplete-suggestions"></div>
<button id="search-btn"
class="absolute right-1 top-1/2 transform -translate-y-1/2 bg-gray-200 px-3 py-2 rounded-full shadow-sm hover:bg-gray-300 transition">
<i class="fas fa-search text-gray-600 text-lg flex justify-center items-center"></i>
</button>
</div>
</div>
</h1>
<!-- Loading Indicator -->
<div id="loading-indicator" class="hidden">
<i class="fas fa-spinner fa-spin text-gray-600 text-5xl"></i>
</div>
<div class="container mx-auto p-4">
<!-- Empty message -->
<div id="empty-message" class="hidden">
<p>No products available.</p>
</div>
<div id="skeleton-container" class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-6">
</div>
<div id="product-container" class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-6">
<!-- Products will be dynamically inserted here -->
</div>
</div>
<!-- Purchase Modal (Hidden by Default) -->
<div id="purchase-modal"
class="hidden fixed z-10 inset-0 flex items-center justify-center overflow-y-auto bg-black/50 backdrop-blur-md transition-all">
<div class="bg-white p-6 rounded-xl shadow-lg w-[90%] md:w-[40%] relative">
<button onclick="closePurchaseModal()"
class="absolute top-3 right-3 px-3 py-2 rounded-full cursor-pointer text-gray-600 hover:text-black text-xl transition-all transform hover:rotate-180">
<i class="fas fa-times"></i>
</button>
<div id="modal-product-details" class="space-y-3">
<img id="modal-product-image" src="" class="w-2/3 rounded-xl mx-auto" />
<span>
<h2 id="modal-product-title" class="text-sm font-bold"></h2>
<p id="modal-product-price" class="text-lg font-semibold text-green-600"></p>
</span>
</div>
<div class="mt-4">
<h3 class="text-sm font-semibold mb-2">Choose Payment Method</h3>
<select id="payment-method" class="w-full p-2 border rounded-lg">
<option value="credit_card">Credit/Debit Card</option>
<option value="upi" selected>UPI</option>
<option value="wallet">Wallet</option>
</select>
</div>
<button onclick=" processPayment()"
class="w-full mt-4 bg-blue-300 text-blue-900 py-2 rounded-full font-semibold hover:bg-blue-400 transition-all">
Confirm & Pay
</button>
</div>
</div>
<!-- QR Code Modal (Hidden by Default) -->
<div id="qr-modal" class="hidden fixed inset-0 flex items-center justify-center bg-black/50 backdrop-blur-md z-20">
<div class="bg-white p-6 rounded-xl m-2 md:w-1/3 sm:w-1/2 xs:w-1/2 shadow-lg relative">
<button id="close-qr-modal"
class="absolute top-3 right-3 px-3 py-2 rounded-full cursor-pointer text-gray-600 hover:text-black text-xl transition-all transform hover:rotate-180">
<i class="fas fa-times"></i>
</button>
<img id="qr-code-image" src="" class="w-2/3 rounded-lg mx-auto" alt="QR Code" />
<p id="qr-timer" class="text-center mt-4 font-bold"></p>
</div>
</div>
<script>
const skeletonHtml = `
<div class="bg-white p-4 rounded-lg shadow-md relative skeleton">
<div class="relative group cursor-pointer animate-pulse">
<div class="absolute top-2 right-2 w-4 h-4 rounded-full skeleton"></div>
<div class="absolute top-1 right-8 text-white text-xs py-1 px-2 rounded skeleton"></div>
</div>
<div class="far fa-heart text-red-500 text-xl absolute top-4 left-4 cursor-pointer skeleton"></div>
<div class="w-full h-40 mb-4 rounded skeleton"></div>
<h2 class="text-lg font-bold h-4 rounded skeleton"></h2>
<div class="container flex justify-between items-center mt-2">
<div class="text-gray-600 text-sm h-4 rounded w-1/2 skeleton"></div>
<div class="text-gray-800 text-sm h-4 rounded w-1/2 skeleton"></div>
</div>
<div class="flex justify-between items-center mt-2">
<div>
<span class="line-through text-red-500 h-4 rounded w-1/5 skeleton"></span>
<span class="text-xl font-bold text-green-600 h-4 rounded w-1/5 skeleton"></span>
</div>
<button class="ml-auto px-6 py-1 bg-gray-700 text-white rounded-xl skeleton"></button>
</div>
</div>
`;
const skeletonContainer = document.getElementById('skeleton-container');
// Function to display skeleton
function displaySkeletons(number) {
skeletonContainer.innerHTML = '';
for (let i = 0; i < number; i++) {
skeletonContainer.innerHTML += skeletonHtml;
}
}
//displaying 6 skeleton at the beginning.
displaySkeletons(6);
const allProducts = []
async function fetchProducts() {
const productContainer = document.getElementById('product-container');
const emptyMessage = document.getElementById('empty-message');
try {
skeletonContainer.classList.remove("hidden")
productContainer.classList.add("hidden")
const response = await fetch('https://dummyjson.com/products');
const data = await response.json();
const products = data.products.slice(19, 30);
allProducts.push(...products)
if (products.length == 0) {
emptyMessage.classList.remove('hidden');
productContainer.classList.add("hidden")
} else {
emptyMessage.classList.add('hidden');
productContainer.classList.remove("hidden")
}
productContainer.innerHTML = '';
const randomBgColors = ["bg-blue-100", "bg-purple-100", "bg-pink-100", "bg-yellow-100", "bg-green-100", "bg-amber-100", "bg-red-100",
"bg-orange-100", "bg-lime-100", "bg-emerald-100", "bg-teal-100", "bg-cyan-100", "bg-sky-100", "bg-violet-100", "bg-fuchsia-100",
"bg-indigo-100", "bg-gray-100"];
products.forEach(product => {
const productCard = document.createElement('div');
const randomBg = randomBgColors[Math.floor(Math.random() * randomBgColors.length)];
productCard.className = `${randomBg} p-4 rounded-lg shadow-md relative`;
const discountedPrice = (product.price * (1 - product.discountPercentage / 100)).toFixed(2);
// Availability indicator colors
let availabilityColor = "bg-green-500";
if (product.stock <= 0) {
availabilityColor = "bg-red-500";
} else if (product.stock < 10) {
availabilityColor = "bg-yellow-500";
}
// Calculate average rating (assuming you want to calculate it)
const averageRating = product.rating || 0;
productCard.innerHTML = `
<div class="relative group cursor-pointer">
<div class="absolute top-2 right-2 w-4 h-4 rounded-full ${availabilityColor}"></div>
<div class="absolute top-1 right-8 bg-black text-white text-xs py-1 px-2 rounded opacity-0 group-hover:opacity-100 transition-opacity duration-300">
${product.stock} in stock
</div>
</div>
<i class="far fa-heart text-red-500 text-xl absolute top-4 left-4 cursor-pointer hover:scale-110 hover:animate-pulse transition-all " onclick="this.classList.toggle('fas')"></i>
<img alt="${product.title}" class="w-full h-auto mb-4 rounded" src="${product.thumbnail}" />
<h2 class="text-lg font-bold">${product.title}</h2>
<div class="container flex justify-between items-center mt-2">
<p class="text-gray-600 text-sm">${product.category}</p>
<p class="text-gray-800 text-sm"><i class="fas fa-star text-yellow-500 cursor-pointer"></i> ${averageRating.toFixed(1)} (${product.rating || 0} rating)</p>
</div>
<div class="flex justify-between items-center mt-2">
<div>
<span class="line-through text-red-500">$${product.price.toFixed(2)}</span>
<span class="text-xl font-bold text-green-600">$${discountedPrice}</span>
</div>
<button onclick="openPurchaseModal(${product.id})" class="ml-auto px-6 py-1 bg-gray-700 text-white rounded-xl transition-all hover:bg-gray-900 hover:shadow-lg hover:scale-105 hover:translate-y-1">
<i class="fas fa-shopping-cart text-xl cursor-pointer"></i>
</button>
</div>
`;
productContainer.appendChild(productCard);
});
} catch (error) {
console.error('Error fetching products:', error);
const productContainer = document.getElementById('product-container');
productContainer.innerHTML = `<p class="text-red-500">Error loading products. Please try again later.</p>`;
} finally {
skeletonContainer.classList.add("hidden");
productContainer.classList.remove("hidden")
}
}
function openPurchaseModal(productId) {
fetch(`https://dummyjson.com/products/${productId}`)
.then(response => response.json())
.then(product => {
document.getElementById('modal-product-image').src = product.thumbnail;
document.getElementById('modal-product-title').innerText = product.title;
document.getElementById('modal-product-price').innerText = `$${product.price.toFixed(2)}`;
document.getElementById('purchase-modal').classList.remove('hidden');
})
.catch(error => console.error('Error fetching product:', error));
}
function closePurchaseModal() {
document.getElementById('purchase-modal').classList.add('hidden');
closeQrModal()// Close qr model in purchase model closing
}
function closeQrModal() {
document.getElementById('qr-modal').classList.add('hidden');
}
let timerInterval;
function startQrTimer() {
let timeLeft = 120; // 2 minutes in seconds (2 * 60)
const timerDisplay = document.getElementById('qr-timer');
clearInterval(timerInterval); // Clear any existing timer
timerInterval = setInterval(() => {
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
timerDisplay.textContent = `Time left: ${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
timeLeft--;
if (timeLeft < 0) {
clearInterval(timerInterval);
timerDisplay.textContent = "QR Code Expired";
closeQrModal()//closing the qr model after timer is up.
}
}, 1000);
}
function processPayment() {
const selectedPayment = document.getElementById('payment-method').value;
const qrModal = document.getElementById('qr-modal');
const qrCodeImage = document.getElementById('qr-code-image');
const closeQrModalBtn = document.getElementById("close-qr-modal");
if (selectedPayment === "upi") {
qrCodeImage.src = "qr_white.jpeg"; // Replace with your QR code
qrModal.classList.remove("hidden");
startQrTimer(); // Start the timer
}
alert(`Pay using ${selectedPayment} within 2 minutes`);
//closing event for the close qr model button
closeQrModalBtn.addEventListener("click", closeQrModal);
}
//search
const searchInput = document.getElementById('search-input');
const autocompleteSuggestions = document.getElementById('autocomplete-suggestions');
searchInput.addEventListener('input', () => {
const searchTerm = searchInput.value.toLowerCase();
const filteredProducts = allProducts.filter(product => {
return product.title.toLowerCase().includes(searchTerm);
});
displayAutocompleteSuggestions(filteredProducts);
});
function displayAutocompleteSuggestions(suggestions) {
autocompleteSuggestions.innerHTML = ''; // Clear previous suggestions
if (suggestions.length > 0) {
autocompleteSuggestions.style.display = 'block';
suggestions.forEach(suggestion => {
const suggestionDiv = document.createElement('div');
suggestionDiv.textContent = suggestion.title;
suggestionDiv.addEventListener('click', () => {
searchInput.value = suggestion.title;
autocompleteSuggestions.style.display = 'none';
});
autocompleteSuggestions.appendChild(suggestionDiv);
});
} else {
autocompleteSuggestions.style.display = 'none';
}
}
// Hide autocomplete when clicking outside
document.addEventListener('click', (event) => {
if (!event.target.closest('.relative')) {
autocompleteSuggestions.style.display = 'none';
}
});
fetchProducts();
</script>
</body>
</html>