-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
334 lines (308 loc) · 11.8 KB
/
Copy pathapp.js
File metadata and controls
334 lines (308 loc) · 11.8 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// ✅ Helper function to handle API errors
function handleAPIError(response) {
if (response.status === 404) {
throw new Error("User not found! Please check the username.");
} else if (response.status === 403) {
throw new Error("API rate limit exceeded. Please try again later.");
} else if (!response.ok) {
throw new Error(`HTTP Error: ${response.status}`);
}
}
// ✅ Format date nicely
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
}
// ✅ Get GitHub User
async function getGitHubUser(username) {
try {
console.log(`Making HTTP GET request for user: ${username}`);
const response = await fetch(`https://api.github.com/users/${username}`);
handleAPIError(response);
const userData = await response.json();
return userData;
} catch (error) {
console.log("Error getting user:", error.message);
throw error;
}
}
// ✅ Get Repositories
async function getUserRepositories(username) {
try {
console.log(`Fetching repositories for user: ${username}`);
const response = await fetch(`https://api.github.com/users/${username}/repos`);
handleAPIError(response);
const repos = await response.json();
return repos;
} catch (error) {
console.log("Error getting repositories:", error.message);
throw error;
}
}
// ✅ Get Followers
async function getUserFollowers(username) {
try {
console.log(`Fetching followers for user: ${username}`);
const response = await fetch(`https://api.github.com/users/${username}/followers`);
handleAPIError(response);
const followers = await response.json();
return followers;
} catch (error) {
console.log("Error getting followers:", error.message);
throw error;
}
}
// ✅ Get Following
async function getUserFollowing(username) {
try {
console.log(`Fetching following list for user: ${username}`);
const response = await fetch(`https://api.github.com/users/${username}/following`);
handleAPIError(response);
const following = await response.json();
return following;
} catch (error) {
console.log("Error getting following:", error.message);
throw error;
}
}
// ✅ Get Events
async function getUserEvents(username) {
try {
console.log(`Fetching recent events for user: ${username}`);
const response = await fetch(`https://api.github.com/users/${username}/events/public`);
handleAPIError(response);
const events = await response.json();
return events.slice(0, 10);
} catch (error) {
console.log("Error getting events:", error.message);
throw error;
}
}
// ✅ Get Organizations
async function getUserOrganizations(username) {
try {
console.log(`Fetching organizations for user: ${username}`);
const response = await fetch(`https://api.github.com/users/${username}/orgs`);
handleAPIError(response);
const orgs = await response.json();
return orgs;
} catch (error) {
console.log("Error getting organizations:", error.message);
throw error;
}
}
// ✅ BONUS: Repository Details
async function getRepositoryDetails(owner, repo) {
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}`);
handleAPIError(response);
const repoDetails = await response.json();
return repoDetails;
} catch (error) {
console.log("❌ Error getting repository details:", error.message);
throw error;
}
}
// ✅ BONUS: Repository Languages
async function getRepositoryLanguages(owner, repo) {
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/languages`);
handleAPIError(response);
const languages = await response.json();
return languages;
} catch (error) {
console.log("❌ Error getting repository languages:", error.message);
throw error;
}
}
// ✅ BONUS: Repository Contributors
async function getRepositoryContributors(owner, repo) {
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/contributors`);
handleAPIError(response);
const contributors = await response.json();
return contributors;
} catch (error) {
console.log("❌ Error getting repository contributors:", error.message);
throw error;
}
}
// ==============================================
// ✅ UI Handling Section
// ==============================================
const usernameInput = document.getElementById("usernameInput");
const searchBtn = document.getElementById("searchBtn");
const loadingMessage = document.getElementById("loadingMessage");
const errorSection = document.getElementById("errorSection");
const errorText = document.getElementById("errorText");
const tryAgainBtn = document.getElementById("tryAgainBtn");
// Display containers
const userProfileCard = document.getElementById("userProfileCard");
const repositoriesContainer = document.getElementById("repositoriesContainer");
const activityContainer = document.getElementById("activityContainer");
const followersContainer = document.getElementById("followersContainer");
const followingContainer = document.getElementById("followingContainer");
const organizationsContainer = document.getElementById("organizationsContainer");
// Profile fields
const userAvatar = document.getElementById("userAvatar");
const userName = document.getElementById("userName");
const userUsername = document.getElementById("userUsername");
const userBio = document.getElementById("userBio");
const userCompany = document.getElementById("userCompany");
const userLocation = document.getElementById("userLocation");
const userBlog = document.getElementById("userBlog");
const userJoined = document.getElementById("userJoined");
const repoCount = document.getElementById("repoCount");
const followerCount = document.getElementById("followerCount");
const followingCount = document.getElementById("followingCount");
searchBtn.addEventListener("click", async () => {
const username = usernameInput.value.trim();
if (!username) return alert("Please enter a GitHub username!");
// Reset UI
errorSection.classList.add("hidden");
userProfileCard.classList.add("hidden");
repositoriesContainer.classList.add("hidden");
activityContainer.classList.add("hidden");
organizationsContainer.classList.add("hidden");
loadingMessage.classList.remove("hidden");
try {
// Fetch all data parallelly
const [user, repos, events, followers, following, orgs] = await Promise.all([
getGitHubUser(username),
getUserRepositories(username),
getUserEvents(username),
getUserFollowers(username),
getUserFollowing(username),
getUserOrganizations(username)
]);
displayUserProfile(user);
displayRepositories(repos);
displayEvents(events);
displayFollowers(followers);
displayFollowing(following);
displayOrganizations(orgs);
loadingMessage.classList.add("hidden");
} catch (error) {
loadingMessage.classList.add("hidden");
errorSection.classList.remove("hidden");
errorText.textContent = error.message;
}
});
tryAgainBtn.addEventListener("click", () => {
errorSection.classList.add("hidden");
usernameInput.value = "";
});
// ✅ Display Functions
function displayUserProfile(user) {
userProfileCard.classList.remove("hidden");
userAvatar.src = user.avatar_url;
userName.textContent = user.name || "No Name Provided";
userUsername.textContent = `@${user.login}`;
userBio.textContent = user.bio || "No bio available.";
userCompany.textContent = user.company || "N/A";
userLocation.textContent = user.location || "N/A";
userBlog.href = user.blog || "#";
userBlog.textContent = user.blog ? "Visit" : "N/A";
userJoined.textContent = formatDate(user.created_at);
repoCount.textContent = user.public_repos;
followerCount.textContent = user.followers;
followingCount.textContent = user.following;
}
function displayRepositories(repos) {
repositoriesContainer.innerHTML = "";
repositoriesContainer.classList.remove("hidden");
if (!repos.length) {
repositoriesContainer.innerHTML = `<p class="no-data">No repositories found.</p>`;
return;
}
repos.forEach(repo => {
const card = document.createElement("div");
card.classList.add("repo-card");
card.innerHTML = `
<h4><a href="${repo.html_url}" target="_blank">${repo.name}</a></h4>
<p>${repo.description || "No description"}</p>
<div class="repo-info">
<span class="language">${repo.language || "N/A"}</span>
<span class="stars">⭐ ${repo.stargazers_count}</span>
<span class="forks">🍴 ${repo.forks_count}</span>
</div>
`;
repositoriesContainer.appendChild(card);
});
}
function displayEvents(events) {
activityContainer.innerHTML = "";
activityContainer.classList.remove("hidden");
if (!events.length) {
activityContainer.innerHTML = `<p class="no-data">No recent activity found.</p>`;
return;
}
events.forEach(event => {
const card = document.createElement("div");
card.classList.add("activity-card");
card.innerHTML = `
<div class="activity-info">
<span class="activity-type">${event.type}</span>
<span class="activity-date">${formatDate(event.created_at)}</span>
</div>
<p>Repo: ${event.repo.name}</p>
`;
activityContainer.appendChild(card);
});
}
function displayFollowers(followers) {
followersContainer.innerHTML = "";
if (!followers.length) {
followersContainer.innerHTML = `<p class="no-data">No followers found.</p>`;
return;
}
followers.forEach(user => {
const card = document.createElement("div");
card.classList.add("user-mini-card");
card.innerHTML = `
<img src="${user.avatar_url}" class="mini-avatar" alt="${user.login}">
<a href="${user.html_url}" target="_blank">${user.login}</a>
`;
followersContainer.appendChild(card);
});
}
function displayFollowing(following) {
followingContainer.innerHTML = "";
if (!following.length) {
followingContainer.innerHTML = `<p class="no-data">No following found.</p>`;
return;
}
following.forEach(user => {
const card = document.createElement("div");
card.classList.add("user-mini-card");
card.innerHTML = `
<img src="${user.avatar_url}" class="mini-avatar" alt="${user.login}">
<a href="${user.html_url}" target="_blank">${user.login}</a>
`;
followingContainer.appendChild(card);
});
}
function displayOrganizations(orgs) {
organizationsContainer.innerHTML = "";
organizationsContainer.classList.remove("hidden");
if (!orgs.length) {
organizationsContainer.innerHTML = `<p class="no-data">No organizations found.</p>`;
return;
}
orgs.forEach(org => {
const card = document.createElement("div");
card.classList.add("org-card");
card.innerHTML = `
<img src="${org.avatar_url}" class="org-avatar" alt="${org.login}">
<div class="org-info">
<h4><a href="https://github.com/${org.login}" target="_blank">${org.login}</a></h4>
<p>${org.description || "No description available"}</p>
</div>
`;
organizationsContainer.appendChild(card);
});
}