-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
395 lines (307 loc) · 11 KB
/
script.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
const students = [];
const teachers = [];
async function loaded(){
document.getElementById("select").addEventListener("change", select_func, false);
document.getElementById("form").addEventListener("submit", (e) => {
e.preventDefault()
document.getElementsByClassName("alert")[0].classList.add("hide");
const form = document.getElementById("form");
const data = new FormData(form);
const [role, name, age, subject] = [...data.values()]
if (!name.trim() || !age.trim()){
document.getElementsByClassName("alert")[0].classList.remove("hide");
return;
}
if (role === "student"){
addStudentToFront(role, name, age)
};
if (role === "teacher") {
addTeacherToFront(role, name, age, subject)
};
}, false);
await fetch("http://127.0.0.1:8000/students/v1/get-students")
.then(res => res.json())
.then(data => localStorage.setItem("Students" ,JSON.stringify(data)))
.catch((e) => console.log("no data on localStorage"))
await fetch("http://127.0.0.1:8000/teachers/v1/get-teachers")
.then(res => res.json())
.then(data => localStorage.setItem("Teachers", JSON.stringify(data)))
.catch((e) => console.log("no data on localStorage"))
showData()
}
async function addStudentToFront(role, name, age){
const object = await addInDatabase(role, name, age);
const student = new Student(name, age, object.id);
students.push(student);
Person.showPersonUI(students, "Students");
}
async function addTeacherToFront(role, name, age, subject){
const object = await addInDatabase(role, name, age, subject);
const teacher = new Teacher(name, age, object.id, subject);
teachers.push(teacher);
Person.showPersonUI(teachers, "Teachers")
}
async function addInDatabase(role, name, age, subject = "Math"){
if (role === "student") {
const data = {
"id": "",
"name": name,
"age": age,
"grades": [],
"passed": true
}
const response = await fetch("http://127.0.0.1:8000/students/v1/post-student", {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error("Network response was not ok.");
}
const responseData = await response.json();
return responseData;
};
if (role === "teacher"){
const data = {
"id": "",
"name": name,
"age": age,
"subject": subject
}
const response = await fetch("http://127.0.0.1:8000/teachers/v1/post-teacher", {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
},
});
if (!response.ok) {
throw new Error("Network response was not ok.");
}
const responseData = await response.json();
return responseData;
}
}
const select_func = (event) => {
const input = document.getElementById("subject");
if (event.target.selectedIndex === 1) {
input.classList.add("hide");
} else if (event.target.selectedIndex === 0){
input.classList.remove("hide");
}
input.toggleAttribute("required");
}
const showData = () => {
const students_data = JSON.parse(localStorage.getItem("Students"));
const teachers_data = JSON.parse(localStorage.getItem("Teachers"));
students_data.forEach(element => {
students.push(new Student(element.name, element.age, element.id, element.grades, element.passed))
});
Person.showPersonUI(students, "Students");
teachers_data.forEach(element => {
teachers.push(new Teacher(element.name, element.age, element.id, element.subject));
});
Person.showPersonUI(teachers, "Teachers");
}
const deleteTeacher = (buttonDelete) => {
const teacher = buttonDelete.target.parentNode.parentNode;
teacher.parentNode.removeChild(teacher);
deleteInDatabase(0, buttonDelete)
}
const deleteStudent = (buttonDelete) => {
const student = buttonDelete.target.parentNode.parentNode.parentNode.parentNode;
student.parentNode.removeChild(student);
deleteInDatabase(1,buttonDelete)
}
async function statusStudent(button){
const body_t = {
"id": button.target.dataset.uid,
"passed": button.target.dataset.passed === "true"
};
await fetch("http://127.0.0.1:8000/students/v1/update-passed", {
method: "PATCH",
body: JSON.stringify(body_t),
headers: {
"Content-Type": "application/json",
}
});
students.map(item => {
if (item.uid === body_t.id){
item.setStatus = button.target.dataset.passed === "true";
}
});
Person.showPersonUI(students, "Students");
}
async function addGrades(id){
let grade = "";
let grades = [];
await Swal.fire({
title: "Insert a grade for the student",
input: "text",
inputAttributes: {
autocapitalize: "off"
},
showCancelButton: true,
confirmButtonText: "Confirm",
showLoaderOnConfirm: true,
preConfirm: async (grade) => {
try {
return parseFloat(grade);
} catch (error) {
Swal.showValidationMessage(`
failed: ${error}
`);
}
},
allowOutsideClick: () => !Swal.isLoading()
}).then(res => grade = res);
if (isNaN(grade.value) || grade.value === ""){
return;
}
students.map(item => {
if (item.uid === id){
item.setGrade = grade.value;
grades = item.getGrades;
}
});
console.log(grades);
await fetch("http://127.0.0.1:8000/students/v1/update-grades", {
method: "PATCH",
body: JSON.stringify({
"id": id,
"grades": grades
}),
headers: {
"Content-Type": "application/json",
}
});
Person.showPersonUI(students, "Students");
}
async function deleteInDatabase(role, buttonDelete) {
if(role === 0){
await fetch(`http://127.0.0.1:8000/teachers/v1/delete_teacher?id=${buttonDelete.target.dataset.uid}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
}
})
teachers.forEach((e,i) => {
if (e.uid === buttonDelete.target.dataset.uid){
teachers.splice(i,1);
}
})
} else if (role === 1){
await fetch(`http://127.0.0.1:8000/students/v1/delete-student?id=${buttonDelete.target.dataset.uid}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
}
})
students.forEach((e,i) => {
if (e.uid === buttonDelete.target.dataset.uid){
students.splice(i,1);
}
})
}
}
class Person{
constructor(name, age, uid){
this.name = name;
this.age = age;
this.uid = uid;
}
static showPersonUI(persons, tipe){
if (tipe === "Students"){
const students = document.getElementsByClassName("students")[0];
students.textContent = "";
const fragment = document.createDocumentFragment()
persons.forEach(item => {
fragment.appendChild(item.addNewStudent());
})
students.appendChild(fragment);
};
if (tipe === "Teachers"){
const teachers = document.getElementsByClassName("teachers")[0];
teachers.textContent = "";
const fragment = document.createDocumentFragment();
persons.forEach(item => {
fragment.appendChild(item.addNewTeacher());
})
teachers.appendChild(fragment);
}
}
}
class Student extends Person {
#passed = true;
#grades = [];
constructor(name, age, uid, grades = [], passed = true){
super(name, age);
this.uid = uid;
this.#grades = grades;
this.#passed = passed;
}
set setStatus(passed){
this.#passed = passed;
}
set setGrade(grade){
this.#grades.push(grade);
}
get getGrades(){
return this.#grades;
}
addNewStudent(){
let prom = ""
if (this.#grades.length != 0){
prom = (this.#grades.reduce((redx, item) => item + redx) / this.#grades.length).toFixed(2);
}
const template = document.getElementById("student-template");
const clone = template.content.firstElementChild.cloneNode(true);
clone.querySelector(".pass-fail h3 .name").textContent = this.name;
clone.querySelector(".age").textContent = this.age;
clone.querySelector(".grades").textContent = "Grades: " + this.#grades.join(", ");
clone.querySelector(".prom").textContent = "PROM: " + prom;
if (this.#passed){
clone.querySelector(".pass-fail h3 .passed").style.background = "#18AA25";
clone.querySelector(".pass-fail h3 .name").style.color = "#18AA25";
} else {
clone.querySelector(".pass-fail h3 .passed").style.background = "#c2271c";
clone.querySelector(".pass-fail h3 .name").style.color = "#c2271c";
}
clone.querySelector(".pass-fail h3 .passed").textContent = this.#passed ? "Passed": "Failed";
const buttonDelete = clone.querySelector(".delete-student");
buttonDelete.addEventListener("click", deleteStudent, false);
buttonDelete.dataset.uid = this.uid;
const buttonPass = clone.querySelector(".pass");
buttonPass.dataset.uid = this.uid;
buttonPass.addEventListener("click", statusStudent, false);
buttonPass.disabled = this.#passed;
const buttonFail = clone.querySelector(".fail");
buttonFail.dataset.uid = this.uid;
buttonFail.addEventListener("click", statusStudent, false);
buttonFail.disabled = !this.#passed;
const buttonAdd = clone.querySelector(".add-grade");
buttonAdd.dataset.uid = this.uid;
buttonAdd.addEventListener("click", () => {addGrades(this.uid)}, false);
return clone;
}
}
class Teacher extends Person {
constructor(name, age, uid, subject = ""){
super(name, age, uid);
this.subject = subject
}
addNewTeacher(){
const template = document.getElementById("teacher-template");
const clone = template.content.firstElementChild.cloneNode(true);
clone.querySelector(".teacher h3").textContent = this.name;
clone.querySelector(".teacher #area").textContent = this.subject;
clone.querySelector(".teacher #age").textContent = this.age;
const buttonDelete = clone.querySelector(".delete-teacher");
buttonDelete.addEventListener("click", deleteTeacher, false);
buttonDelete.dataset.uid = this.uid;
return clone;
}
}
window.addEventListener("load", loaded, false);