-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (70 loc) · 2.14 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 || "No Name"}</h1>
<h1>@${profile.login}</h1>
</div>
</div>
<a href="${profile.html_url}" target="_blank">
<button class="primary-btn">Check Profile</button>
</a>
</div>
<div class="about">
<h2>About</h2>
<p>${profile.bio || "No bio available"}</p>
</div>
<div class="status">
<div class="status-item">
<h3>Followers</h3>
<p>${profile.followers}</p>
</div>
<div class="status-item">
<h3>Following</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.trim();
if (!username) {
alert("Please enter a GitHub username");
return;
}
loadingEl.innerText = "loading.....";
loadingEl.style.color = "black";
try {
const res = await fetch(`${url}/${username}`);
const data = await res.json();
if (data.message === "Not Found") {
loadingEl.innerHTML = "User not found";
loadingEl.style.color = "red";
profileContainerEl.innerText = "";
} else {
loadingEl.innerText = "";
profileContainerEl.innerHTML = generateProfile(data);
}
console.log("data", data);
} catch (error) {
console.log({ error });
loadingEl.innerText = "Something went wrong";
loadingEl.style.color = "red";
}
};
searchButtonEl.addEventListener("click", fetchProfile);