-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (173 loc) · 7.61 KB
/
index.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
// function loadTable() {
// const xhttp = new XMLHttpRequest();
// xhttp.open("GET", "https://www.mecallapi.com/api/users");
// xhttp.send();
// xhttp.onreadystatechange = function() {
// if (this.readyState == 4 && this.status == 200) {
// console.log(this.responseText);
// var trHTML = '';
// const objects = JSON.parse(this.responseText);
// for (let object of objects) {
// trHTML += '<tr>';
// trHTML += '<td>'+object['id']+'</td>';
// trHTML += '<td><img width="50px" src="'+object['avatar']+'" class="avatar"></td>';
// trHTML += '<td>'+object['fname']+'</td>';
// trHTML += '<td>'+object['lname']+'</td>';
// trHTML += '<td>'+object['username']+'</td>';
// trHTML += '<td><button type="button" class="btn btn-outline-secondary" onclick="showUserEditBox('+object['id']+')">Edit</button>';
// trHTML += '<button type="button" class="btn btn-outline-danger" onclick="userDelete('+object['id']+')">Del</button></td>';
// trHTML += "</tr>";
// }
// document.getElementById("mytable").innerHTML = trHTML;
// }
// };
// }
// loadTable();
function loadTable() {
const xhttp = new XMLHttpRequest();
xhttp.open("GET", "https://4dqirdobsf.execute-api.us-east-1.amazonaws.com/prod/books");
xhttp.send();
console.log(this.responseText);
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(
this.responseText);
var trHTML = '';
const objects = JSON.parse(this.responseText);
for (let object of objects) {
trHTML += '<tr>';
trHTML += '<td><img width="80px" src="'+object['image']+'" class="avatar"></td>';
trHTML += '<td>'+object['name']+'</td>';
trHTML += '<td>'+object['author']+'</td>';
trHTML += '<td>'+object['price']+'</td>';
trHTML += '<td>'+object['category']+'</td>';
trHTML += '<td>'+object['bookISBN']+'</td>';
trHTML += '<td><button type="button" class="btn btn-outline-danger" onclick="userDelete()"><img src="https://img.icons8.com/color/36/000000/mouse-left-click.png"/></button></td>';
// trHTML += '<td class="fav"> <img class="white-star" src="https://i.postimg.cc/g0b9JG0w/unfav.png" /></td>';
// trHTML += '<td class="fav"><img class="yellow-star hide" src="https://i.postimg.cc/QN1T9bSH/fav.png" /></td>';
trHTML += '<td> <img alt="" onclick="ShowUndo()" style="height:50px;" src="https://i.postimg.cc/g0b9JG0w/unfav.png" id="image" /></td>';
trHTML += "</tr>";
}
document.getElementById("mytable").innerHTML = trHTML;
}
};
}
loadTable();
function showUserCreateBox() {
Swal.fire({
title: 'Add New Book to your collection',
html:
'<input id="id" type="hidden">' +
'<input id="image" class="swal2-input" placeholder="image">' +
'<input id="author" class="swal2-input" placeholder="author">' +
'<input id="name" class="swal2-input" placeholder="name">' +
'<input id="price" class="swal2-input" placeholder="price">' +
'<input id="category" class="swal2-input" placeholder="category">' +
'<input id="bookISBN" class="swal2-input" placeholder="bookISBN">',
focusConfirm: false,
preConfirm: () => {
userCreate();
}
})
}
function showBookInformation() {
Swal.fire({
title: 'Add New Book to your collection',
html:
'<input id="id" type="hidden">' +
'<input id="image" class="swal2-input" placeholder="image">' +
'<input id="author" class="swal2-input" placeholder="author">' +
'<input id="name" class="swal2-input" placeholder="name">' +
'<input id="price" class="swal2-input" placeholder="price">' +
'<input id="category" class="swal2-input" placeholder="category">' +
'<input id="bookISBN" class="swal2-input" placeholder="bookISBN">',
focusConfirm: false,
preConfirm: () => {
userCreate();
}
})
}
function userCreate() {
const image = document.getElementById("image").value;
const author = document.getElementById("author").value;
const name = document.getElementById("name").value;
const price = document.getElementById("price").value;
const category = document.getElementById("category").value;
const bookISBN = document.getElementById("bookISBN").value;
const xhttp = new XMLHttpRequest();
xhttp.open("POST", "https://4dqirdobsf.execute-api.us-east-1.amazonaws.com/prod/addbooks");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({
"image": image, "name": name, "category": category, "bookISBN": bookISBN, "author": author, "price": price,
"avatar": "https://www.mecallapi.com/users/cat.png"
}));
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const objects = JSON.parse(this.responseText);
Swal.fire(objects['message']);
loadTable();
}
};
}
function userDelete() {
const xhttp = new XMLHttpRequest();
xhttp.open("DELETE", "https://wggt89lje6.execute-api.us-east-1.amazonaws.com/prod/getcars");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({
"bookISBN": bookISBN
}));
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
const objects = JSON.parse(this.responseText);
Swal.fire(objects['message']);
loadTable();
}
};
}
function showUserEditBox(bookISBN) {
console.log(bookISBN);
const xhttp = new XMLHttpRequest();
xhttp.open("GET", "https://wggt89lje6.execute-api.us-east-1.amazonaws.com/prod/getcars/"+bookISBN);
xhttp.send();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const objects = JSON.parse(this.responseText);
const user = objects['user'];
console.log(user);
Swal.fire({
title: 'Edit Book',
html:
'<input id="id" type="hidden" value='+user['bookISBN']+'>' +
'<input id="name" class="swal2-input" placeholder="First" value="'+user['name']+'">' +
'<input id="image" class="swal2-input" placeholder="Last" value="'+user['image']+'">' +
'<input id="category" class="swal2-input" placeholder="Username" value="'+user['category']+'">',
// '<input id="email" class="swal2-input" placeholder="Email" value="'+user['email']+'">',
focusConfirm: false,
preConfirm: () => {
userEdit();
}
})
}
};
}
function userEdit() {
const bookISBN = document.getElementById("bookISBN").value;
const name = document.getElementById("name").value;
const category = document.getElementById("category").value;
const image = document.getElementById("image").value;
// const email = document.getElementById("email").value;
const xhttp = new XMLHttpRequest();
xhttp.open("PUT", "https://www.mecallapi.com/api/users/update");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({
"id": id, "fname": fname, "lname": lname, "username": username, "email": email,
"avatar": "https://www.mecallapi.com/users/cat.png"
}));
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const objects = JSON.parse(this.responseText);
Swal.fire(objects['message']);
loadTable();
}
};
}