Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
289 lines (251 loc) · 10.1 KB
/
Copy pathindex.js
File metadata and controls
289 lines (251 loc) · 10.1 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
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
let currentPage = 'home';
let currentBook = null;
let books = [];
const main = document.querySelector('main');
const pageListMainContent = `<h2 class="text-2xl font-bold mb-4">Daftar Buku Perpustakaan</h2>
<table class="min-w-full border border-gray-300">
<thead>
<tr>
<th class="px-6 py-3 bg-gray-100 border-b text-left">Judul</th>
<th class="px-6 py-3 bg-gray-100 border-b text-left">Penulis</th>
<th class="px-6 py-3 bg-gray-100 border-b text-left">Tahun Terbit</th>
<th class="px-6 py-3 bg-gray-100 border-b text-left">Jumlah</th>
<th class="px-6 py-3 bg-gray-100 border-b text-center">Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>`;
const pageEditBookMainContent = `<h2 class="text-2xl font-bold mb-4">Edit Buku</h2>
<form class="max-w-sm mx-auto" onsubmit="return handleEditForm(event)">
</form>
`;
const pageAddBookMainContent = `<h2 class="text-2xl font-bold mb-4">Tambah Buku</h2>
<form class="max-w-sm mx-auto" onsubmit="return handleAddForm(event)">
<div class="mb-4">
<label for="title" class="block text-gray-700 font-semibold mb-2">Judul Buku</label>
<input required type="text" id="title" name="title" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:border-blue-500" />
</div>
<div class="mb-4">
<label for="author" class="block text-gray-700 font-semibold mb-2">Penulis Buku</label>
<input required type="text" id="author" name="author" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:border-blue-500" />
</div>
<div class="mb-4">
<label for="year" class="block text-gray-700 font-semibold mb-2">Tahun Terbit</label>
<input required type="number" id="year" name="year" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:border-blue-500" />
</div>
<div class="mb-4">
<label for="quantity" class="block text-gray-700 font-semibold mb-2">Jumlah Stok</label>
<input required type="number" id="quantity" name="quantity" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:border-blue-500" />
</div>
<div class="flex justify-center">
<input type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" value="Tambah Buku" />
</div>
</form>
`;
async function handleClickEditButton(bookId) {
try {
// Ambil data buku dari server berdasarkan id, simpan hasilnya ke variabel currentBook
const response = await fetch(`http://localhost:3333/books/${bookId}`)
currentBook = await response.json();
currentPage = 'edit';
loadPage();
} catch (error) {
console.log(error);
console.log('Terjadi kesalahan saat mengambil data buku');
}
}
async function handleClickDeleteButton(bookId) {
try {
//panggil function deleteBook dengan parameter bookId
await deleteBook(bookId);
loadPage();
} catch (error) {
console.log(error);
console.log('Terjadi kesalahan saat menghapus buku');
}
}
async function handleEditForm(event) {
try {
// gunakan preventDefault untuk mencegah browser melakukan reload halaman
event.preventDefault()
//Ambil data dari form, simpan ke dalam variabel book
const book = {
id : currentBook.id,
title: document.getElementById('title').value,
author: document.getElementById('author').value,
year: document.getElementById('year').value,
quantity: document.getElementById('quantity').value,
}
// panggil function editBook dengan parameter book
await editBook(book)
currentBook = null;
currentPage = 'home';
loadPage();
} catch (error) {
console.log(error);
console.log('Terjadi kesalahan saat mengubah buku');
}
}
async function handleAddForm(event) {
try {
// gunakan preventDefault untuk mencegah browser melakukan reload halaman
event.preventDefault()
//Ambil data dari form, simpan ke dalam variabel book
const book = {
title: document.getElementById('title').value,
author: document.getElementById('author').value,
year: document.getElementById('year').value,
quantity: document.getElementById('quantity').value,
}
// panggil function addBook dengan parameter book
await addBook(book)
currentPage = 'home';
loadPage();
} catch (error) {
console.log(error);
console.log('Terjadi kesalahan saat menambah buku');
}
}
function handleClickAddNav() {
// ubah currentPage menjadi 'add'
currentPage = 'add'
loadPage();
}
// add event listener click tag a didalam li dengan function handleClickAddNav
const navLinks = document.querySelectorAll('li a');
navLinks.forEach((navLink) => {
navLink.addEventListener('click', handleClickAddNav)
});
function generateRows(books) {
let rows = '';
if (books.length === 0) {
rows = `<tr>
<td colspan="6" class="px-6 py-4 border-b text-center">Tidak ada buku yang ditemukan</td>
</tr>`;
} else {
//Jangan lupa untuk ganti BookId dengan id dari book yang sedang di looping simpan row yang dibuat ke variabel rows
books.forEach((book) => {
rows += `
<tr class="book-item">
<td class="px-6 py-4 border-b">${book.title}</td>
<td class="px-6 py-4 border-b">${book.author}</td>
<td class="px-6 py-4 border-b">${book.year}</td>
<td class="px-6 py-4 border-b">${book.quantity}</td>
<td class="px-6 py-4 border-b text-center">
<button class="inline-block bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" onclick="handleClickEditButton(${book.id})">Edit</button>
<button class="inline-block bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded" onclick="handleClickDeleteButton(${book.id})">Hapus</button>
</td>
</tr>
`
})
}
return rows;
}
function generateEditFormInput() {
return `<div class="mb-4">
<label for="title" class="block text-gray-700 font-semibold mb-2">Judul Buku</label>
<input required type="text" id="title" name="title" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:border-blue-500" value="${currentBook?.title}">
</div>
<div class="mb-4">
<label for="author" class="block text-gray-700 font-semibold mb-2">Penulis Buku</label>
<input required type="text" id="author" name="author" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:border-blue-500" value="${currentBook?.author}">
</div>
<div class="mb-4">
<label for="year" class="block text-gray-700 font-semibold mb-2">Tahun Terbit</label>
<input required type="number" id="year" name="year" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:border-blue-500" value="${currentBook?.year}">
</div>
<div class="mb-4">
<label for="quantity" class="block text-gray-700 font-semibold mb-2">Jumlah Stok</label>
<input required type="number" id="quantity" name="quantity" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:border-blue-500" value="${currentBook?.quantity}">
</div>
<div class="flex justify-center">
<input type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" value="simpan" />
</div>`;
}
async function loadPage() {
switch (currentPage) {
case 'home':
// panggil function fetchBooks
await fetchBooks();
main.innerHTML = pageListMainContent;
const tableBody = document.querySelector('tbody');
//panggil function generateRows dengan parameter books dan simpan hasilnya ke variabel rows kemudian isi innerHTML dari tableBody dengan rows
const rows = generateRows(books);
tableBody.innerHTML = rows;
break;
case 'edit':
main.innerHTML = pageEditBookMainContent;
const form = document.querySelector('form');
//panggil function generateEditFormInput dan simpan hasilnya ke variabel formInput kemudian isi innerHTML dari form dengan formInput
const formInput = generateEditFormInput();
form.innerHTML = formInput;
break;
case 'add':
main.innerHTML = pageAddBookMainContent;
break;
}
}
async function fetchBooks() {
try {
/*
fetch data buku dari http://localhost:3333/books
simpan hasilnya ke variabel global books
*/
// TODO: answer here
const response = await fetch('http://localhost:3333/books');
const booksData = await response.json()
books = booksData;
} catch (error) {
console.log(error);
console.log('Terjadi kesalahan saat mengambil data buku');
}
}
async function addBook(book) {
try {
//tambahkan buku baru ke http://localhost:3333/books dengan method POST body yang dikirim adalah book yang dikirimkan sebagai parameter function
const response = await fetch('http://localhost:3333/books',{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(book)
});
const newBook = await response.json();
return newBook;
} catch (error) {
console.log(error);
console.log('Terjadi kesalahan saat menambah buku');
}
}
async function editBook(book) {
try {
//ubah buku yang ada di http://localhost:3333/books/:id dengan method PUT body yang dikirim adalah book yang dikirimkan sebagai parameter function
const response = await fetch(`http://localhost:3333/books/${book.id}`,{
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(book)
});
const updateBook = await response.json();
return updateBook;
} catch (error) {
console.log(error);
console.log('Terjadi kesalahan saat mengubah buku');
}
}
async function deleteBook(bookId) {
try {
//hapus buku yang ada di http://localhost:3333/books/:id dengan method DELETE id buku yang akan dihapus dikirimkan sebagai parameter function
const response = await fetch(`http://localhost:3333/books/${bookId}`,{
method: 'DELETE',
});
const removeBook = await response.json();
return removeBook;
} catch (error) {
console.log(error);
console.log('Terjadi kesalahan saat menghapus buku');
}
}
loadPage();