-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendorsPage.html
293 lines (239 loc) · 9.89 KB
/
vendorsPage.html
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
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="./vendorsPageStyle.css" />
<head>
<script
src="https://kit.fontawesome.com/bad63f900f.js"
crossorigin="anonymous"
></script>
<script src="https://www.gstatic.com/firebasejs/9.10.0/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.10.0/firebase-firestore-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.10.0/firebase-auth-compat.js"></script>
</head>
<script type="text/javascript" src="projectsStorageService.js"></script>
<body>
<div id="header">
<div id="websiteTitle">InvoiceMasters</div>
<div id="userDiv">
<button id="userBtn" onclick="toProfile()">
<i class="fa-solid fa-user"></i>
</button>
Username
</div>
</div>
<button id="newVendorBtn" onclick="newVendorDialog()">
Add new vendor <i class="fa-solid fa-file-circle-plus"></i>
</button>
<div id="main"></div>
<div id="newVendorDialog">
<button id="closeBtn" class="closeBtn" onclick="closeDialog()">
<i class="fa-solid fa-xmark"></i>
</button>
<label class="vendorLabel" for="vendorName"> Vendor name: </label>
<input class="vendorInput" id="vendorName" />
<label class="vendorLabel" for="vendorAdress"> Adress: </label>
<input class="vendorInput" id="vendorAdress" />
<label class="vendorLabel" for="vendorVAT"> VAT Number: </label>
<input class="vendorInput" id="vendorVAT" />
<label class="vendorLabel" for="vendorEmail"> Email: </label>
<input class="vendorInput" id="vendorEmail" />
<button id="addVendorDB" class="btnDB" onclick="addVendorDB()">
Add vendor
</button>
</div>
<div id="editDialog">
<button id="closeBtnEdit" class="closeBtn" onclick="closeDialog()">
<i class="fa-solid fa-xmark"></i>
</button>
<label class="vendorLabel" for="vendorNameEdit"> Vendor name: </label>
<input class="vendorInput" id="vendorNameEdit" />
<label class="vendorLabel" for="vendorAdressEdit"> Adress: </label>
<input class="vendorInput" id="vendorAdressEdit" />
<label class="vendorLabel" for="vendorVATEdit"> VAT Number: </label>
<input class="vendorInput" id="vendorVATEdit" />
<label class="vendorLabel" for="vendorEmailEdit"> Email: </label>
<input class="vendorInput" id="vendorEmailEdit" />
<button id="editVendorDB" class="btnDB" onclick="editVendorDB()">
Save changes
</button>
</div>
<div id="deleteVendorDialog">
<button class="closeBtn" onclick="closeDialog()">
<i class="fa-solid fa-xmark"></i>
</button>
<h2>Are you sure you want to delete this vendor?</h2>
<h5>This action cannot be undone.</h5>
<button class="deleteVendorBtn" onclick="deleteVendorDB()">Delete</button>
</div>
<script>
const toProfile = () => {
location.href = "profilePage.html";
};
const newVendorDialog = () => {
const vendorDialog = document.getElementById("newVendorDialog");
vendorDialog.style.display = "inline-grid";
};
const closeDialog = () => {
const vendorDialog = document.getElementById("newVendorDialog");
vendorDialog.style.display = "none";
const editDialog = document.getElementById("editDialog");
editDialog.style.display = "none";
const deleteVendorDialog =
document.getElementById("deleteVendorDialog");
deleteVendorDialog.style.display = "none";
};
const addVendorDB = () => {
const vendorName = document.getElementById("vendorName").value;
const vendorAdress = document.getElementById("vendorAdress").value;
const vendorVAT = document.getElementById("vendorVAT").value;
const vendorEmail = document.getElementById("vendorEmail").value;
try {
newVendor(
vendorName,
vendorAdress,
vendorVAT,
vendorEmail,
currentUser
);
alert("Vendor added successfully");
closeDialog();
vendorsDOM();
} catch (error) {
console.log(error);
}
};
let currentVendor = "";
const editVendor = (event) => {
const whichVendorBtn = event.target.id;
const whichVendorBtnId = whichVendorBtn.split("-").pop();
currentVendor = vendorsInfo[whichVendorBtnId];
const editDialog = document.getElementById("editDialog");
editDialog.style.display = "grid";
document.getElementById("vendorNameEdit").value =
currentVendor.data.vendorName;
document.getElementById("vendorAdressEdit").value =
currentVendor.data.vendorAdress;
document.getElementById("vendorVATEdit").value =
currentVendor.data.vendorVAT;
document.getElementById("vendorEmailEdit").value =
currentVendor.data.vendorEmail;
};
const editVendorDB = () => {
const vendorId = currentVendor.id;
const name = document.getElementById("vendorNameEdit").value;
const adress = document.getElementById("vendorAdressEdit").value;
const VAT = document.getElementById("vendorVATEdit").value;
const email = document.getElementById("vendorEmailEdit").value;
try {
updateVendor(vendorId, name, adress, VAT, email);
const editDialog = document.getElementById("editDialog");
editDialog.style.display = "none";
vendorsDOM();
} catch (error) {
console.log(error);
}
};
const deleteVendor = (event) => {
const whichVendorBtn = event.target.id;
const whichVendorBtnId = whichVendorBtn.split("-").pop();
currentVendor = vendorsInfo[whichVendorBtnId];
const deleteVendorDialog =
document.getElementById("deleteVendorDialog");
deleteVendorDialog.style.display = "grid";
};
const deleteVendorDB = () => {
const vendorId = currentVendor.id;
try {
deleteVendorData(vendorId);
const deleteVendorDialog =
document.getElementById("deleteVendorDialog");
deleteVendorDialog.style.display = "none";
vendorsDOM();
} catch (error) {
console.log(error);
}
};
let vendorsWithId = [];
let vendorsInfo = [];
const vendorsDOM = () => {
vendorsWithId.length = 0;
vendorsInfo.length = 0;
getVendors().then((getVendorsQueryResult) => {
getVendorsQueryResult.forEach((doc) => {
vendorsWithId.push({ id: doc.id, data: doc.data() });
vendorsInfo = vendorsWithId.filter(
(vendor) => vendor.data.currentUser === currentUser
);
const table = document.createElement("table");
const headerRow = document.createElement("tr");
const headerNames = [
"Vendor Name",
"Adress",
"VAT Number",
"Email",
"Options",
];
headerNames.forEach((headerName) => {
const headerCell = document.createElement("th");
headerCell.textContent = headerName;
headerRow.appendChild(headerCell);
});
table.appendChild(headerRow);
// Create the rows for each vendor
for (let i = 0; i < vendorsInfo.length; i++) {
let row = document.createElement("tr");
row.className = "rowClass";
row.setAttribute("id", "rowClass-" + i);
let cells = [
vendorsInfo[i].data.vendorName,
vendorsInfo[i].data.vendorAdress,
vendorsInfo[i].data.vendorVAT,
vendorsInfo[i].data.vendorEmail,
];
cells.forEach((cellData) => {
const cell = document.createElement("td");
cell.textContent = cellData;
row.appendChild(cell);
});
table.appendChild(row);
}
// Add the table to the page
const main = document.getElementById("main");
main.innerHTML = "";
main.appendChild(table);
});
optionsAdd();
});
};
const optionsAdd = () => {
let trs = document.getElementsByTagName("table")[0].rows;
for (let i = 1; i < trs.length; i++) {
let td = document.createElement("td");
td.classList.add("optionsTd");
// pune id cu nr si cauta dupa nr ca sa gasesti vendorul si sa modifici/stergi
trs[i].appendChild(td);
}
const optionsTdCollection =
document.getElementsByClassName("optionsTd");
for (let j = 0; j < optionsTdCollection.length; j++) {
let editBtn = document.createElement("i");
editBtn.classList.add("fa-solid");
editBtn.classList.add("fa-pen-to-square");
editBtn.classList.add("optionsBtn");
editBtn.setAttribute("id", "editBtn-" + j);
editBtn.addEventListener("click", editVendor);
let deleteBtn = document.createElement("i");
deleteBtn.classList.add("fa-solid");
deleteBtn.classList.add("fa-trash");
deleteBtn.classList.add("optionsBtn");
deleteBtn.setAttribute("id", "deleteBtn-" + j);
deleteBtn.addEventListener("click", deleteVendor);
optionsTdCollection[j].appendChild(editBtn);
optionsTdCollection[j].appendChild(deleteBtn);
}
};
vendorsDOM();
// setTimeout(optionsAdd, 1000);
</script>
</body>
</html>