-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
166 lines (138 loc) · 4.92 KB
/
script.js
File metadata and controls
166 lines (138 loc) · 4.92 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// error
const errorBox = document.querySelector(".errorBox");
const errorText = document.querySelector(".errorText");
// profile
const profileWrapper = document.querySelector(".profileWrapper");
// user search
const form = document.querySelector("form");
const search = document.querySelector("#search");
// repo
const reposTitleContainer = document.querySelector(".reposTitleContainer");
const reposContainer = document.querySelector(".reposContainer");
// api
const API = "https://api.github.com/users";
// handle search
form.addEventListener("submit", (e) => {
e.preventDefault();
hideError(); // clear previous error
profileWrapper.innerHTML = "";
reposContainer.innerHTML = "";
reposTitleContainer.innerHTML = "";
fetchData();
search.value = "";
});
// fetch data
async function fetchData() {
const username = search.value.trim();
// if empty username
if (username.length === 0) {
showError("❌ Cant't search empty username !!");
return;
}
// fetch data
const response = await fetch(`${API}/${username}`);
const data = await response.json();
console.log("Search limit left : " + response.headers.get("X-RateLimit-Remaining"));
// if failed fetching
if (response.status === 404) {
showError(" User not found. Please check the username.");
return;
}
if (response.status === 403) {
const rateLimitRemaining = response.headers.get(
"X-RateLimit-Remaining"
);
if (rateLimitRemaining === "0") {
showError("GitHub API rate limit exceeded. Try again later.");
} else {
showError("Access forbidden. You may be blocked or unauthorized.");
}
return;
}
hideError();
// else set profile header
const avatarUrl = data.avatar_url;
const name = data.name;
const login = data.login;
const bio = data.bio;
const followers = data.followers;
const following = data.following;
const newProfileHead = document.createElement("div");
newProfileHead.className = "profileContainer";
newProfileHead.innerHTML = `
<div class="profileHead">
<div class="profileImg">
<img src=${avatarUrl} alt="user-image">
</div>
<div class="profileName">
<p class="profileNameText">${name || "Not available"}</p>
<p class="profileUsernameText">@${login}</p>
</div>
</div>
<!-- bio -->
<div class="bioContainer">
<p class="bioText">Bio : ${bio || "Not available"}</p>
</div>
<div class="followContainer">
<span><i class="fa-solid fa-user-group"></i></span>
<!-- <i class="fa-solid fa-user-group"></i> -->
<span class="followersText"><span class="followTextNum">${followers}</span> Followers</span>
<span class="followingText"><span class="followingTextNum">${following}</span> Following</span>
</div>
`;
profileWrapper.append(newProfileHead);
// repo title
const repoTitle = document.createElement("h2");
repoTitle.className = "reposTitle";
repoTitle.textContent = "📁 Repositories";
reposTitleContainer.prepend(repoTitle);
// fetch repo
const repoUrl = data.repos_url;
const repoResponse = await fetch(repoUrl);
const repoData = await repoResponse.json();
// if failed to fetch repo
if (!repoResponse.ok) {
showError("Failed to fetch repos.");
return;
}
if (repoData.length === 0) {
const noRepo = document.createElement("p");
noRepo.className = "noRepoError";
noRepo.textContent = "User haven't any repository !!";
reposContainer.append(noRepo);
return;
}
// repo items
repoData.forEach((obj) => {
const repoName = obj.name;
const repoUrl = obj.html_url;
const repoDesc = obj.description;
const repoLanguage = obj.language;
const repoStars = obj.stargazers_count;
const repoUpdatedAt = obj.updated_at;
// create repo dynamically
const newRepo = document.createElement("div");
newRepo.className = "repoCard";
newRepo.innerHTML = `
<a href=${repoUrl} target="_blank" class="repoName">${repoName}</a>
<p class="repoDesc">${repoDesc || "No description available"}</p>
<div class="repoMeta">
<span class="repoLang">🌐 ${repoLanguage}</span>
<span class="repoStars">⭐ ${repoStars}</span>
<span class="repoUpdated">🕒 Last update on ${
repoUpdatedAt.split("T")[0]
}</span>
</div>
`;
reposContainer.append(newRepo);
});
}
// error handle
function showError(message) {
errorText.textContent = message;
errorBox.style.display = "block";
}
function hideError() {
errorText.textContent = "";
errorBox.style.display = "none";
}