-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
307 lines (273 loc) · 8.94 KB
/
app.js
File metadata and controls
307 lines (273 loc) · 8.94 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
//variables
const fullName = localStorage.getItem("full_name");
let password = localStorage.getItem("password");
let userName = document.getElementById("userName");
// inputs
let title = document.getElementById("title");
let price = document.getElementById("price");
let taxes = document.getElementById("taxes");
let ads = document.getElementById("ads");
let discount = document.querySelector(".discount");
let total = document.getElementById("total");
let count = document.getElementById("count");
let category = document.getElementById("category");
let search = document.getElementById("search");
// buttons
let createBtn = document.getElementById("create");
let deleteAll = document.getElementById("deleteAll");
// Checking if the user is registered
if((fullName === null ) || (password === null)){
// if not back to the login page
window.location = "index.html";
}
// return the first letter of the first and last name
function setName(fullName){
const words = fullName.trim().split(' ');
const firstName = words[0][0];
let lastName;
if (words.length > 1) {
lastName = words[1][0];
} else {
lastName = '';
}
return `${firstName}${lastName}`;
}
// getting the full name from the local storage and add it into the user name feild
window.addEventListener('load',()=>{
let name = setName(fullName);
userName.innerText = name ;
let fname = fullName.toUpperCase()
userName.setAttribute("title",fname);
});
// total function
function setTotal(){
if(price.value !== "" && taxes.value !== ""){
let totalPrice = parseFloat(price.value) + parseFloat(taxes.value)
total.innerText = totalPrice
document.querySelector(".price p").style.backgroundColor = "green"
if(ads.value !== ""){
totalPrice += parseFloat(ads.value)
total.innerText = totalPrice
}
if(discount.value !== ""){
totalPrice -= parseFloat(discount.value)
total.innerText = totalPrice
}
}else {
document.querySelector(".price p").style.backgroundColor = "#d61c22"
}
}
setInterval(() => {
setTotal()
}, 100); // set total every 100 ms
// create function
let productsList;
if (localStorage.products != null) {
productsList = JSON.parse(localStorage.products);
}else{
productsList = [];
}
function create() {
if (title.value !== "" && total.innerText !== "") {
let product = {
ID:IDGenerator(),
Title:title.value,
Price:price.value,
Taxes:taxes.value,
Ads:ads.value,
Discount:discount.value,
Total:total.innerText,
Count:count.value,
Category:category.value
};
// count
if (product.Count > 1) {
for (let i = 0; i < product.Count; i++) {
productsList.push(product);
}
} else if(product.Count == null || product.Count <= 1){
productsList.push(product);
}
// saving the products info in local storage
localStorage.setItem("products",JSON.stringify(productsList));
document.querySelector(".alert").style.transform = 'translateY(-30px)'
}else {
if (window.innerWidth > 560) {
document.querySelector(".alert").style.transform = 'translateY(25px)'
}
}
}
// clear all inputs
function clear() {
title.value = "";
price.value = "";
taxes.value = "";
ads.value = "";
discount.value = "";
total.innerText = "";
count.value = "";
category.value = "";
title.focus();
}
// total products
function setTotalPro() {
let totalpro = document.querySelector('#totalPro')
totalpro.innerText = productsList.length;
}
setTotalPro()
// displaying the product on the table
function display() {
let row = "";
for (let i = 0; i < productsList.length; i++) {
row += `
<tr>
<td data-label="ID">${productsList[i].ID}</td>
<td data-label="TITLE">${productsList[i].Title}</td>
<td data-label="PRICE">${productsList[i].Price}</td>
<td data-label="TAXES">${productsList[i].Taxes}</td>
<td data-label="ADS">${productsList[i].Ads}</td>
<td data-label="DISCOUNT">${productsList[i].Discount}</td>
<td data-label="TOTAL">${productsList[i].Total}</td>
<td data-label="CATEGORY">${productsList[i].Category}</td>
<td onclick = "update(${i})" data-label="UPDATE"><button class="btn">update</button></td>
<td onclick = "deletePro(${i})" data-label="DELETE"><button class="btn">delete</button></td>
</tr>
`
}
document.querySelector("table tbody").innerHTML = row;
}
display()
// click event of the create button
createBtn.addEventListener("click",()=>{
create();
display(); // update the table
setTotalPro(); // update the total
showDeleteAll()
showSearch()
createBtn.innerText = "create"
if (title.value !== "" && total.innerText !== "") {
clear();
}
})
// ID generator function
function IDGenerator(){
let numberPart = Math.floor(Math.random() * 900) + 100; // return a random number between 100 and 999
let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let strPart = '';
for (let i = 0; i < 3; i++) {
strPart += letters.charAt(Math.floor(Math.random() * letters.length)); // return a random string of three letters
}
let ID = strPart + numberPart; // there are 3,814,040,000,000 potential combinations
if( !(listID.includes(ID)) ){
listID.push(ID);
return ID
}
}
let listID = [];
// delete product
function deletePro(i) {
productsList.splice(i,1);
// localStorage.setItem("products",JSON.stringify(productsList));
display(); // update the table
setTotalPro();
showDeleteAll()
showSearch()
}
// function to delete all products
function deleteAllPro() {
productsList.splice(0); // remove all products from the productsList array
localStorage.removeItem("products"); // remove the 'products' key from local storage
display(); // update the table
setTotalPro(); // update the total
showDeleteAll()
showSearch()
}
// add a click event listener to the 'deleteAll' button
deleteAll.addEventListener('click', deleteAllPro);
// function to display the 'deleteAll' button
function showDeleteAll() {
if (productsList.length > 0) {
deleteAll.style.display = "block"
}else {
deleteAll.style.display = "none"
}
}
showDeleteAll()
// update function
function update(i) {
title.value = productsList[i].Title;
price.value = productsList[i].Price;
taxes.value = productsList[i].Taxes;
ads.value = productsList[i].Ads;
discount.value = productsList[i].Discount;
total.innerText = productsList[i].Total;
count.value = productsList[i].Count;
category.value = productsList[i].Category;
createBtn.innerText = "Save";
title.focus();
// checks if the ID of each product in productsList is not equal to the ID of the clicked product
productsList = productsList.filter(product => {
return product.ID !== productsList[i].ID;
});
display();
setTotalPro();
localStorage.setItem("products",JSON.stringify(productsList));
}
// serch function
var searchMode = document.getElementById('searchBy');
function searchPro() {
// checks if the ID of each product in productsList is equal to the ID inserted
if (searchMode.value === "ID") {
productsList = productsList.filter(product => {
return product.ID.toUpperCase() === search.value.toUpperCase().trim();
});
}
// checks if the Title of each product in productsList is equal to the Title inserted
else if(searchMode.value === "Title") {
productsList = productsList.filter(product => {
return product.Title.toUpperCase() === search.value.toUpperCase().trim();
});
}
// checks if the Category of each product in productsList is equal to the Category inserted
else if(searchMode.value === "Category") {
productsList = productsList.filter(product => {
return product.Category.toUpperCase() === search.value.toUpperCase().trim();
});
}
if(search.value === '') {
return productsList;
}
display(); // calling the display function to update the table(we're not updating the productsList array!!!)
}
// enter event for search input
search.addEventListener("keydown", (event) => {
// check if the "Enter" key was pressed
if (event.key === "Enter") {
searchPro();
setTotalPro();
}
});
// setPlaceHolder
function setPlaceHolder(searchMode) {
if (searchMode === "ID") {
search.placeholder = "Search By ID"
}else if (searchMode === "Title") {
search.placeholder = "Search By Title"
}else {
search.placeholder = "Search By Category"
}
}
searchMode.addEventListener("click",()=>{
setPlaceHolder(searchMode.value)
})
// function to display the 'search' div
function showSearch() {
if (productsList.length > 0) {
document.querySelector(".search").style.display = "flex"
search.style.display = "block"
}else {
document.querySelector(".search").style.display = "none"
search.style.display = "none"
}
}
showSearch()