-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
65 lines (49 loc) · 1.95 KB
/
Copy pathapi.js
File metadata and controls
65 lines (49 loc) · 1.95 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
const githubUserInformation = document.querySelector('.username');
const SearchButton = document.querySelector('.searchInfo');
const displayInformation = document.querySelector('.userInformation')
// fetching the data from githubs api
async function fetchUserInfo(username) {
// const username = githubUserInformation.value.trim();
try {
const response = await fetch(`https://api.github.com/users/${username}`); //template literal
const userInfo = await response.json();
return userInfo;
} catch (error) {
console.log(error.message);
}
}
// const getData = async () => {
// const userData = githubUserInformation.value.trim();
// const request = await fetch(`https://api.github.com/users/${userData}`);
// const response = await request.json();
// return response;
// }
// getData().then(data => {
// if (!data) throw new Error(`${userName.value} is not a valid user`);
// }).catch( (err) => console.log.Error(err));
SearchButton.addEventListener("click", loadData);
// githubUserInformation.addEventListener("keyup", loadData);
const display = function (userInfo) {
if (!userInfo) {
alert("No user found");
return;
}else if (userInfo.name === undefined) {
alert("No user found");
}
const display = `
<img src="${userInfo.avatar_url}" alt="">
<p><b>Name : ${userInfo.Name} </b></p>
<small>@username : ${userInfo.login}</small> <br>
<small>joined : ${new Date(userInfo.created_at).toLocaleDateString()}</small>
<p>following : ${userInfo.following}</p> <p>follower : ${userInfo.follower}</p>
`;
displayInformation.innerHTML = display;
}
async function loadData() {
const username = githubUserInformation.value.trim();
if (username === "") {
alert("Please enter a username");
}
const userInfo = await fetchUserInfo(username);
display(userInfo);
}