-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
185 lines (154 loc) · 5.6 KB
/
main.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
const result = document.getElementById("result");
const url = "http://localhost:3000/data";
const xhr = new XMLHttpRequest();
// Create currency formatter
const currency = new Intl.NumberFormat('id-ID', {
style: 'currency',
currency: 'IDR',
});
// GET All
function fetchData() {
xhr.onerror = function () {
alert("Error!")
}
xhr.onloadstart = function () {
result.innerHTML = "Loading..";
}
xhr.onloadend = function () {
result.innerHTML = "";
const data = JSON.parse(this.response);
for (let i = 0; i < data.length; i++) {
const row = document.createElement("tr");
row.innerHTML = `
<td>${data[i].id}</td>
<td>${data[i].title}</td>
<td>${currency.format(data[i].price)}</td>
<td>
<img style="height: 75px;" src="${data[i].img}">
</td>
<td>
<button class="btn btn-success pb-2" data-bs-toggle="modal"
data-bs-target="#detailModal" onclick="fetchDetail(${data[i].id})">
<img style="height: 15px;" src="assets/eye.svg">
</button>
<button class="btn btn-warning pb-2" data-bs-toggle="modal"
data-bs-target="#editBook" onclick="editItem(${data[i].id})">
<img style="height: 15px;" src="assets/pen.svg">
</button>
<button class="btn btn-danger pb-2" onclick="deleteData(${data[i].id})">
<img style="height: 15px;" src="assets/trash.svg">
</button>
</td>
`
result.appendChild(row);
}
}
xhr.onprogress = function () {
result.innerHTML = "Loading..";
}
xhr.open("GET", url);
xhr.send();
}
// EDIT
function editItem(id) {
console.log(id);
const item = document.getElementById('item');
xhr.onload = function () {
console.log(this);
item.innerHTML = ""
const data = JSON.parse(this.response);
item.innerHTML = `
<div class="modal-header">
<h5 class="modal-title" id="editBookLabel">Edit Buku</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body" >
<form class="mb-3">
<div class="mb-3">
<label for="title" class="form-label">Judul Buku</label>
<input type="text" class="form-control" id="editTitle" value="${data[id - 1].title}">
</div>
<div class="mb-3">
<label for="price" class "form-label">Harga</label>
<input type="text" class="form-control" id="editPrice" value="${data[id - 1].price}">
</div>
<div class="mb-3">
<label for="img" class="form-label">Gambar</label>
<input type="text" class="form-control" id="editImg" value="${data[id - 1].img}">
</div>
<div class="mb-3">
<label for="desc" class="form-label">Deskripsi</label>
<textarea class="form-control" rows="5" id="editDesc">${data[id - 1].description}</textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
<!-- saveChanges() -->
<button onclick="saveChanges(${id})" type="submit" class="btn btn-primary">Simpan</button>
</div>
`
}
xhr.open("GET", url);
xhr.send();
}
function saveChanges(id) {
const data = JSON.stringify({
title: document.getElementById("editTitle").value,
price: document.getElementById("editPrice").value,
img: document.getElementById("editImg").value,
description: document.getElementById("editDesc").value
});
xhr.open("PUT", url + `/${id}`);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(data);
}
// POST
function postData(event) {
event.preventDefault();
const data = JSON.stringify({
title: document.getElementById("title").value,
price: document.getElementById("price").value,
img: document.getElementById("img").value,
description: document.getElementById("desc").value
});
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onload = function () {
console.log(this.responseText);
};
xhr.send(data);
}
// DELETE
function deleteData(id) {
xhr.open("DELETE", url + `/${id}`);
xhr.send();
}
// replace \n menjadi <br> untuk description
function newBr(inputString) {
return inputString.replace(/\n/g, '<br>');
}
// GET BY ID
function fetchDetail(id) {
xhr.onload = function () {
result.innerHTML = "";
const data = JSON.parse(this.response);
const info = document.getElementById("info");
info.innerHTML = `
<img style="width: 200px;" src="${data[id - 1].img}">
<div style="margin-left: 15px;">
<h5>${data[id - 1].title}</h5>
<h6>${currency.format(data[id - 1].price)}</h6>
<p class="fw-bold mb-1">Deskripsi</p>
<p>${newBr(data[id - 1].description)}</p>
</div>`;
}
xhr.onloadstart = function () {
info.innerHTML = "Loading..";
}
xhr.onprogress = function () {
info.innerHTML = "Loading..";
}
xhr.open("GET", url);
xhr.send();
}