-
Notifications
You must be signed in to change notification settings - Fork 0
/
products.js
286 lines (227 loc) · 8.39 KB
/
products.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
function openSidebar() {
var side = document.getElementById('sidebar');
side.style.display = (side.style.display === "block") ? "none" : "block";
}
function closeSidebar() {
document.getElementById('sidebar').style.display = 'none';
}
function openForm() {
var form = document.getElementById("product-form")
form.style.display = (form.style.display === "block") ? "none" : "block";
}
function closeForm() {
document.getElementById("product-form").style.display = "none";
}
let products = [];
function init() {
const storedProducts = localStorage.getItem("bizTrackProducts");
if (storedProducts) {
products = JSON.parse(storedProducts);
} else {
products = [
{
prodID: "PD001",
prodName: "Baseball caps",
prodDesc: "Peace embroidered cap",
prodCat: "Hats",
prodPrice: 25.00,
prodSold: 20
},
{
prodID: "PD002",
prodName: "Water bottles",
prodDesc: "Floral lotus printed bottle",
prodCat: "Drinkware",
prodPrice: 48.50,
prodSold: 10
},
{
prodID: "PD003",
prodName: "Sweatshirts",
prodDesc: "Palestine sweater",
prodCat: "Clothing",
prodPrice: 17.50,
prodSold: 70
},
{
prodID: "PD004",
prodName: "Posters",
prodDesc: "Vibes printed poster",
prodCat: "Home decor",
prodPrice: 12.00,
prodSold: 60
},
{
prodID: "PD005",
prodName: "Pillow cases",
prodDesc: "Morrocan print pillow case",
prodCat: "Accessories",
prodPrice: 17.00,
prodSold: 40
},
];
localStorage.setItem("bizTrackProducts", JSON.stringify(products));
}
renderProducts(products);
}
function addOrUpdate(event) {
let type = document.getElementById("submitBtn").textContent;
if (type === 'Add') {
newProduct(event);
} else if (type === 'Update'){
const prodID = document.getElementById("product-id").value;
updateProduct(prodID);
}
}
function newProduct(event) {
event.preventDefault();
const prodID = document.getElementById("product-id").value;
const prodName = document.getElementById("product-name").value;
const prodDesc = document.getElementById("product-desc").value;
const prodCat = document.getElementById("product-cat").value;
const prodPrice = parseFloat(document.getElementById("product-price").value);
const prodSold = parseInt(document.getElementById("product-sold").value);
if (isDuplicateID(prodID, null)) {
alert("Product ID already exists. Please use a unique ID.");
return;
}
const product = {
prodID,
prodName,
prodDesc,
prodCat,
prodPrice,
prodSold,
};
products.push(product);
renderProducts(products);
localStorage.setItem("bizTrackProducts", JSON.stringify(products));
document.getElementById("product-form").reset();
}
function renderProducts(products) {
const prodTableBody = document.getElementById("tableBody");
prodTableBody.innerHTML = "";
const prodToRender = products;
prodToRender.forEach(product => {
const prodRow = document.createElement("tr");
prodRow.className = "product-row";
prodRow.dataset.prodID = product.prodID;
prodRow.dataset.prodName = product.prodName;
prodRow.dataset.prodDesc = product.prodDesc;
prodRow.dataset.prodCat = product.prodCat;
prodRow.dataset.prodPrice = product.prodPrice;
prodRow.dataset.prodSold = product.prodSold;
prodRow.innerHTML = `
<td>${product.prodID}</td>
<td>${product.prodName}</td>
<td>${product.prodDesc}</td>
<td>${product.prodCat}</td>
<td>$${product.prodPrice.toFixed(2)}</td>
<td>${product.prodSold}</td>
<td class="action">
<i title="Edit" onclick="editRow('${product.prodID}')" class="edit-icon fa-solid fa-pen-to-square"></i>
<i onclick="deleteProduct('${product.prodID}')" class="delete-icon fas fa-trash-alt"></i>
</td>
`;
prodTableBody.appendChild(prodRow);
});
}
function editRow(prodID) {
const productToEdit = products.find(product => product.prodID === prodID);
document.getElementById("product-id").value = productToEdit.prodID;
document.getElementById("product-name").value = productToEdit.prodName;
document.getElementById("product-desc").value = productToEdit.prodDesc;
document.getElementById("product-cat").value = productToEdit.prodCat;
document.getElementById("product-price").value = productToEdit.prodPrice;
document.getElementById("product-sold").value = productToEdit.prodSold;
document.getElementById("submitBtn").textContent = "Update";
document.getElementById("product-form").style.display = "block";
}
function deleteProduct(prodID) {
const indexToDelete = products.findIndex(product => product.prodID === prodID);
if (indexToDelete !== -1) {
products.splice(indexToDelete, 1);
localStorage.setItem("bizTrackProducts", JSON.stringify(products));
renderProducts(products);
}
}
function updateProduct(prodID) {
const indexToUpdate = products.findIndex(product => product.prodID === prodID);
if (indexToUpdate !== -1) {
const updatedProduct = {
prodID: document.getElementById("product-id").value,
prodName: document.getElementById("product-name").value,
prodDesc: document.getElementById("product-desc").value,
prodCat: document.getElementById("product-cat").value,
prodPrice: parseFloat(document.getElementById("product-price").value),
prodSold: parseInt(document.getElementById("product-sold").value),
};
if (isDuplicateID(updatedProduct.prodID, prodID)) {
alert("Product ID already exists. Please use a unique ID.");
return;
}
products[indexToUpdate] = updatedProduct;
localStorage.setItem("bizTrackProducts", JSON.stringify(products));
renderProducts(products);
document.getElementById("product-form").reset();
document.getElementById("submitBtn").textContent = "Add";
}
}
function isDuplicateID(prodID, currentID) {
return products.some(product => product.prodID === prodID && product.prodID !== currentID);
}
function sortTable(column) {
const tbody = document.getElementById("tableBody");
const rows = Array.from(tbody.querySelectorAll("tr"));
const isNumeric = column === "prodPrice" || column === "prodSold";
const sortedRows = rows.sort((a, b) => {
const aValue = isNumeric ? parseFloat(a.dataset[column]) : a.dataset[column];
const bValue = isNumeric ? parseFloat(b.dataset[column]) : b.dataset[column];
if (typeof aValue === "string" && typeof bValue === "string") {
return aValue.localeCompare(bValue, undefined, { sensitivity: "base" });
} else {
return aValue - bValue;
}
});
rows.forEach(row => tbody.removeChild(row));
sortedRows.forEach(row => tbody.appendChild(row));
}
document.getElementById("searchInput").addEventListener("keyup", function(event) {
if (event.key === "Enter") {
performSearch();
}
});
function performSearch() {
const searchInput = document.getElementById("searchInput").value.toLowerCase();
const rows = document.querySelectorAll(".product-row");
rows.forEach(row => {
const visible = row.innerText.toLowerCase().includes(searchInput);
row.style.display = visible ? "table-row" : "none";
});
}
function exportToCSV() {
const productsToExport = products.map(product => {
return {
prodID: product.prodID,
prodName: product.prodName,
prodDesc: product.prodDesc,
prodCategory: product.prodCat,
prodPrice: product.prodPrice.toFixed(2),
QtySold: product.prodSold,
};
});
const csvContent = generateCSV(productsToExport);
const blob = new Blob([csvContent], { type: 'text/csv' });
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'biztrack_product_table.csv';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function generateCSV(data) {
const headers = Object.keys(data[0]).join(',');
const rows = data.map(order => Object.values(order).join(','));
return `${headers}\n${rows.join('\n')}`;
}
init();