-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
71 lines (67 loc) · 1.86 KB
/
script.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
58
59
60
61
62
63
64
65
66
67
68
69
70
const url = "https://api.github.com/users";
const searchInputEl = document.getElementById("searchInput");
const searchButtonEl = document.getElementById("searchBtn");
const profileContainerEl = document.getElementById("profileContainer");
const loadingEl = document.getElementById("loading");
const generateProfile = (profile) => {
return `
<div class="profile-box">
<div class="top-section">
<div class="left">
<div class="avatar">
<img alt="avatar" src="${profile.avatar_url}" />
</div>
<div class="self">
<h1>${profile.name}</h1>
<h1>@${profile.login}</h1>
</div>
</div>
<a href="${profile.html_url}" target="_black">
<button class="primary-btn">Check Profile</button>
</a>
</div>
<div class="about">
<h2>About</h2>
<p>
${profile.bio}
</p>
</div>
<div class="status">
<div class="status-item">
<h3>Followers</h3>
<p>${profile.followers}</p>
</div>
<div class="status-item">
<h3>Followings</h3>
<p>${profile.following}</p>
</div>
<div class="status-item">
<h3>Repos</h3>
<p>${profile.public_repos}</p>
</div>
</div>
</div>
`;
};
const fetchProfile = async () => {
const username = searchInputEl.value;
loadingEl.innerText = "loading.....";
loadingEl.style.color = "black";
try {
const res = await fetch(`${url}/${username}`);
const data = await res.json();
if (data.bio) {
loadingEl.innerText = "";
profileContainerEl.innerHTML = generateProfile(data);
} else {
loadingEl.innerHTML = data.message;
loadingEl.style.color = "red";
profileContainerEl.innerText = "";
}
console.log("data", data);
} catch (error) {
console.log({ error });
loadingEl.innerText = "";
}
};
searchButtonEl.addEventListener("click", fetchProfile);