-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
344 lines (306 loc) · 9.53 KB
/
script.js
File metadata and controls
344 lines (306 loc) · 9.53 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
const ITEMS_PER_PAGE = 6
let currentPage = 1
let currentView = "grid"
let searchTerm = ""
let editingId = null
const products = [
{
id: 1,
name: "Wireless Headphones",
price: 4499,
category: "Electronics",
stock: 15,
description: "High-quality wireless headphones with noise cancellation",
image: "/wireless-headphones.png",
},
{
id: 2,
name: "USB-C Cable",
price: 649,
category: "Accessories",
stock: 50,
description: "Durable 2-meter USB-C charging cable",
image: "/usb-c-cable.jpg",
},
{
id: 3,
name: "Laptop Stand",
price: 1749,
category: "Office",
stock: 8,
description: "Adjustable aluminum laptop stand",
image: "/laptop-stand.png",
},
{
id: 4,
name: "Mechanical Keyboard",
price: 5999,
category: "Electronics",
stock: 12,
description: "RGB mechanical keyboard with blue switches",
image: "/mechanical-keyboard.png",
},
{
id: 5,
name: "Wireless Mouse",
price: 1299,
category: "Electronics",
stock: 25,
description: "Ergonomic wireless mouse with 2.4GHz connectivity",
image: "/wireless-mouse.png",
},
{
id: 6,
name: "Monitor Arm",
price: 2899,
category: "Office",
stock: 10,
description: "Full motion monitor arm with VESA mount",
image: "/monitor-arm.jpg",
},
{
id: 7,
name: "Desk Lamp",
price: 1599,
category: "Office",
stock: 20,
description: "LED desk lamp with adjustable brightness",
image: "/modern-desk-lamp.png",
},
{
id: 8,
name: "Phone Stand",
price: 599,
category: "Accessories",
stock: 40,
description: "Universal phone stand for desk",
image: "/phone-stand.jpg",
},
{
id: 9,
name: "Webcam HD",
price: 2999,
category: "Electronics",
stock: 18,
description: "1080p HD webcam with built-in microphone",
image: "/classic-webcam.png",
},
{
id: 10,
name: "Desk Organizer",
price: 999,
category: "Office",
stock: 35,
description: "Multi-compartment desk organizer",
image: "/desk-organizer.png",
},
{
id: 11,
name: "Portable Charger",
price: 1899,
category: "Accessories",
stock: 22,
description: "20000mAh portable power bank",
image: "/portable-charger-lifestyle.png",
},
{
id: 12,
name: "Bluetooth Speaker",
price: 2499,
category: "Electronics",
stock: 14,
description: "Portable Bluetooth speaker with deep bass",
image: "/bluetooth-speaker.jpg",
},
]
const modal = document.getElementById("modal")
const addBtn = document.getElementById("addBtn")
const cancelBtn = document.getElementById("cancelBtn")
const modalClose = document.querySelector(".modal-close")
const productForm = document.getElementById("productForm")
const searchInput = document.getElementById("searchInput")
const productContainer = document.getElementById("productContainer")
const toggleBtns = document.querySelectorAll(".toggle-btn")
// Modal handlers
addBtn.addEventListener("click", openModal)
cancelBtn.addEventListener("click", closeModal)
modalClose.addEventListener("click", closeModal)
modal.addEventListener("click", (e) => {
if (e.target === modal) closeModal()
})
// Form submission
productForm.addEventListener("submit", (e) => {
e.preventDefault()
saveProduct()
})
// Search
searchInput.addEventListener("input", (e) => {
searchTerm = e.target.value.toLowerCase()
currentPage = 1
render()
})
// View toggle
toggleBtns.forEach((btn) => {
btn.addEventListener("click", () => {
toggleBtns.forEach((b) => b.classList.remove("active"))
btn.classList.add("active")
currentView = btn.dataset.view
render()
})
})
function openModal() {
editingId = null
document.getElementById("modalTitle").textContent = "Add Product"
productForm.reset()
document.getElementById("productId").value = ""
clearErrors()
modal.classList.add("open")
}
function editProduct(id) {
const product = products.find((p) => p.id === id)
if (!product) return
editingId = id
document.getElementById("modalTitle").textContent = "Edit Product"
document.getElementById("productId").value = product.id
document.getElementById("productName").value = product.name
document.getElementById("productPrice").value = product.price
document.getElementById("productCategory").value = product.category
document.getElementById("productStock").value = product.stock
document.getElementById("productDescription").value = product.description
document.getElementById("productImage").value = product.image
clearErrors()
modal.classList.add("open")
}
function closeModal() {
modal.classList.remove("open")
editingId = null
productForm.reset()
}
function validateForm() {
const name = document.getElementById("productName").value.trim()
const price = document.getElementById("productPrice").value
const category = document.getElementById("productCategory").value.trim()
let isValid = true
clearErrors()
if (!name) {
document.getElementById("nameError").textContent = "Product name is required"
isValid = false
}
if (!price || price < 0) {
document.getElementById("priceError").textContent = "Valid price is required"
isValid = false
}
if (!category) {
document.getElementById("categoryError").textContent = "Category is required"
isValid = false
}
return isValid
}
function clearErrors() {
document.getElementById("nameError").textContent = ""
document.getElementById("priceError").textContent = ""
document.getElementById("categoryError").textContent = ""
}
function saveProduct() {
if (!validateForm()) return
const id = document.getElementById("productId").value
const newProduct = {
id: id ? Number.parseInt(id) : Math.max(...products.map((p) => p.id), 0) + 1,
name: document.getElementById("productName").value.trim(),
price: Number.parseInt(document.getElementById("productPrice").value),
category: document.getElementById("productCategory").value.trim(),
stock: Number.parseInt(document.getElementById("productStock").value) || 0,
description: document.getElementById("productDescription").value.trim(),
image: document.getElementById("productImage").value.trim() || "/diverse-products-still-life.png",
}
if (id) {
const index = products.findIndex((p) => p.id === newProduct.id)
if (index !== -1) {
products[index] = newProduct
}
} else {
products.unshift(newProduct)
}
closeModal()
currentPage = 1
render()
}
function deleteProduct(id) {
if (confirm("Are you sure you want to delete this product?")) {
const index = products.findIndex((p) => p.id === id)
if (index !== -1) {
products.splice(index, 1)
render()
}
}
}
function getFilteredProducts() {
return products.filter(
(p) => p.name.toLowerCase().includes(searchTerm) || p.category.toLowerCase().includes(searchTerm),
)
}
function render() {
const filtered = getFilteredProducts()
const start = (currentPage - 1) * ITEMS_PER_PAGE
const paginated = filtered.slice(start, start + ITEMS_PER_PAGE)
productContainer.className = currentView === "list" ? "product-grid list-view" : "product-grid"
if (paginated.length === 0) {
productContainer.innerHTML = `
<div class="empty-state" style="grid-column: 1 / -1;">
<p>${searchTerm ? "No products found matching your search." : "No products yet. Add one to get started!"}</p>
</div>
`
} else {
productContainer.innerHTML = paginated
.map(
(product) => `
<div class="product-card">
${currentView === "list" ? "" : ""}
<img src="${product.image}" alt="${product.name}" class="product-image" onerror="this.src='/diverse-products-still-life.png'">
<div class="product-content">
<div class="product-info">
<h3 class="product-name">${product.name}</h3>
<span class="product-category">${product.category}</span>
${product.description ? `<p class="product-description">${product.description}</p>` : ""}
<div class="product-meta">
<span class="product-price">₹${product.price.toLocaleString("en-IN")}</span>
<span class="product-stock">Stock: ${product.stock}</span>
</div>
</div>
<div class="product-buttons">
<button class="edit-btn" onclick="editProduct(${product.id})">Edit</button>
<button class="delete-btn" onclick="deleteProduct(${product.id})">Delete</button>
</div>
</div>
</div>
`,
)
.join("")
}
renderPagination(filtered.length)
}
function renderPagination(totalItems) {
const totalPages = Math.ceil(totalItems / ITEMS_PER_PAGE)
const pagination = document.getElementById("pagination")
if (totalPages <= 1) {
pagination.innerHTML = ""
return
}
let html = ""
if (currentPage > 1) {
html += `<button class="page-btn" onclick="changePage(${currentPage - 1})">Previous</button>`
}
for (let i = 1; i <= totalPages; i++) {
html += `<button class="page-btn ${currentPage === i ? "active" : ""}" onclick="changePage(${i})">${i}</button>`
}
if (currentPage < totalPages) {
html += `<button class="page-btn" onclick="changePage(${currentPage + 1})">Next</button>`
}
pagination.innerHTML = html
}
function changePage(page) {
currentPage = page
render()
window.scrollTo({ top: 0, behavior: "smooth" })
}
render()