-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
114 lines (102 loc) · 3.85 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Details API</title>
</head>
<body>
<h1>Fetch User Details</h1>
<label for="user-email">Email:</label>
<input type="text" id="user-email" placeholder="Enter user email">
<button id="fetch-button">Get Details</button>
<br>
<br>
<div id="user-details">
<label for="firstName">First Name:</label>
<input type="text" id="firstName"><br>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName"><br>
<label for="email">Email:</label>
<input type="text" id="email"><br>
<label for="phone">Phone:</label>
<input type="text" id="phone"><br>
<label for="about">About:</label>
<textarea id="about" rows="4" cols="50"></textarea><br>
<label for="linkedIn">LinkedIn:</label>
<input type="text" id="linkedIn"><br>
<label for="twitter">Twitter:</label>
<input type="text" id="twitter"><br>
<label for="facebook">Facebook:</label>
<input type="text" id="facebook"><br>
<button id="save-button">Save Changes</button>
</div>
<script>
const userEmailInput = document.getElementById('user-email');
const fetchButton = document.getElementById('fetch-button');
const userDetails = document.getElementById('user-details');
const saveButton = document.getElementById('save-button');
fetchButton.addEventListener('click', async () => {
const userEmail = userEmailInput.value.trim();
if (!userEmail) {
alert('Please enter a valid email address.');
return;
}
try {
const response = await fetch('http://127.0.0.1:8000/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email: userEmail }),
});
if (!response.ok) {
throw new Error(`Error fetching user details: ${response.statusText}`);
}
const data = await response.json();
// Populate fields with fetched data
document.getElementById('firstName').value = data.firstName;
document.getElementById('lastName').value = data.lastName;
document.getElementById('email').value = data.email;
document.getElementById('phone').value = data.phone;
document.getElementById('about').value = data.about;
document.getElementById('linkedIn').value = data.linkedIn;
document.getElementById('twitter').value = data.twitter;
document.getElementById('facebook').value = data.facebook;
} catch (error) {
console.error('Error:', error);
alert(`Error: ${error.message}`);
}
});
saveButton.addEventListener('click', async () => {
// Collect updated user details
const updatedUser = {
firstName: document.getElementById('firstName').value,
lastName: document.getElementById('lastName').value,
email: document.getElementById('email').value,
phone: document.getElementById('phone').value,
about: document.getElementById('about').value,
linkedIn: document.getElementById('linkedIn').value,
twitter: document.getElementById('twitter').value,
facebook: document.getElementById('facebook').value,
};
try {
const response = await fetch('http://127.0.0.1:8000/api/user_update', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updatedUser),
});
if (!response.ok) {
throw new Error(`Error updating user details: ${response.statusText}`);
}
alert('User details updated successfully!');
} catch (error) {
console.error('Error:', error);
alert(`Error: ${error.message}`);
}
});
</script>
</body>
</html>