-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
112 lines (108 loc) · 3.28 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
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
const API_LINK = "https://api.github.com/users";
const search_btn = document.querySelector(".search");
const search_term = document.getElementById("search-term");
const repoUl = document.getElementById("repo");
const searching = document.querySelector(".searching");
search_term.focus();
const repo = [];
search_btn.addEventListener("click", async (e) => {
e.preventDefault();
if (search_term.value) {
searching.innerHTML = "Searching...";
try {
const userDetails = await getUserDetails(
`${API_LINK}/${search_term.value}`
);
const repos = await getRepoDetails(
`${API_LINK}/${search_term.value}/repos`
);
showUserDetails(userDetails, repos);
} catch (error) {
alert("An error occurred. Please try again.");
console.error(error);
} finally {
searching.innerHTML = "";
}
} else {
alert("Please enter any GitHub username");
search_term.focus();
}
});
function showUserDetails(userData, userRepos) {
const box = document.querySelector(".box-body");
const repoList = userRepos
.map(
(repo) =>
`<a href="${repo.html_url}" target="_blank"><li>${repo.name}</li></a>`
)
.join("");
box.innerHTML = `
<div class="profile-box">
<div class="row">
<div class="image">
<img src="${userData.avatar_url}" alt="">
</div>
<div class="about">
<div class="details">
<h1 class="name">${userData.name}</h1>
<h3 class="username">@${userData.login}</h3>
<p class="country"><span><ion-icon name="location-sharp"></ion-icon></span>${
userData.location ? userData.location : "Unknown"
}</p>
</div>
<div class="btn-profile">
<a href="${
userData.html_url
}" target="_blank">Visit Profile</a>
</div>
</div>
</div>
<div class="bio">
<h3>About</h3>
<p>${
userData.bio ? userData.bio : "Bio description is unavailable"
}</p>
</div>
<div class="row-followers">
<div class="col">
<h3 class="heading">
Followers
</h3>
<p>${userData.followers}</p>
</div>
<div class="col">
<h3 class="heading">
Following
</h3>
<p>${userData.following}</p>
</div>
<div class="col">
<h3 class="heading">
Repos
</h3>
<p>${userData.public_repos}</p>
</div>
</div>
<h3 class="repo-heading">Repositories</h3>
<div class="respos-row">
<ul id="repo">
${repoList}
</ul>
</div>
</div>
`;
}
async function getUserDetails(api) {
const response = await fetch(api);
if (!response.ok) {
throw new Error("User not found");
}
return response.json();
}
async function getRepoDetails(repo_api) {
const response = await fetch(repo_api);
if (!response.ok) {
throw new Error("Repositories not found");
}
return response.json();
}