-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
78 lines (69 loc) · 1.95 KB
/
script.js
File metadata and controls
78 lines (69 loc) · 1.95 KB
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
document.getElementById('entry-or-exit').addEventListener('change', function() {
var destinationLabel = document.getElementById('destination-label');
var destinationInput = document.getElementById('destination');
if (this.value === 'entry') {
destinationLabel.style.display = 'none';
destinationInput.style.display = 'none';
} else {
destinationLabel.style.display = 'block';
destinationInput.style.display = 'block';
}
});
const form = document.getElementById('entry-form');
const studentList = document.getElementById('student-list');
form.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(form);
const studentId = formData.get('student-id');
const entryOrExit = formData.get('entry-or-exit');
let destination = ""
if(entryOrExit == "exit"){
destination = formData.get('destination');
}
try {
if(entryOrExit == "exit"){
const response = await fetch('http://localhost:3000/addEntry', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
studentId,
destination
})
});
if (!response.ok) {
throw new Error('Failed to add entry');
}
form.reset();
getStudents();
}
else{
const response = await fetch('http://localhost:3000/changeEntry', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
studentId
})
});
if (!response.ok) {
throw new Error('Failed to change entry');
}
form.reset();
getStudents();
}
}
catch (error) {
console.error(error);
}
});
const getStudents = async () => {
try {
const response = await fetch('http://localhost:3000/getStudents');
const data = await response.json();
} catch (error) {
console.error(error);
}
};