-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
390 lines (371 loc) · 13.1 KB
/
script.js
File metadata and controls
390 lines (371 loc) · 13.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Veasna Bun
// add event listener for the signup-button-model
// Get the <span> element that closes the modal
var span = $(".close")[0];
// When the user clicks on the signup-model button, open the signup-modal-content
$("#signup-model").click(function () {
$(".signup-model-container").css("display", "block");
});
// When the user clicks on <span> (x), close the modal
$(span).click(function () {
$(".signup-model-container").css("display", "none");
});
// When the user clicks anywhere outside of the modal, close it
$(window).click(function (event) {
if (event.target == $(".signup-model-container")[0]) {
$(".signup-model-container").css("display", "none");
}
});
const localURL = "http://127.0.0.1:3000";
// route "/" (method:GET)
// setInterval call the server ever (set secound) sec to make sure its live
// it uses the / route to get the state of the browser
setInterval(function () {
$(document).ready(function () {
// Check if the browser is online
$.ajax({
url: `${localURL}/`,
type: 'GET',
success: function (response) {
console.log(response);
$(".created").html(response);
},
error: function (xhr, status, error) {
$(".created").html("Error: connection lost");
console.log('Request failed. Returned status of ' + xhr);
}
});
})
}, 10000); // execute every 10 second that passes
//route "/signup" (method:POST)
// when the sign-button located in the module is click
// get the value in the signup-model-container form
// send them to the route /signup to create an account
$('#signup-button').click(function (event) {
event.preventDefault();
const userData = {
username: $("#signup-username").val(),
email: $("#signup-email").val(),
password: $("#signup-password").val()
};
$.ajax({
url: `${localURL}/signup`,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(userData),
dataType: 'json',
success: function (response) {
console.log('Success:', response);
$("#alert-success-1").html(response);
$("#alert-success-1").css("display", "block");
// Fade out the success after 1 second
setTimeout(function () {
$("#alert-success-1").fadeOut();
}, 1500);
},
error: function (xhr, status, error) {
console.log('Error:', xhr);
$("#alert-fail-1").html(xhr.responseJSON.error);
$("#alert-fail-1").css("display", "block");
// Fade out the success after 1 second
setTimeout(function () {
$("#alert-fail-1").fadeOut();
}, 1500);
}
});
});
//route "/login" (method:POST)
// when the log-in button is click
// get the value in the login-container form
// send the to the route /login to get the token
// token need be used to access protect metho in the server.
// store token
var token = "";
$('#login-button').click(function (event) {
event.preventDefault();
const userData = {
username: $("#login-username").val(),
password: $("#login-password").val()
};
$.ajax({
url: `${localURL}/login`,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(userData),
dataType: 'json',
success: function (response) {
console.log(response);
token = response.message;
$("#alert-success").html("Success: login...");
$("#alert-success").css("display", "block");
// Fade out the success after 1 second
setTimeout(function () {
$("#alert-success").fadeOut();
$(".web-title_login-container_signup-model-container").fadeOut();
$(".user-nav_user-profile-container").fadeIn();
}, 1000);
getUserData();
},
error: function (xhr, status, error) {
console.log('Error:', xhr);;
$("#alert-fail").html(xhr.responseJSON.error);
$("#alert-fail").css("display", "block");
// Fade out the success after 2 second
setTimeout(function () {
$("#alert-fail").fadeOut();
}, 1000);
}
});
});
// route "/user" (method:GET)
// when the user successful login
// a type 'GET' method will need to call to the route /user
// updating the webpage to the user data
// this will be call many time from other route to keep the web updated
// 'Authorization': token will need to be set to access user data
// have to keep track of username, email, and password to access profile setting.
var trackusername = "";
var trackemail = "";
var trackpassword = "";
function getUserData() {
$.ajax({
url: `${localURL}/user`,
type: 'GET',
headers: {
'Authorization': token
},
success: function (response) {
console.log(response);
//keep track of current infor
trackusername = response.username;
trackemail = response.email;
trackpassword = response.password;
$("#profile-username, #profile-username-1").html(response.username);
$("#profile-email").html(response.email);
$("#profile-password").html(response.password);
getUserIcon();
},
error: function (xhr, status, error) {
console.log(error);
}
});
}
// route "/user/:username" (method:PUT)
// update username name button when click update the username in memory
// the user must successful login for data to be save in json
$('#update-username-button').on('click', function (event) {
event.preventDefault();
var newUsername = $('#update-username').val().trim();
if (newUsername === "") {
newUsername = null;
}
$.ajax({
url: `${localURL}/user/${newUsername}`,
type: 'PUT',
data: newUsername,
headers: {
'Authorization': token
},
success: function (response) {
getUserData();
$("#alert-success-2").html(response);
$("#alert-success-2").css("display", "block");
setTimeout(function () {
$("#alert-success-2").fadeOut();
}, 2000);
return false;
},
error: function (xhr, status, error) {
$("#alert-fail-2").html(xhr.responseJSON.error);
$("#alert-fail-2").css("display", "block");
setTimeout(function () {
$("#alert-fail-2").fadeOut();
}, 2000)
}
});
});
// route "/user/:username/:password/:email" (method:PUT)
// update email button when click update the email in memory
// // the user must successful login for data to be save in json
$('#update-email-button').on('click', function (event) {
event.preventDefault();
var newEmail = $('#update-email').val();
if (newEmail === "") {
newEmail = null;
}
$.ajax({
url: `${localURL}/user/${trackusername}/${trackpassword}/${newEmail}`,
type: 'PUT',
data: trackusername, trackpassword, newEmail,
headers: {
'Authorization': token
},
success: function (response) {
getUserData();
$("#alert-success-2").html(response);
$("#alert-success-2").css("display", "block");
setTimeout(function () {
$("#alert-success-2").fadeOut();
}, 2000);
return false;
},
error: function (xhr, status, error) {
$("#alert-fail-2").html(xhr.responseJSON.error);
$("#alert-fail-2").css("display", "block");
setTimeout(function () {
$("#alert-fail-2").fadeOut();
}, 2000)
}
});
});
// route "/user/:username/:password" (method:PUT)
// update passwprd button when click update the the password in memory
// the user must successful login for data to be save in json
$('#update-password-button').on('click', function (event) {
event.preventDefault();
var newPassword = $('#update-password').val().trim();
if (newPassword === "") {
newPassword = null;
}
$.ajax({
url: `${localURL}/user/${trackusername}/${newPassword}`,
type: 'PUT',
data: trackusername, trackpassword,
headers: {
'Authorization': token
},
success: function (response) {
getUserData();
$("#alert-success-2").html(response);
$("#alert-success-2").css("display", "block");
setTimeout(function () {
$("#alert-success-2").fadeOut();
}, 2000);
return false;
},
error: function (xhr, status, error) {
$("#alert-fail-2").html(xhr.responseJSON.error);
$("#alert-fail-2").css("display", "block");
setTimeout(function () {
$("#alert-fail-2").fadeOut();
}, 2000)
}
});
});
// route "/user" (method:DELETE)
// this button will delete the current login user
// will than be sent back to the home screen
// remove all token associate with this account
$('#delete-button').on('click', function (event) {
$.ajax({
url: `${localURL}/user`,
type: 'DELETE',
headers: {
'Authorization': token
},
success: function (response) {
console.log(response);
},
error: function (xhr, status, error) {
console.log(error);
}
});
});
// route "/signout" (method:DELETE)
// this button will signout the user and
// update any changes they made to there account the current login user
// will than be sent back to the home screen
// remove all token associate with this account
$('#signout-button').on('click', function () {
$.ajax({
url: `${localURL}/signout`,
type: 'DELETE',
headers: {
'Authorization': token
},
success: function (response) {
console.log(response);
},
error: function (xhr, status, error) {
console.log(error);
}
});
});
// warning button for userIcon
$('#upload-form').mouseover(function() {
$("#alert-warning-2").html("Warning: uploading a new image will automatically sign out user and any changes that were made will be save to JSON.");
$("#alert-warning-2").css("display", "block");
// Fade out the success after 1 second
setTimeout(function () {
$("#alert-warning-2").fadeOut();
}, 5000);
});
// route "/userIcon" (method:POST)
var base64String ="";
var fileName ="";
// Add an event listener to the input element for profile image
// when a new files is uploading extract the file to 64 byte
$('#image-input').on('change', function(event) {
event.preventDefault();
fileName = $(this).val().split('\\').pop();
// Get the input element
const imageInput = $('#image-input');
// Get the selected file
const file = imageInput[0].files[0];
// Create a file reader
const reader = new FileReader();
// Set up the file reader to convert the file to a base64 string
reader.readAsDataURL(file);
reader.onload = function() {
base64String = reader.result.split(',')[1];
};
});
// when the profile image submit is click
// continue to route "userIcon"
// this will cause the user to automatically sign out
// any changes will be save to JSON on the server side
$('#upload-form').on( "submit", function(e) {
e.preventDefault();
const userData = {
image: base64String,
filename: fileName
}
$.ajax({
url: `${localURL}/userIcon`,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(userData),
dataType: 'json',
headers: {
'Authorization': token
},
success: function (response) {
console.log(response);
return false;
},
error: function (xhr, status, error) {
console.log('Error:', xhr);;
}
});
});
// route "/userIcon" (method:GET)
// this is call whe the route "/login is call"
// display server image.
function getUserIcon() {
$.ajax({
url: `${localURL}/userIcon`,
type: 'GET',
headers: {
'Authorization': token
},
success: function (response) {
// Get the image element by its ID
const img = $('#profile-image');
// Set the src attribute of the image element to the base64-encoded image data
img.attr('src', `data:image/jpeg;base64,${response.image}`)
},
error: function (xhr, status, error) {
console.log(error);
}
});
}