-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (137 loc) · 6.39 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
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
const repositoriesElement = document.getElementById('repositories');
const loaderElement = document.getElementById('loader');
const userDetails = document.querySelector('.user-details');
const github = document.querySelector('.github');
const searchBarElement = document.querySelector('#searchBar');
let repos_url, currentPage = 1;
async function fetchUserInfo() {
const username = document.getElementById('username').value;
userDetails.innerHTML = null;
github.innerHTML = null;
loaderElement.innerHTML = null;
try {
loaderElement.style.display = 'block';
const response = await fetch(`https://api.github.com/users/${username}`);
const userInfo = await response.json();
loaderElement.style.display = 'none';
if (userInfo.message === 'Not Found') {
return alert(`Invalid username ${username}`);
};
let avatarContainer = document.createElement('div');
let profileImage = document.createElement('img');
profileImage.src = userInfo?.avatar_url ?? 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT2paNURw1DBfUC5w4I3m3EoIo7vHLpWxtXCg&usqp=CAU';
profileImage.setAttribute('alt', userInfo?.name);
avatarContainer.appendChild(profileImage);
const detailsContainer = document.createElement('div');
const person = document.createElement('h1');
person.innerText = userInfo?.name;
person.style.fontSize = '21px';
let bio = document.createElement('p');
bio.innerText = userInfo?.bio;
let locationContainer = document.createElement('div');
locationContainer.setAttribute('class', 'location');
const locationIcon = document.createElement('i');
locationIcon.setAttribute('class', 'fa-solid fa-location-dot');
locationIcon.style.fontSize = '21px';
const location = document.createElement('p');
location.innerText = userInfo?.location;
locationContainer.append(locationIcon, location);
const twitter = document.createElement('p');
twitter.innerText = `Twitter: https://twitter.com/${userInfo?.twitter_username}`;
detailsContainer.append(person, bio, locationContainer, twitter);
userDetails.append(avatarContainer, detailsContainer);
const githubLinkIcon = document.createElement('i');
githubLinkIcon.style.fontSize = '19px';
githubLinkIcon.setAttribute('class', 'fa-solid fa-paperclip');
const githubState = document.createElement('p');
githubState.innerText = `https://github.com/${userInfo?.login}`;
github.append(githubLinkIcon, githubState);
repos_url = userInfo.repos_url;
fetchRepos();
} catch (error) {
console.error('Error fetching User Details:', error);
alert('Error fetching User Details. Please check the console for details.');
} finally {
loaderElement.style.display = 'none';
}
};
async function fetchRepos() {
document.querySelector('.perPage-box').style.display = 'block';
document.querySelector('#pagination-container').style.display = 'block';
const perPage = document.getElementById('perPage').value;
try {
let repos = await fetch(`${repos_url}?per_page=${perPage}&page=${currentPage}`);
repos = await repos.json();
renderRepos(repos);
searchBarElement.style.display = 'block';
} catch (error) {
console.error('Error fetching repositories:', error);
alert('Error fetching repositories. Please check the console for details.');
}
};
const renderRepos = (repos) => {
repositoriesElement.innerHTML = '';
repos && repos.map(({ topics, name, description }) => {
let repoContainer = document.createElement('div');
repoContainer.className = 'repository';
let nameOfRepo = document.createElement('h2');
nameOfRepo.innerText = name;
nameOfRepo.style.color = 'rgb(104, 104, 234)'
let descOfRepo = document.createElement('p');
descOfRepo.innerText = description ?? 'Lorem ipsum dolor sit, amet consectetur adipisicing elit. Veritatis debitis adipisci voluptates recusandae expedita rem, tenetur tempore eveniet illum molestiae cupiditate, saepe odio et.';
let topicsContainer = document.createElement('div');
topicsContainer.setAttribute('class', 'topics');
topics?.forEach((topic, index) => {
const topicPara = document.createElement('span');
if (index > 3) return;
if (index === 3) {
const remainingTopicLength = topics.length - 4;
if (remainingTopicLength > 0) {
topicPara.innerText = `${topic} ${remainingTopicLength}+`;
} else {
topicPara.innerText = `${topic}`;
}
} else {
topicPara.innerText = topic;
}
topicsContainer.appendChild(topicPara);
});
repoContainer.append(nameOfRepo, descOfRepo, topicsContainer);
repositoriesElement.append(repoContainer);
});
};
function filterRepositories() {
const searchTerm = document.getElementById('search').value.toLowerCase();
const repositories = document.getElementsByClassName('repository');
for (const repository of repositories) {
const repositoryName = repository.textContent.toLowerCase();
if (repositoryName.includes(searchTerm)) {
repository.style.display = 'block';
} else {
repository.style.display = 'none';
}
}
};
function previousPage() {
if (currentPage > 1) {
currentPage--;
document.getElementById('currentPage').textContent = currentPage;
fetchRepos();
}
// Disable "Previous" button when on the first page
document.getElementById('previousButton').disabled = (currentPage === 1);
document.getElementById('nextButton').disabled = false;
};
function nextPage() {
currentPage++;
document.getElementById('currentPage').textContent = currentPage;
fetchRepos();
// Check if it's the last page and disable "Next" button
const perPage = document.getElementById('perPage').value;
const repositoriesCount = document.getElementById('repositories');
if (repositoriesCount < perPage) {
document.getElementById('nextButton').disabled = true;
}
// Enable "Previous" button when moving to the next page
document.getElementById('previousButton').disabled = false;
};