-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_users.html
214 lines (184 loc) · 9.21 KB
/
add_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
<!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, shrink-to-fit=no">
<title>Add New Protocol</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">
<!-- <li class="nav-item" role="presentation"><a class="nav-link active" href="home.html">Home</a></li> -->
<!-- <li class="nav-item" role="presentation"><a class="nav-link active" href="index.html">Logout</a></li> -->
<!-- <li class="nav-item" role="presentation"><a class="nav-link" href="login.html">Dashboards</a></li>
<li class="nav-item" role="presentation"><a class="nav-link" href="register.html">Schedules</a></li> -->
</ul>
<button id = "return" class="btn btn-secondary" type="button" href="users.html">Users</button>
<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>
<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">Add New User</h3>
</div>
<form id = "form" class="form-signin">
<!-- Form attributes -->
First Name: <input type="text" placeholder="First Name: " id="first" required=""><br /><br />
Last Name: <input type="text" placeholder="Last Name: " id="last" required=""><br /><br />
Email: <input type="text" placeholder="Email: " id="email" required=""><br /><br />
Password: <input type="text" placeholder="Password: " id="password" required=""><br /><br />
Contact No: <input type="text" placeholder="Contact No: " id="contact" required=""><br /><br />
Student No: <input type="text" placeholder="Contact No: " id="student" required=""><br /><br />
<!-- ID: <input type="text" placeholder="ID: " id="user_id"><br /><br /> -->
<button id="saveButton" href="#" class="btn btn-sm btn-success btn-create" >Submit</button>
</form>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.all.min.js"></script>
<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 { getAnalytics } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-analytics.js";
import { collection,getFirestore, doc, getDocs, setDoc } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-firestore.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
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"
};
// Initialize Firebase and variables
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getFirestore();
//create date and time variables
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;
var newPostKey; // initialise variable to store firestore doc name
var id; //initialise variable to store user id
//add event listener to form that allows us to add protocols and descriptions to firebase
//code to get the last key in the database, this allows us to increment the post key and id.
var keys = [];
const querySnapshot = await getDocs(collection(db, "users"));
querySnapshot.forEach((doc) => {
var user_id = doc.data().id;
var key = user_id - 1;
keys.push(key);
});
//get value of last postKey which will the value of last id-1
var post_keys = keys.sort(function(a, b){return a - b;});
console.log(post_keys)
var lastKey = post_keys[keys.length -1];
console.log(lastKey);
//set new Post Key and id
newPostKey = lastKey + 1;
console.log(newPostKey);
var PostKey = newPostKey.toString() //convert to string to allow function to work
id = newPostKey + 1;
console.log(id);
console.log(typeof PostKey);
console.log(PostKey)
form.addEventListener("submit", (event) =>{
event.preventDefault();
addUser(); //call add users function
addStudent(); // call add student function
console.log("User Added successfully");
//toast message to tell admin that new user has been added
Swal.fire({
toast: true,
icon: 'success',
title: 'User added successfully',
animation: false,
position: 'center',
showConfirmButton: false,
timer: 1000,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
})
form.reset(); //clear form
});
//function to add user input to firestore database
async function addUser()
{
//function that add user to user collection
var submit = document.getElementById("saveButton").value;
var first_name = document.getElementById("first").value;
var last_name = document.getElementById("last").value;
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var contact = document.getElementById("contact").value;
var student = document.getElementById("student").value;
await setDoc(doc(db, "users", PostKey), {
created_at: dateTime,
email: email,
id: id,
password_digest: password,
updated_at: dateTime,
user_ContactNo: contact,
user_FirstName: first_name,
user_LastName: last_name,
user_Type: "Student", //all users being added are students
user_profile_photo : "https://www.business2community.com/wp-content/uploads/2017/08/blank-profile-picture-973460_640.png", //set default profile picture
Student_Number: student
});
}
async function addStudent()
{
//function that adds student to student collection
var student = document.getElementById("student").value;
await setDoc(doc(db, "students", PostKey), {
courseCodes: "",
created_at: dateTime,
id: id,
user_id: id,
programme_id: 1,
updated_at: dateTime,
studentNumber: student
});
}
</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 back = document.getElementById("return");
back.addEventListener("click", function(){
document.location.href = "users.html";
});
</script>
</body>
</html>