-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile_script.js
More file actions
202 lines (167 loc) · 5.53 KB
/
profile_script.js
File metadata and controls
202 lines (167 loc) · 5.53 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
const urlPOST = "http://localhost:8080/signin"
const urlGET = "http://localhost:8080/user/"
window.onload = init;
var templates = {}
templates.users = Handlebars.compile(`
<section class="user-info">
<h3>{{name}}</h3>
<ul>
<li>Διεύθυνση: {{this.address}}</li>
<li>Τηλέφωνο: {{this.phone}}</li>
<li>Μορφωτικό επίπεδο: {{this.education}}</li>
<li>email: {{this.email}}</li>
</ul>
</section>
`);
function init() {
const form = document.getElementById('signin-form');
const emailInput = document.querySelector('input[type="email"]');
const alertBoxemailIn = document.querySelector('.input-email > .alert-box');
const password = document.getElementById('profile-password-txt');
const alertBoxpswrdIn = document.querySelector('#signin-password-input-pswrd > .alert-box');
const div = document.querySelector('#user-results');
const alertResultBox = document.querySelector('#alert-box-results');
form.addEventListener('submit', function (e) {
e.preventDefault();
const emailValue = emailInput.value.trim();
const passwordValue = password.value.trim();
console.log("Submission button clicked!")
if (checkEmail(emailValue, alertBoxemailIn) && checkPassword(passwordValue, alertBoxpswrdIn)) {
let status = "201";
let userdata = {
signin_email: emailValue,
signin_password: passwordValue,
}
console.log(userdata);
sendPostRequest(userdata)
.then(response => {
status = response.status;
return response.json();
})
.then(responseMsg => {
console.log(responseMsg)
console.log(status)
showAlertBox(alertResultBox);
if (status == "202") {
disableForm(form);
makeGETRequest(urlGET, emailValue, div);
changeToSuccess(alertResultBox.children[0], responseMsg.msg);
}
else {
changeToFail(alertResultBox.children[0], responseMsg.msg);
console.log(">!< Looks like something went wrong :/ >!<");
}
console.log(responseMsg.msg)
})
.catch(error => {
console.log(">!< Fetch error >!<", error);
});
}
});
}
function checkEmail(value, alertBox) {
let flag = true;
let constraints = /^([a-zA-Z\d\.-]+)@([a-zA-Z\d-]+)\.([a-zA-Z]{2,4})(\.[a-zA-Z]{2,4})?$/;
if (isEmpty(value)) {
showBox(alertBox, "Συμπληρώστε το πεδίο.")
flag = false;
}
else if (constraints.test(value)) {
hideBox(alertBox);
}
else {
showBox(alertBox, "To email που συμπληρώσατε δεν είναι έγκυρο.");
flag = false;
}
return flag;
}
function checkPassword(password, alertBox) {
let flag = true;
if (isEmpty(password)) {
showBox(alertBox, "Συμπληρώστε το πεδίο.")
flag = false;
}
else {
hideBox(alertBox);
}
return flag;
}
function sendPostRequest(data) {
let myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');
let init = {
method: 'POST',
headers: myHeaders,
body: JSON.stringify(data)
};
return fetch(urlPOST, init)
}
function showBox(box, message) {
box.style.display = "initial";
box.innerHTML = message;
}
function hideBox(box) {
box.style.display = "none";
}
function isEmpty(value) {
if (value === '') {
return true;
}
return false;
}
function disableForm(form) {
let formInputs = form.getElementsByTagName('input');
let buttonsArea = document.getElementById('reg-form-buttons');
console.log(buttonsArea)
for (input of formInputs) {
input.readOnly = true;
}
buttonsArea.style.display = "none";
}
function makeGETRequest(url, email, div) {
let myHeaders = new Headers();
myHeaders.append('Accept', 'application/json');
let init = {
method: "GET",
headers: myHeaders
}
fetch(url + email, init)
.then(response => {
return response.json();
})
.then(data => {
console.log(data);
let userItem = {
"name": "🧍 " + data._firstName + " " + data._lastName,
"address": data._address,
"phone": data._telNumber,
"education": data._educationLevel,
"email": data._email
}
let userContent = templates.users(userItem);
div.innerHTML = userContent;
})
.catch(error => {
console.log(">!< Fetch error >!<", error);
})
}
function disableForm(form) {
let formInputs = form.getElementsByTagName('input');
let buttonsArea = document.getElementById('signin-form-buttons');
console.log(buttonsArea)
for (input of formInputs) {
input.readOnly = true;
}
buttonsArea.style.display = "none";
}
function showAlertBox(box) {
box.style.display = "initial";
}
function changeToSuccess(box, message) {
box.style.backgroundColor = "rgba(40, 212, 40, 0.63)";
box.innerHTML = message;
}
function changeToFail(box, message) {
box.style.backgroundColor = "rgba(247, 30, 30, 0.651)";
box.innerHTML = message;
}