-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
123 lines (113 loc) · 4.1 KB
/
index.html
File metadata and controls
123 lines (113 loc) · 4.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
<html lang="en">
<style>
.container {
margin: 40px auto;
width: 80%;
}
.button {
width: 160px;
height: 45px;
border-radius: 6px;
font-size: 15px;
margin-top: 20px;
}
img {
width: 328px;
height: 287px;
display: block;
margin-bottom: 20px;
}
hr {
width: 400px;
margin-left: 0;
}
h3 {
display: inline-block;
}
#container {
display: none;
}
#container-edit {
display: none;
}
#container-edit input {
height: 32px;
}
#container-edit hr {
margin: 25px 0;
}
#container-edit input {
width: 195px;
font-size: 15px;
}
</style>
<script>
(async function init() {
const response = await fetch('http://localhost:3000/get-profile');
console.log("response", response);
const user = await response.json();
console.log(JSON.stringify(user));
document.getElementById('name').textContent = user.name ? user.name : 'Anna Smith';
document.getElementById('email').textContent = user.email ? user.email : 'anna.smith@example.com';
document.getElementById('interests').textContent = user.interests ? user.interests : 'coding';
const cont = document.getElementById('container');
cont.style.display = 'block';
})();
async function handleUpdateProfileRequest() {
const contEdit = document.getElementById('container-edit');
const cont = document.getElementById('container');
const payload = {
name: document.getElementById('input-name').value,
email: document.getElementById('input-email').value,
interests: document.getElementById('input-interests').value
};
const response = await fetch('http://localhost:3000/update-profile', {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
const jsonResponse = await response.json();
document.getElementById('name').textContent = jsonResponse.name;
document.getElementById('email').textContent = jsonResponse.email;
document.getElementById('interests').textContent = jsonResponse.interests;
cont.style.display = 'block';
contEdit.style.display = 'none';
}
function updateProfile() {
const contEdit = document.getElementById('container-edit');
const cont = document.getElementById('container');
document.getElementById('input-name').value = document.getElementById('name').textContent;
document.getElementById('input-email').value = document.getElementById('email').textContent;
document.getElementById('input-interests').value = document.getElementById('interests').textContent;
cont.style.display = 'none';
contEdit.style.display = 'block';
}
</script>
<body>
<div class='container' id='container'>
<h1>User profile</h1>
<img src='profile-picture' alt="user-profile">
<span>Name: </span><h3 id='name'>Anna Smith</h3>
<hr />
<span>Email: </span><h3 id='email'>anna.smith@example.com</h3>
<hr />
<span>Interests: </span><h3 id='interests'>coding</h3>
<hr />
<button class='button' onclick="updateProfile()">Edit Profile</button>
</div>
<div class='container' id='container-edit'>
<h1>User profile</h1>
<img src='profile-picture' alt="user-profile">
<span>Name: </span><label for='input-name'></label><input type="text" id='input-name' value='Anna Smith' />
<hr />
<span>Email: </span><label for='input-email'></label><input type="email" id='input-email' value='anna.smith@example.com' />
<hr />
<span>Interests: </span><label for='input-interests'></label><input type="text" id='input-interests' value='coding' />
<hr />
<button class='button' onclick="handleUpdateProfileRequest()">Update Profile</button>
</div>
</body>
</html>