Repository navigation
Expand file tree
/
Copy pathshop.html
More file actions
271 lines (223 loc) · 8.78 KB
/
Copy pathshop.html
File metadata and controls
271 lines (223 loc) · 8.78 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>E-commerce System</title>
<link rel="stylesheet" href="styles.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<header>
<nav>
<!-- LEFT: Logo -->
<div class="nav-left">
<svg xmlns="http://www.w3.org/2000/svg" width="26" height="26" fill="currentColor"
class="bi bi-cart-check" viewBox="0 0 16 16">
<path
d="M11.354 6.354a.5.5 0 0 0-.708-.708L8 8.293 6.854 7.146a.5.5 0 1 0-.708.708l1.5 1.5a.5.5 0 0 0 .708 0z" />
<path
d="M.5 1a.5.5 0 0 0 0 1h1.11l.401 1.607 1.498 7.985A.5.5 0 0 0 4 12h1a2 2 0 1 0 0 4 2 2 0 0 0 0-4h7a2 2 0 1 0 0 4 2 2 0 0 0 0-4h1a.5.5 0 0 0 .491-.408l1.5-8A.5.5 0 0 0 14.5 3H2.89l-.405-1.621A.5.5 0 0 0 2 1z" />
</svg>
<span class="logo-text">Cartify</span>
</div>
<!-- CENTER: Links -->
<div class="nav-center">
<a href="index.html">Home</a>
<a href="shop.html">Shop</a>
<a href="wishlist.html">Wishlist</a>
<a href="cart.html">Cart</a>
</div>
<!-- RIGHT: Search -->
<div class="nav-right">
<input type="text" placeholder="Search products...">
</div>
</nav>
</header>
<hr class="divider" style="
border: none;
height: 2px;
padding-bottom: 10px;
background-color: aliceblue;
margin: 0;
">
<section class="shop">
<div class="shop-container"> <!-- NEW WRAPPER -->
<div class="sidebar">
<div class="search" style="background-color:#F3F4FA;">
<input type="text" placeholder="Search products...">
</div>
<div class="filter">
<h3>Category</h3>
<label>
<input type="checkbox" name="category" value="electronics"> Electronics
</label><br>
<label>
<input type="checkbox" name="category" value="jewelery"> Jewellery
</label><br>
<label>
<input type="checkbox" name="category" value="men's clothing"> Men's Clothing
</label><br>
<label>
<input type="checkbox" name="category" value="women's clothing"> Women's Clothing
</label><br>
<label>
<input type="checkbox" name="category" value="smartphones"> Smartphones
</label><br>
<label>
<input type="checkbox" name="category" value="beauty-products"> Beauty Products
</label><br>
</div>
<div class="price-filter">
<h3>Price Range</h3>
<input type="range" id="priceRange" min="0" max="20000" value="20000">
<p>Max Price: ₹<span id="priceValue">500</span></p>
</div>
</div>
<div class="products"> <!-- WRAP PRODUCTS -->
<h1>Featured Product</h1>
<div class="featured-product" id="product-list">
<p id="loading">Loading products...</p>
</div>
</section>
<!-- <script src="script.js"></script> -->
<script>
const productList = document.getElementById("product-list");
const loading = document.getElementById("loading");
const searchInput = document.querySelector("input[type='text']");
const priceRange = document.getElementById("priceRange");
const priceValue = document.getElementById("priceValue");
const categoryInputs = document.querySelectorAll("input[name='category']");
let allProducts = [];
let cart = JSON.parse(localStorage.getItem("cart")) || [];
// ✅ FETCH PRODUCTS (BOTH APIs)
async function fetchProducts() {
try {
const [dummyRes, fakeRes] = await Promise.all([
fetch("https://dummyjson.com/products"),
fetch("https://fakestoreapi.com/products")
]);
const dummyData = await dummyRes.json();
const fakeData = await fakeRes.json();
// ✅ Normalize DummyJSON
const dummyProducts = dummyData.products.map(p => ({
id: "d-" + p.id,
title: p.title,
price: p.price,
image: p.thumbnail,
category: p.category
}));
// ✅ Normalize FakeStore
const fakeProducts = fakeData.map(p => ({
id: "f-" + p.id,
title: p.title,
price: p.price,
image: p.image,
category: p.category
}));
// ✅ FIX: assign to global variable
allProducts = [...dummyProducts, ...fakeProducts];
displayProducts(allProducts);
loading.style.display = "none";
} catch (error) {
productList.innerHTML = "<p>Error loading products</p>";
}
}
// ✅ DISPLAY PRODUCTS
function displayProducts(products) {
productList.innerHTML = "";
products.forEach(product => {
const card = document.createElement("div");
card.classList.add("product-card");
card.innerHTML = `
<img src="${product.image}" style="width:150px;height:150px;object-fit:contain;">
<h3>${product.title.substring(0, 25)}...</h3>
<p>₹${Math.round(product.price * 80)}</p>
<button class="cart-btn" style="padding: 3px ">Add To Cart</button>
<button class="wishlist-btn" style="padding: 3px">❤️ Wishlist</button>
`;
// Cart button
card.querySelector(".cart-btn").addEventListener("click", () => {
addToCart(product);
});
// Wishlist button
card.querySelector(".wishlist-btn").addEventListener("click", () => {
addToWishlist(product);
});
productList.appendChild(card);
});
}
// ✅ ADD TO CART (no duplicates)
function addToCart(product) {
if (!cart.find(item => item.id === product.id)) {
cart.push(product);
localStorage.setItem("cart", JSON.stringify(cart));
showToast("Added to cart!");
} else {
showToast("Already in cart!");
}
}
// ✅ ADD TO WISHLIST (no duplicates)
function addToWishlist(product) {
let wishlist = JSON.parse(localStorage.getItem("wishlist")) || [];
if (!wishlist.find(item => item.id === product.id)) {
wishlist.push(product);
localStorage.setItem("wishlist", JSON.stringify(wishlist));
showToast("Added to wishlist ❤️");
} else {
showToast("Already in wishlist!");
}
}
// ✅ FILTER EVENTS
searchInput.addEventListener("input", filterProducts);
priceRange.addEventListener("input", () => {
priceValue.innerText = priceRange.value;
filterProducts();
});
categoryInputs.forEach(input => {
input.addEventListener("change", filterProducts);
});
// ✅ FILTER LOGIC
function filterProducts() {
let filtered = [...allProducts];
const searchText = searchInput.value.toLowerCase();
// 🔍 Search
filtered = filtered.filter(p =>
p.title.toLowerCase().includes(searchText)
);
// 💰 Price
filtered = filtered.filter(p =>
(p.price * 80) <= priceRange.value
);
// 🏷️ Category
const selectedCategories = Array.from(categoryInputs)
.filter(i => i.checked)
.map(i => i.value);
if (selectedCategories.length > 0) {
filtered = filtered.filter(p =>
selectedCategories.includes(p.category)
);
}
displayProducts(filtered);
}
// ✅ TOAST
function showToast(msg) {
const toast = document.createElement("div");
toast.innerText = msg;
toast.style.position = "fixed";
toast.style.bottom = "20px";
toast.style.right = "20px";
toast.style.background = "#10B981";
toast.style.color = "white";
toast.style.padding = "10px";
toast.style.borderRadius = "5px";
document.body.appendChild(toast);
setTimeout(() => toast.remove(), 2000);
}
// ✅ START
fetchProducts();
</script>
// <!-- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script> -->
</body>
</html>