-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
126 lines (111 loc) · 4.02 KB
/
script.js
File metadata and controls
126 lines (111 loc) · 4.02 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
const API = 'https://api.github.com/users/';
const main = document.querySelector('.main-content');
const card = document.getElementById('card');
const loading = document.getElementById('loading');
const form = document.getElementById('search-form');
// preventing from page refresh and fetching github progile when submitting the form
form.addEventListener('submit', async (event) => {
event.preventDefault();
await fetchProfile();
});
const fetchProfile = async () => {
const usernameInput = document.getElementById('username');
const username = usernameInput.value.trim();
// If username is empty or containing spaces it will return false and will display this message
if (!username) {
displayError('GitHub username must be required.');
return false;
}
if(/\s/.test(username)){
displayError('Invalid username. Please enter a valid GitHub username.');
return false;
}
// Showing loading spinner and clearing the card
loading.style.display = 'block';
card.innerHTML = '';
try {
const response = await fetch(API+username);
if (!response.ok) {
if (response.status === 403) {
// Handles 403 Forbidden error
displayError('Profile is not accessible right now due to privacy or rate limiting. Please try again later.');
} else {
// Handles other non-200 responses
displayDefaultProfile();
}
return;
}
const data = await response.json();
displayProfile(data);
} catch (error) {
displayDefaultProfile();
return;
} finally {
loading.style.display = 'none';
card.style.display = 'block';
}
usernameInput.value = '';
};
const displayProfile = (data) => {
main.style.display = 'block';
card.style.display = 'block';
card.innerHTML = `
<div>
<img src="${data.avatar_url || './Default_Profile.png'}" alt="${data.name || 'User'}">
</div>
<div class="user-info">
<h1>${data.name || 'Anonymous'}</h1>
<p>${data.bio || 'No bio available'}</p>
<ul class="info">
<li>${data.followers || 0}<strong>Followers</strong></li>
<li>${data.following || 0}<strong>Following</strong></li>
<li>${data.public_repos || 0}<strong>Repos</strong></li>
</ul>
<div id="repos"></div>
</div>`;
fetchRepos(data.login); // Fetching and displaying repos
};
const fetchRepos = async (username) => {
const reposContainer = document.getElementById('repos');
reposContainer.innerHTML = '';
try {
const response = await fetch(API + username + '/repos');
// If no repositories are found, showing this message
if (!response.ok) {
throw new Error('Repositories not found');
}
const repos = await response.json();
repos.slice(0, 10).forEach(repo => {
const link = document.createElement('a');
link.href = repo.html_url;
link.target = '_blank';
link.className = 'repo';
link.innerText = repo.name;
reposContainer.appendChild(link);
});
} catch (error) {
reposContainer.innerHTML = '<p>No repositories found.</p>';
}
};
const displayDefaultProfile = () => {
main.style.display = 'block';
card.innerHTML = `
<div>
<img src="./Default_Profile.png" alt="Default Profile">
</div>
<div class="user-info">
<h1>Anonymous</h1>
<p>No bio available</p>
<ul class="info">
<li>0<strong>Followers</strong></li>
<li>0<strong>Following</strong></li>
<li>0<strong>Repos</strong></li>
</ul>
</div>`;
};
const displayError = (message) => {
main.style.display = 'block';
card.style.display = 'block';
card.innerHTML = `<p class="error-message">${message}</p>`;
loading.style.display = 'none';
};