-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (45 loc) · 1.7 KB
/
index.js
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
async function fetchGitHubUserDetails(username, accessToken) {
const url = `https://api.github.com/users/${username}`;
const headers = {
Authorization: `Bearer ${accessToken}`,
Accept: 'application/vnd.github.v3+json',
};
try {
const response = await axios.get(url, { headers });
return response.data;
} catch (error) {
console.error('Failed to fetch user details:', error.response.status, error.response.data);
return null;
}
}
const form = document.querySelector('.needs-validation');
const input = document.getElementById('floatingTextarea2');
const userDetailsContainer = document.querySelector('.user-details');
form.addEventListener('submit', async (event) => {
event.preventDefault();
event.stopPropagation();
if (form.checkValidity()) {
const accessToken = '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';
const username = input.value.trim();
userDetailsContainer.innerHTML = '<h1>Loading...</h1>';
const userDetails = await fetchGitHubUserDetails(username, accessToken);
if (userDetails) {
userDetailsContainer.innerHTML = `
<div class = "text-center">
<h1>User Details</h1>
<p>Username: ${userDetails.login}</p>
<p>Name: ${userDetails.name}</p>
<p>Location: ${userDetails.location}</p>
<p>Bio: ${userDetails.bio}</p>
<p>Public Repositories: ${userDetails.public_repos}</p>
<p>Followers: ${userDetails.followers}</p>
<p>Following: ${userDetails.following}</p>
</div>
<!-- Add more details here as needed -->
`;
} else {
userDetailsContainer.innerHTML = '<h1>Error retrieving user details</h1>';
}
}
form.classList.add('was-validated');
});