-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducts.js
More file actions
210 lines (178 loc) · 6.88 KB
/
products.js
File metadata and controls
210 lines (178 loc) · 6.88 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
// Open IndexedDB
let db;
const request = indexedDB.open("ProductDB", 1);
request.onupgradeneeded = function (event) {
let db = event.target.result;
if (!db.objectStoreNames.contains("products")) {
db.createObjectStore("products", { keyPath: "id", autoIncrement: true });
}
};
request.onsuccess = function (event) {
db = event.target.result;
loadProducts();
};
// Open product form
function openProductForm() {
document.getElementById("productForm").style.display = "block";
}
// Close product form
function closeProductForm() {
document.getElementById("productForm").style.display = "none";
}
// Save product
function saveProduct() {
const name = document.getElementById("productName").value.trim();
const category = document.getElementById("productCategory").value.trim();
const price = document.getElementById("productPrice").value.trim();
const description = document.getElementById("productDescription").value.trim();
const imageInput = document.getElementById("productImage");
let image = "";
if (imageInput.files.length > 0) {
const reader = new FileReader();
reader.onload = function (e) {
image = e.target.result;
storeProduct(name, category, price, description, image);
};
reader.readAsDataURL(imageInput.files[0]);
} else {
storeProduct(name, category, price, description, image);
}
}
// Store product in IndexedDB
function storeProduct(name, category, price, description, image) {
if (!name || !category || !price) {
alert("Name, category, and price are required.");
return;
}
const transaction = db.transaction("products", "readwrite");
const store = transaction.objectStore("products");
store.add({ name, category, price, description, image });
transaction.oncomplete = function () {
closeProductForm();
loadProducts();
};
}
// Load products
function loadProducts() {
const transaction = db.transaction("products", "readonly");
const store = transaction.objectStore("products");
const request = store.getAll();
request.onsuccess = function () {
displayProducts(request.result);
};
}
// Display products in table
function displayProducts(products) {
const tableBody = document.getElementById("productList");
tableBody.innerHTML = "";
let totalProducts = 0;
let categories = new Set();
products.forEach((product, index) => {
totalProducts++;
categories.add(product.category);
let row = `<tr>
<td>${index + 1}</td>
<td>${product.name}</td>
<td>${product.category}</td>
<td>${product.price}</td>
<td>
<button onclick="viewProduct(${product.id})">View</button>
<button onclick="editProduct(${product.id})">Edit</button>
<button onclick="deleteProduct(${product.id})">Delete</button>
</td>
</tr>`;
tableBody.innerHTML += row;
});
document.getElementById("totalProducts").textContent = totalProducts;
document.getElementById("totalCategories").textContent = categories.size;
}
// View product details
function viewProduct(id) {
const transaction = db.transaction("products", "readonly");
const store = transaction.objectStore("products");
const request = store.get(id);
request.onsuccess = function () {
const product = request.result;
if (product) {
document.getElementById("viewImage").src = product.image || "default.jpg";
document.getElementById("viewName").textContent = product.name;
document.getElementById("viewCategory").textContent = product.category;
document.getElementById("viewPrice").textContent = product.price;
document.getElementById("viewDescription").textContent = product.description;
document.getElementById("productView").style.display = "block";
}
};
}
// Close product view modal
function closeProductView() {
document.getElementById("productView").style.display = "none";
}
// Open edit product form
function editProduct(id) {
const transaction = db.transaction("products", "readonly");
const store = transaction.objectStore("products");
const request = store.get(id);
request.onsuccess = function () {
const product = request.result;
if (product) {
document.getElementById("editProductName").value = product.name;
document.getElementById("editProductCategory").value = product.category;
document.getElementById("editProductPrice").value = product.price;
document.getElementById("editProductDescription").value = product.description;
document.getElementById("editProductForm").dataset.id = product.id;
document.getElementById("editProductForm").style.display = "block";
}
};
}
// Save edited product
function saveEditedProduct() {
const id = Number(document.getElementById("editProductForm").dataset.id);
const name = document.getElementById("editProductName").value.trim();
const category = document.getElementById("editProductCategory").value.trim();
const price = document.getElementById("editProductPrice").value.trim();
const description = document.getElementById("editProductDescription").value.trim();
const transaction = db.transaction("products", "readwrite");
const store = transaction.objectStore("products");
const request = store.get(id);
request.onsuccess = function () {
let product = request.result;
product.name = name;
product.category = category;
product.price = price;
product.description = description;
store.put(product);
};
transaction.oncomplete = function () {
closeEditForm();
loadProducts();
};
}
// Close edit form
function closeEditForm() {
document.getElementById("editProductForm").style.display = "none";
}
// Delete product with confirmation
function deleteProduct(id) {
if (confirm("Are you sure you want to delete this product? This action cannot be undone.")) {
let transaction = db.transaction(["products"], "readwrite");
let store = transaction.objectStore("products");
store.delete(id);
transaction.oncomplete = function () {
loadProducts();
};
}
}
// Search function
function searchProducts() {
const query = document.getElementById("productSearch").value.toLowerCase();
const transaction = db.transaction("products", "readonly");
const store = transaction.objectStore("products");
const request = store.getAll();
request.onsuccess = function () {
let filteredProducts = request.result.filter(product =>
product.name.toLowerCase().includes(query) ||
product.category.toLowerCase().includes(query)
);
displayProducts(filteredProducts);
};
}