-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
150 lines (139 loc) · 4.92 KB
/
scripts.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
document.addEventListener('DOMContentLoaded', () => {
loadBooks();
loadStudents();
fetchCurrentTime();
});
const addBookForm = document.getElementById('addBookForm');
const bookList = document.getElementById('bookList');
const searchButton = document.getElementById('searchButton');
const searchQuery = document.getElementById('searchQuery');
const enterStudentForm = document.getElementById('enterStudentForm');
const leaveStudentForm = document.getElementById('leaveStudentForm');
const studentList = document.getElementById('studentList');
const issueBookForm = document.getElementById('issueBookForm');
const returnBookForm = document.getElementById('returnBookForm');
const currentTimeElement = document.getElementById('currentTime');
addBookForm.addEventListener('submit', (event) => {
event.preventDefault();
const name = document.getElementById('bookName').value;
const author = document.getElementById('authorName').value;
const id = document.getElementById('bookId').value;
fetch('/add_book', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, author, id })
}).then(() => {
addBookForm.reset();
loadBooks();
});
});
function loadBooks() {
fetch('/books')
.then(response => response.json())
.then(books => {
bookList.innerHTML = '';
books.forEach(book => {
const li = document.createElement('li');
li.textContent = `${book.name} by ${book.author} (ID: ${book.id}, Status: ${book.status})`;
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', () => {
removeBook(book.id);
});
li.appendChild(removeButton);
bookList.appendChild(li);
});
});
}
function removeBook(id) {
fetch('/remove_book', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id })
}).then(() => {
loadBooks();
});
}
searchButton.addEventListener('click', () => {
const query = searchQuery.value.toLowerCase();
fetch('/books')
.then(response => response.json())
.then(books => {
bookList.innerHTML = '';
books.forEach(book => {
if (book.name.toLowerCase().includes(query) || book.author.toLowerCase().includes(query)) {
const li = document.createElement('li');
li.textContent = `${book.name} by ${book.author} (ID: ${book.id}, Status: ${book.status})`;
bookList.appendChild(li);
}
});
});
});
enterStudentForm.addEventListener('submit', (event) => {
event.preventDefault();
const name = document.getElementById('studentName').value;
fetch('/add_student', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name })
}).then(() => {
enterStudentForm.reset();
loadStudents();
});
});
leaveStudentForm.addEventListener('submit', (event) => {
event.preventDefault();
const name = document.getElementById('studentNameLeave').value;
fetch('/remove_student', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name })
}).then(() => {
leaveStudentForm.reset();
loadStudents();
});
});
function loadStudents() {
fetch('/students')
.then(response => response.json())
.then(students => {
studentList.innerHTML = '';
students.forEach(student => {
const li = document.createElement('li');
li.textContent = student;
studentList.appendChild(li);
});
});
}
issueBookForm.addEventListener('submit', (event) => {
event.preventDefault();
const id = document.getElementById('issueBookId').value;
const student = document.getElementById('issueStudentName').value;
fetch('/issue_book', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id, student })
}).then(() => {
issueBookForm.reset();
loadBooks();
});
});
returnBookForm.addEventListener('submit', (event) => {
event.preventDefault();
const id = document.getElementById('returnBookId').value;
fetch('/return_book', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id })
}).then(() => {
returnBookForm.reset();
loadBooks();
});
});
function fetchCurrentTime() {
fetch('/current_time')
.then(response => response.json())
.then(data => {
currentTimeElement.textContent = `Current Time: ${data.current_time}`;
});
}