-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.html
227 lines (216 loc) · 10.5 KB
/
info.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous"/>
<title>Your info</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>
<script src="globals.js"></script>
<div class="container-fluid bg-image">
<div class="row justify-content-center align-items-center min-vh-100">
<div class="col-12 col-md-6 col-lg-4">
<div class="card p-4">
<h2 id="infoTitle" class="text-center mb-3">Personal Info</h2>
<form class="row g-3" onsubmit="event.preventDefault();" oninput="checkValid()">
<div class="col-md-6">
<input type="text" class="form-control" id="personalName" placeholder="name">
<div id="personalNameFeedback" class="valid-feedback d-none">Looks good!</div>
</div>
<div class="col-md-6">
<input type="text" class="form-control" id="surname" placeholder="surname">
<div id="surnameFeedback" class="valid-feedback d-none">Looks good!</div>
</div>
<div class="col-md-6">
<input type="number" class="form-control" id="age" placeholder="age" min="0" max="99">
<div id="ageFeedback" class="valid-feedback d-none">Looks good!</div>
</div>
<div class="col-md-6">
<select class="form-select" id="superhero">
<option id="select" selected disabled hidden value="">Choose your superhero</option>
</select>
<div id="superheroFeedback" class="valid-feedback d-none">Looks good!</div>
</div>
<div class="col-md-12">
<input type="text" class="form-control" id="email" placeholder="email">
<div id="emailFeedback" class="valid-feedback d-none">Looks good!</div>
</div>
<div class="d-flex">
<button id="save" class="btn btn-primary col" onclick="saveData()" type="submit">Save</button>
<button id="deleteAccount" class="btn btn-danger col" onclick="remove()" type="submit">Delete</button>
</div>
</form>
<a id="goBack" class="btn btn-warning btn-block w-100 mt-3 text-center" href="index.html">go back</a>
</div>
</div>
</div>
</div>
<script>
var words = JSON.parse(localStorage.getItem('words'));
console.log(words);
function checkValid(){
let personalName = document.getElementById('personalName');
surname = document.getElementById('surname');
age = document.getElementById('age');
email = document.getElementById('email');
superhero = document.getElementById('superhero');
if (personalName.value !== "") {
showValid('personalName', words[9].text);
} else {
showInvalid('personalName',words[10].text);
// console.log("Name missing");
}
if(surname.value !== ""){
showValid('surname',words[9].text);
}
else{
showInvalid('surname',words[11].text);
// console.log("surname missing");
}
if(age.value !== ""){
showValid('age',words[9].text);
}
else{
showInvalid('age',words[12].text);
// console.log("age missing");
}
if(email.value !== ""){
if(emailRegex.test(email.value)){
showValid('email',words[9].text);
}
else{
showInvalid('email',words[14].text);
// console.log("email not valid");
}
}
else{
showInvalid('email',words[13].text);
// console.log("email missing");
}
if(superhero.value !== ""){
showValid('superhero',words[9].text);
}
else{
showInvalid('superhero',words[15].text);
// console.log("superhero missing");
}
}
function addData(user){
document.getElementById('personalName').value = user.name;
document.getElementById('surname').value = user.surname;
document.getElementById('age').value = user.age;
document.getElementById('email').value = user.email;
select = document.getElementById('select');
select.value = user.superhero;
select.textContent = user.superhero;
}
function saveData(){
var flag = false
nome = document.getElementById('personalName').value;
surname = document.getElementById('surname').value;
age = document.getElementById('age').value;
email = document.getElementById('email').value;
superhero = document.getElementById('superhero').value;
obj = {
name : nome,
surname : surname,
age : age,
email : email,
superhero: superhero
}
console.log("fetch");
fetch("http://localhost:3000/users/"+id+"?api_key="+api_key,setPostOption(obj))
.then(response => {
if( response.status == 200){
flag = true;
}
else
flag = false
return response.json();
})
.then(response => {
if(!flag){
console.log(response)
switch(response.response){
case "name missing":
showInvalid('personalName',words[10].text);
// console.log("name missing");
break;
case "surname missing":
showInvalid('surname',words[11].text);
// console.log("surname missing");
break;
case "age missing":
showInvalid('age',words[12].text);
// console.log("age missing");
break;
case "email missing":
showInvalid('email',words[13].text);
// console.log("email missing");
break;
case "email not valid":
showInvalid('email',words[14].text);
// console.log("email not valid");
break;
case "superhero missing":
showInvalid('superhero',words[15].text);
// console.log("superhero missing");
break;
case "email already used":
showInvalid('email',words[16].text);
// console.log("email not valid");
break;
}
}
else{
localStorage.removeItem('arrayId');
window.location.href = "index.html";
}
});
}
function remove(){
var flag = false;
fetch("http://localhost:3000/users/"+id+"?api_key="+api_key,del)
.then(response => {
if(response.status == 200){
flag = true;
}
return response.json();
})
.then(response => {
if(flag){
console.log("eliminato");
localStorage.removeItem('idUser');
localStorage.removeItem('arrayId');
window.location.href = "login.html";
}
else
console.log(response);
});
}
validateLog();
getAllHeroes();
localStorage.setItem('Pag',1);
loadLang();
let id = localStorage.getItem('idUser');
fetch("http://localhost:3000/users/"+id+"?api_key="+api_key,get)
.then(response => {
if(response.status == 200)
flag = true;
return response.json();
})
.then(response => {
if(flag){
addData(response);
}
else{
localStorage.removeItem('arrayId');
window.location.href = "login.html";
}
});
</script>
</body>
</html>