-
Notifications
You must be signed in to change notification settings - Fork 0
/
Waste Management.js
228 lines (185 loc) · 5.86 KB
/
Waste Management.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
// Display the initial wastes and rows
displayWastes();
displayRow();
// Get input elements
let addName = document.getElementById("addName");
let addNumber = document.getElementById("addNumber");
let addNature = document.getElementById("addNature");
let addLocation = document.getElementById("addLocation");
let addDate = document.getElementById("addDate");
let addBtn = document.getElementById("addBtn");
let clearCardsBtn = document.getElementById("clearCardsBtn");
// Add event listener to Add button
addBtn.addEventListener("click", function (e) {
let list = localStorage.getItem("wastes");
if (list == null) {
wasteObj = [];
} else {
wasteObj = JSON.parse(list);
}
let obj = {
name: addName.value,
number: addNumber.value,
nature: addNature.value,
location: addLocation.value,
date: addDate.value,
isDone: false, // Set isDone property to false for new wastes
};
wasteObj.push(obj);
localStorage.setItem("wastes", JSON.stringify(wasteObj));
// Clear input values
addName.value = "";
addNumber.value = "";
addNature.value = "";
addLocation.value = "";
addDate.value = "";
displayWastes();
displayRow(); // Add this line to update the rows after adding a waste
});
// Function to display wastes from localStorage
function displayWastes() {
let list = localStorage.getItem("wastes");
if (list == null) {
wasteObj = [];
} else {
wasteObj = JSON.parse(list);
}
let card = "";
wasteObj.forEach(function (element, index) {
// Check if the necessary properties exist in the current element
if (
element &&
element.nature &&
element.location &&
element.name &&
element.number &&
element.date
) {
card += `
<div class="task my-2 mx-2 card" style="width: 18rem;">
<div class="card-body card-background">
<h5>Nature of waste:</h5>
<h5 class="card-title nature">${element.nature}</h5><br>
Location: <span class="location">${element.location}</span><br>
Name: <span class="card-text name">${element.name}</span>
<p class="card-text">Contact Number: ${element.number}</p>
<p class="card-text">Date: ${element.date}</p>
<button id="${index}" class="btn btn-danger" onclick="deleteWastes(this.id)">X</button>
</div>
</div>`;
}
});
let elm = document.getElementById("wastes");
if (wasteObj.length != 0) {
elm.innerHTML = card;
} else {
elm.innerHTML = "No waste found as of now";
}
}
function deleteWastes(index) {
let list = localStorage.getItem("wastes");
if (list == null) {
wasteObj = [];
} else {
wasteObj = JSON.parse(list);
}
wasteObj.splice(index, 1);
localStorage.setItem("wastes", JSON.stringify(wasteObj));
displayWastes();
displayRow();
// Remove corresponding row from table
let tableRows = document.querySelectorAll("#tableBody tr");
if (tableRows.length > index) {
tableRows[index].remove();
}
}
// Event listener for submit button
let submit = document.getElementById("submit");
submit.addEventListener("click", Submit);
function Submit(e) {
let list = localStorage.getItem("wastes");
let newWasteObj;
if (list == null) {
newWasteObj = [];
} else {
newWasteObj = JSON.parse(list);
}
let cardElements = document.getElementsByClassName("task");
for (let i = 0; i < cardElements.length; i++) {
let nature = cardElements[i].querySelector(".nature").textContent;
let location = cardElements[i].querySelector(".location").textContent;
let name = cardElements[i].querySelector(".name").textContent;
// Check if the waste is already present in the newWasteObj
let isDuplicate = newWasteObj.some(function (element) {
return (
element.natureTable === nature &&
element.locationTable === location &&
element.nameTable === name
);
});
if (!isDuplicate) {
let newObj = {
nameTable: name,
natureTable: nature,
locationTable: location,
isDone: false, // Set isDone property to false for new wastes
};
newWasteObj.push(newObj);
}
}
localStorage.setItem("wastes", JSON.stringify(newWasteObj));
// Clear input values
addName.value = "";
addNumber.value = "";
addNature.value = "";
addLocation.value = "";
addDate.value = "";
displayRow();
}
function displayRow() {
let tableBody = document.getElementById("tableBody");
let list = localStorage.getItem("wastes");
if (list == null) {
newWasteObj = [];
} else {
newWasteObj = JSON.parse(list);
}
tableBody.innerHTML = ""; // Clear table body before adding rows
newWasteObj.forEach(function (element, index) {
// Check if the necessary properties exist in the current element
if (element && element.natureTable && element.locationTable && element.nameTable) {
let rowClass = element.isDone ? "row-done" : "";
let uiString = `
<tr class="${rowClass}">
<td>${element.natureTable}</td>
<td>${element.locationTable}</td>
<td>${element.nameTable}</td>
<td><button id=${index} class="btn btn-danger" onclick="deleteWastesRow(${index})">Delete</button></td>
</tr>`;
tableBody.insertAdjacentHTML("beforeend", uiString);
}
});
}
function deleteWastesRow(index) {
let list = localStorage.getItem("wastes");
if (list == null) {
newWasteObj = [];
} else {
newWasteObj = JSON.parse(list);
}
newWasteObj.splice(index, 1);
localStorage.setItem("wastes", JSON.stringify(newWasteObj));
displayWastes();
displayRow();
// Remove corresponding row from table
let tableRows = document.querySelectorAll("#tableBody tr");
if (tableRows.length > index) {
tableRows[index].remove();
}
}
// Clear all cards button event listener
clearCardsBtn.addEventListener("click", function (e) {
localStorage.removeItem("wastes");
displayWastes();
displayRow();
});