forked from thiagoecs/nameless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
288 lines (263 loc) · 7.88 KB
/
Copy pathmain.js
File metadata and controls
288 lines (263 loc) · 7.88 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
'use strict';/*
//const url = 'http://localhost:8000'; // change url when uploading to server
// select existing html elements
const loginWrapper = document.querySelector('#login-wrapper');
const userInfo = document.querySelector('#user-info');
const logOut = document.querySelector('#log-out');
const main = document.querySelector('main');
const loginForm = document.querySelector('#login-form');
const addUserForm = document.querySelector('#add-user-form');
const addForm = document.querySelector('#add-cat-form');
const modForm = document.querySelector('#mod-cat-form');
const ul = document.querySelector('ul');
const userLists = document.querySelectorAll('.add-owner');
const imageModal = document.querySelector('#image-modal');
const modalImage = document.querySelector('#image-modal img');
const close = document.querySelector('#image-modal a');
// create cat cards
const createCatCards = (cats) => {
// clear ul
ul.innerHTML = '';
cats.forEach((cat) => {
// create li with DOM methods
const img = document.createElement('img');
img.src = url + '/thumbnails/' + cat.filename;
img.alt = cat.name;
img.classList.add('resp');
// open large image when clicking image
img.addEventListener('click', () => {
modalImage.src = url + '/' + cat.filename;
imageModal.alt = cat.name;
imageModal.classList.toggle('hide');
try {
const coords = JSON.parse(cat.coords);
// console.log(coords);
addMarker(coords);
}
catch (e) {
}
});
const figure = document.createElement('figure').appendChild(img);
const h2 = document.createElement('h2');
h2.innerHTML = cat.name;
const p1 = document.createElement('p');
p1.innerHTML = `Age: ${cat.age}`;
const p2 = document.createElement('p');
p2.innerHTML = `Weight: ${cat.weight}kg`;
const p3 = document.createElement('p');
p3.innerHTML = `Owner: ${cat.ownername}`;
// add selected cat's values to modify form
const modButton = document.createElement('button');
modButton.innerHTML = 'Modify';
modButton.addEventListener('click', () => {
const inputs = modForm.querySelectorAll('input');
inputs[0].value = cat.name;
inputs[1].value = cat.age;
inputs[2].value = cat.weight;
inputs[3].value = cat.cat_id;
modForm.querySelector('select').value = cat.owner;
});
// delete selected cat
const delButton = document.createElement('button');
delButton.innerHTML = 'Delete';
delButton.addEventListener('click', async () => {
const fetchOptions = {
method: 'DELETE',
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem('token'),
},
};
try {
const response = await fetch(url + '/cat/' + cat.cat_id, fetchOptions);
const json = await response.json();
console.log('delete response', json);
getCat();
}
catch (e) {
console.log(e.message());
}
});
const li = document.createElement('li');
li.classList.add('light-border');
li.appendChild(h2);
li.appendChild(figure);
li.appendChild(p1);
li.appendChild(p2);
li.appendChild(p3);
li.appendChild(modButton);
li.appendChild(delButton);
ul.appendChild(li);
});
};
// close modal
close.addEventListener('click', (evt) => {
evt.preventDefault();
imageModal.classList.toggle('hide');
});
// AJAX call
const getCat = async () => {
console.log('getCat token ', sessionStorage.getItem('token'));
try {
const options = {
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem('token'),
},
};
const response = await fetch(url + '/cat', options);
const cats = await response.json();
createCatCards(cats);
}
catch (e) {
console.log(e.message);
}
};
// create user options to <select>
const createUserOptions = (users) => {
userLists.forEach((list) => {
// clear user list
list.innerHTML = '';
users.forEach((user) => {
// create options with DOM methods
const option = document.createElement('option');
option.value = user.user_id;
option.innerHTML = user.name;
option.classList.add('light-border');
list.appendChild(option);
});
});
};
// get users to form options
const getUsers = async () => {
try {
const options = {
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem('token'),
},
};
const response = await fetch(url + '/user', options);
const users = await response.json();
createUserOptions(users);
}
catch (e) {
console.log(e.message);
}
};
// submit add cat form
addForm.addEventListener('submit', async (evt) => {
evt.preventDefault();
const fd = new FormData(addForm);
const fetchOptions = {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem('token'),
},
body: fd,
};
const response = await fetch(url + '/cat', fetchOptions);
const json = await response.json();
console.log('add response', json);
getCat();
});
// submit modify form
modForm.addEventListener('submit', async (evt) => {
evt.preventDefault();
const data = serializeJson(modForm);
const fetchOptions = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + sessionStorage.getItem('token'),
},
body: JSON.stringify(data),
};
console.log(fetchOptions);
const response = await fetch(url + '/cat', fetchOptions);
const json = await response.json();
console.log('modify response', json);
getCat();
});
// login
loginForm.addEventListener('submit', async (evt) => {
evt.preventDefault();
const data = serializeJson(loginForm);
const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
};
const response = await fetch(url + '/auth/login', fetchOptions);
const json = await response.json();
console.log('login response', json);
if (!json.user) {
alert(json.message);
} else {
// save token
sessionStorage.setItem('token', json.token);
// show/hide forms + cats
loginWrapper.style.display = 'none';
logOut.style.display = 'block';
main.style.display = 'block';
userInfo.innerHTML = `Hello ${json.user.name}`;
getCat();
getUsers();
}
});
// logout
logOut.addEventListener('click', async (evt) => {
evt.preventDefault();
try {
const options = {
headers: {
'Authorization': 'Bearer ' + sessionStorage.getItem('token'),
},
};
const response = await fetch(url + '/auth/logout', options);
const json = await response.json();
console.log(json);
// remove token
sessionStorage.removeItem('token');
alert('You have logged out');
// show/hide forms + cats
loginWrapper.style.display = 'flex';
logOut.style.display = 'none';
main.style.display = 'none';
}
catch (e) {
console.log(e.message);
}
});
// submit register form
addUserForm.addEventListener('submit', async (evt) => {
evt.preventDefault();
const data = serializeJson(addUserForm);
const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
};
const response = await fetch(url + '/auth/register', fetchOptions);
const json = await response.json();
console.log('user add response', json);
// save token
sessionStorage.setItem('token', json.token);
// show/hide forms + cats
loginWrapper.style.display = 'none';
logOut.style.display = 'block';
main.style.display = 'block';
userInfo.innerHTML = `Hello ${json.user.name}`;
getCat();
getUsers();
});
// when app starts, check if token exists and hide login form, show logout button and main content, get cats and users
if (sessionStorage.getItem('token')) {
loginWrapper.style.display = 'none';
logOut.style.display = 'block';
main.style.display = 'block';
getCat();
getUsers();
}
*/