-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
271 lines (241 loc) · 7.9 KB
/
script.js
File metadata and controls
271 lines (241 loc) · 7.9 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
// Select DOM elements
const title = document.getElementById("title");
const price = document.getElementById("price");
const taxes = document.getElementById("taxes");
const discount = document.getElementById("discount");
const total = document.getElementById("total");
const count = document.getElementById("count");
const category = document.getElementById("category");
const btnAdd = document.getElementById("btn-add");
const table = document.getElementById("product-table-body");
const search = document.getElementById("search");
const btnSearchTitle = document.getElementById("btn-search-title");
const btnSearchCategory = document.getElementById("btn-search-category");
const btnDelete = document.getElementById("delete");
const scroll = document.getElementById("scroll");
const output = document.querySelector(".stock-output");
const form = document.querySelector(".stock-input");
// Initialize product array from local storage
let products = JSON.parse(localStorage.getItem("products")) || [];
// Function to calculate total
function updateTotal() {
if (price.value == "") {
total.value = "";
total.style.background = "var(--incorrect)";
return;
}
let priceValue = parseFloat(price.value) || 0;
let taxesValue = parseFloat(taxes.value) || 0;
let discountValue = parseFloat(discount.value) || 0;
let totalValue = priceValue + taxesValue - discountValue;
total.value = totalValue >= 0 ? totalValue : 0;
total.style.background = "#EEEFF2";
}
// Event listeners for dynamic total calcul
price.addEventListener("input", updateTotal);
taxes.addEventListener("input", updateTotal);
discount.addEventListener("input", updateTotal);
// Function to validate input fields dynamically
function validateInput(input, condition) {
if (condition) {
input.style.outline = "2px solid var(--incorrect)";
input.dataset.invalid = "true";
} else {
input.style.outline = "none";
input.dataset.invalid = "false";
}
}
// Event listeners for real-time validation
title.addEventListener("input", () =>
validateInput(title, title.value.trim() === "")
);
price.addEventListener("input", () => validateInput(price, price.value === ""));
category.addEventListener("input", () =>
validateInput(category, category.value.trim() === "")
);
// Function to add a product
function addProduct() {
let missingFields = [];
if (title.value.trim() === "") {
validateInput(title, true);
missingFields.push("Title");
}
if (price.value === "" || parseFloat(price.value) <= 0) {
validateInput(price, true);
missingFields.push("Price");
}
if (category.value.trim() === "") {
validateInput(category, true);
missingFields.push("Category");
}
if (missingFields.length > 0) {
document.querySelector("[data-invalid='true']").focus();
return;
}
if (parseInt(count.value) <= 0) {
count.style.outline = "2px solid var(--incorrect)";
count.focus();
return;
} else {
count.style.outline = "none";
}
const itemCount = parseInt(count.value) || 1;
for (let i = 0; i < itemCount; i++) {
products.push({
title: title.value.trim(),
price: price.value,
taxes: taxes.value || 0,
discount: discount.value || 0,
total: total.value,
category: category.value.trim(),
});
}
localStorage.setItem("products", JSON.stringify(products));
[title, price, taxes, discount, count, category, total].forEach((input) => {
input.value = "";
input.style.outline = "none";
input.dataset.invalid = "false";
});
total.style.background = "var(--incorrect)";
showProducts();
}
// Event listener for adding a product
btnAdd.onclick = addProduct;
// Function to display products in the table
function showProducts() {
let rows = "";
for (let i = 0; i < products.length; i++) {
rows += `
<tr>
<td>${i}</td>
<td>${products[i].title}</td>
<td>${products[i].price}</td>
<td>${products[i].taxes}</td>
<td>${products[i].discount}</td>
<td>${products[i].total}</td>
<td>${products[i].category}</td>
<td><button class="btn-secondary btn-update" onclick="updateOneProduct(${i})">Update</button></td>
<td><button class="btn-secondary btn-delete" onclick="deleteOneProduct(${i})">Delete</button></td>
</tr>
`;
}
table.innerHTML = rows;
btnDelete.innerHTML = `Delete All (${products.length})`;
output.style.display = products.length > 0 ? "block" : "none";
}
// Function to delete all products
function deleteAll() {
table.innerHTML = "";
localStorage.clear();
products = [];
output.style.display = "none";
}
btnDelete.addEventListener("click", deleteAll);
// Function to delete one product
function deleteOneProduct(index) {
products.splice(index, 1);
localStorage.setItem("products", JSON.stringify(products));
showProducts();
}
// Function to update a product
function updateOneProduct(index) {
title.value = products[index].title;
price.value = products[index].price;
taxes.value = products[index].taxes;
discount.value = products[index].discount;
total.value = products[index].total;
category.value = products[index].category;
count.setAttribute("readonly", "true");
btnAdd.textContent = "Update Product";
total.style.background = "var(--input-bg-color)";
form.classList.add("update");
form.scrollIntoView({ behavior: "smooth" });
title.focus();
btnDelete.removeEventListener("click", deleteAll);
document.querySelector(".btn-delete").removeAttribute("onclick");
btnAdd.onclick = function () {
saveUpdatedProduct(index);
};
}
// Function to save updated product
function saveUpdatedProduct(index) {
let missingFields = [];
if (title.value.trim() === "") {
validateInput(title, true);
missingFields.push("Title");
}
if (price.value === "" || parseFloat(price.value) <= 0) {
validateInput(price, true);
missingFields.push("Price");
}
if (category.value.trim() === "") {
validateInput(category, true);
missingFields.push("Category");
}
if (missingFields.length > 0) {
document.querySelector("[data-invalid='true']").focus();
return;
}
products[index] = {
title: title.value.trim(),
price: price.value,
taxes: taxes.value || 0,
discount: discount.value || 0,
total: total.value,
category: category.value.trim(),
};
localStorage.setItem("products", JSON.stringify(products));
[title, price, taxes, discount, count, category, total].forEach((input) => {
input.value = "";
input.style.outline = "none";
input.dataset.invalid = "false";
});
total.style.background = "var(--incorrect)";
btnAdd.textContent = "Add Product";
btnAdd.onclick = addProduct;
count.removeAttribute("readonly");
form.classList.remove("update");
showProducts();
btnDelete.addEventListener("click", deleteAll);
}
// Function to filter table results
function filterTableBy(index) {
let filter = search.value.trim().toLowerCase();
let rows = table.getElementsByTagName("tr");
for (let i = 0; i < rows.length; i++) {
let cell = rows[i].getElementsByTagName("td")[index];
if (cell) {
rows[i].style.display = cell.textContent
.trim()
.toLowerCase()
.includes(filter)
? ""
: "none";
}
}
}
// Event listeners for search functionality
btnSearchTitle.addEventListener("focus", () => {
search.placeholder = "Search by Title...";
search.dataset.searchIndex = "1";
filterTableBy(1);
});
btnSearchCategory.addEventListener("focus", () => {
search.placeholder = "Search by Category...";
search.dataset.searchIndex = "6";
filterTableBy(6);
});
search.addEventListener("input", () => {
let index = search.dataset.searchIndex
? parseInt(search.dataset.searchIndex)
: 1;
filterTableBy(index);
});
// Scroll-to-top functionality
window.onscroll = () => {
scroll.style.display = window.scrollY >= 1200 ? "block" : "none";
};
scroll.onclick = () => window.scrollTo(0, 0);
// // Load products on page load
window.onload = title.focus();
showProducts();