-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.html
343 lines (281 loc) · 19.5 KB
/
users.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>USERS</title>
<link rel="stylesheet" href="assets/css/Navigation-Dark-Clean.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="assets/css/styles.css">
</head>
<body>
<nav class="navbar navbar-dark navbar-expand-md bg-dark navigation-clean">
<div class="container">
<a class="navbar-brand" href="#">Student Placement System</a>
<button class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navcol-1">
<span class="visually-hidden">Toggle navigation</span>
<span class="navbar-toggler-icon"></span>
</button>
<div id="navcol-1" class="collapse navbar-collapse">
<ul class="nav navbar-nav ms-auto">
</ul>
<button id = "home" class="btn btn-secondary" type="button" href="home.html">Home</button>
<button id = "out" class="btn btn-primary" type="button" href="index.html">Sign Out</button>
</div>
</div>
</nav>
<div>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<br>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Control Panel</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
</div>
</div>
</div>
<div class="container">
<div class="panel panel-default panel-table">
<div class="panel-heading">
<div class="row">
<div class="col col-xs-6">
<h3 class="panel-title">Users</h3>
</div>
<div class="col col-xs-6 text-right">
<button class="btn btn-sm btn-primary btn-create" type="button" id="add">
Add New User
</button>
</div>
<div class="card-body" >
<div class="table-responsive">
<div>
<table class="table table-bordered">
<thead>
<tr align="center">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Contact Number</th>
<th>ID</th>
<th >
<em class="fa fa-cog"></em>
</th>
</tr>
</thead>
<tbody id="table_body">
</tbody>
</table>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Update User Details</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<label class="labs">First Name: </label>
<input type="text" id="first"></input> <br>
<label class="labs">Last Name: </label>
<input type="text" id="last"></input> <br>
<label class="labs">Email: </label>
<input type="text" id="email"></input> <br>
<label class="labs">Contact No: </label>
<input type="text" id="contact"></input> <br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-bs-dismiss="modal">Close</button>
<button type="button" id="update" class="btn btn-success">Update User</button>
<button type="button" id="remove" class="btn btn-danger">Delete User</button>
</div>
</div>
</div>
</div>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-app.js";
import { getDatabase, ref, child, get, remove, onValue, update} from "https://www.gstatic.com/firebasejs/9.6.11/firebase-database.js";
import { collection,getFirestore, doc, getDocs, setDoc, deleteDoc } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-firestore.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-analytics.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
//--------------------------CONFIGURATION----------------------------------------//
const firebaseConfig = {
apiKey: "AIzaSyAZVQZO8XWR0UTPoSMLrRN7nSltUDW9Xzs",
authDomain: "dfmpc-student-placement-system.firebaseapp.com",
databaseURL: "https://dfmpc-student-placement-system-default-rtdb.europe-west1.firebasedatabase.app",
projectId: "dfmpc-student-placement-system",
storageBucket: "dfmpc-student-placement-system.appspot.com",
messagingSenderId: "295214875936",
appId: "1:295214875936:web:546dcdb4f822d1c4c84345",
measurementId: "G-1JPG0HXVWQ"
};
const app = initializeApp(firebaseConfig);
function getData() {
firebaseConfig.da
}
const analytics = getAnalytics(app);
const db = getFirestore(); //realtime
//----------------------------END OF CONFIGURATION---------------------------------//
var postKey;//this key is used to find each childnode in users
//the following variables are unseen attributes in user table
var created_at;
var password_digest;
var user_Type;
var user_profile_photo;
var user_id;
var student;
//create Modal Form variables
var firstMod = document.getElementById('first');
var lastMod = document.getElementById('last');
var emailMod = document.getElementById('email');
var contactMod = document.getElementById('contact');
//function to fetch user information from firestore database and display in table
async function add_users () {
const querySnapshot = await getDocs(collection(db, "users"));
querySnapshot.forEach((doc) => {
var first_name = doc.data().user_FirstName;
var last_name = doc.data().user_LastName;
var email = doc.data().email;
var contact = doc.data().user_ContactNo;
user_id = doc.data().id;
postKey = user_id - 1;
created_at = doc.data().created_at;
password_digest = doc.data().password_digest;
user_Type = doc.data().user_Type;
user_profile_photo = doc.data().user_profile_photo;
student = doc.data().Student_Number;
AddItemsToTable(first_name, last_name, email, contact, user_id);
});
}
window.onload = add_users(); //load the data when the page loads
//function to add items to each row in table
function AddItemsToTable(first_name, last_name, email, contact, user_id){ //add the data to the table
var table = document.getElementById("table_body"); //get the table
var row = document.createElement("tr"); //create a row
var cell1 = document.createElement("td"); //create a cell
var cell2 = document.createElement("td"); //create a cell
var cell3 = document.createElement("td"); //create a cell
var cell4 = document.createElement("td"); //create a cell
var cell5 = document.createElement("td"); //create a cell
var cell6 = document.createElement("div");
cell1.innerHTML = first_name;
cell2.innerHTML = last_name;
cell3.innerHTML = email;
cell4.innerHTML = contact;
cell5.innerHTML = user_id;
cell6.innerHTML = "<button class='btn btn-sm btn-primary m-2' data-bs-toggle='modal' data-bs-target='#myModal' type='button'>Edit</button>";
row.appendChild(cell1);
row.appendChild(cell2);
row.appendChild(cell3);
row.appendChild(cell4);
row.appendChild(cell5);
row.appendChild(cell6);
table.appendChild(row);
//cell6 event listener ensures that when we click on edit, fields are already filled in with user info
//This allows us to chooose exactly what to edit.
cell6.addEventListener("click", function()
{
console.log(user_id); //confirm that we are looking at the right user's information
// display user information for each existing user on modal form so we see what changes are being made
firstMod.value = first_name;
lastMod.value = last_name;
emailMod.value = email;
contactMod.value = contact;
// idMod.value = user_id;
});
}
// code for Modal form update button.
var updateBtn = document.getElementById("update");
// async function updateUser()
updateBtn.addEventListener("click", async function()
{
console.log(postKey)
//setting dateTime for updated_at attribute in users table
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + today.getMilliseconds();
var dateTime = date+' '+time;
await setDoc(doc(db, "users", postKey.toString()), {
created_at: created_at,
email: emailMod.value,
id: user_id,
password_digest: password_digest,
updated_at: dateTime,
user_ContactNo: contactMod.value,
user_FirstName: firstMod.value,
user_LastName: lastMod.value,
user_Type: user_Type,
user_profile_photo : user_profile_photo,
Student_Number: student
})
.then(() =>{
alert("User Updated Succefully");
window.location.reload(true); //reload window after closing alert
})
.catch((error)=>{
alert("Unsuccesful. Please Try Again." + error)
});
});
async function deleteUser(postKey)
{
await deleteDoc(doc(db, "users", postKey.toString()))
.then(() =>{
alert("User Deleted Succefully");
window.location.reload(true); //reload window after closing alert
})
.catch((error)=>{
alert("Unsuccesful. Please Try Again." + error)
});
window.location.reload(true);
await deleteDoc(doc(db, "students", postKey.toString()))
.then(() =>{
alert("User Deleted Succefully");
window.location.reload(true); //reload window after closing alert
})
.catch((error)=>{
alert("Unsuccesful. Please Try Again." + error)
});
window.location.reload(true);
}
// code for Modal Form delete button
var del = document.getElementById('remove');
del.addEventListener("click", function()
{
deleteUser(postKey);
});
</script>
<script>
var sign_out = document.getElementById("out");
sign_out.addEventListener("click", function(){
document.location.href = "index.html";
});
var home_element = document.getElementById("home");
home_element.addEventListener("click", function(){
document.location.href = "home.html";
});
var add = document.getElementById("add");
add.addEventListener("click", function(){
document.location.href = "add_users.html";
});
</script>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>