-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
377 lines (343 loc) · 19.5 KB
/
script.js
File metadata and controls
377 lines (343 loc) · 19.5 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
$(document).ready(function() {
var colorPairs = {
pair1: ['#DEBB00', '#F0E68C'],
pair2: ['#CB356B', '#BD3F32'],
pair3: ['#56ab2f', '#a8e063'],
pair4: ['#a8ff78', '#78ffd6'],
pair5: ['#4e54c8', '#8f94fb'],
pair6: ['#DA4453', '#89216B'],
pair7: ['#a8c0ff', '#3f2b96'],
pair8: ['#bc4e9c', '#f80759'],
pair9: ['#11998e', '#38ef7d'],
pair10: ['#FC5C7D', '#6A82FB'],
pair11: ['#fc4a1a', '#f7b733'],
pair12: ['#7F00FF', '#E100FF'],
};
var reposArray = [];
var searchArray = [];
var username = localStorage?.getItem('username');
var name =""
var endPageNumber=localStorage.getItem('endPageNumber') ? localStorage.getItem('endPageNumber') : 1;
$('#searches').hide();
function showLoadingIndicator() {
$('#loader').show();
}
function hideLoadingIndicator() {
$('#loader').hide();
$('#loader').removeClass('d-flex flex-column justify-content-center align-items-center d-flex');
}
showLoadingIndicator();
async function fetchUserData(){
try{
const response = await fetch('https://api.github.com/users/' + username);
if (!response.ok) {
if(response.status==404){
alert('User does not exist : Enter correct username');
window.location.href = "index.html";
throw new Error('User does not exist : Enter correct username');
}
if(response.status==403 || response.status==429){
alert('API rate limit exceeded');
window.location.href = "index.html";
throw new Error('API rate limit exceeded');
}
if(response.status==401){
alert('Unauthorized');
window.location.href = "index.html";
throw new Error('Unauthorized');
}
if(response.status==500){
alert('Internal Server Error');
window.location.href = "index.html";
throw new Error('Internal Server Error');
}
hideLoadingIndicator();
}
const data = await response.json();
hideLoadingIndicator();
var userData = data;
console.log(userData);
name = userData.name;
$('#public_repos').text(userData.public_repos);
$('#followers').text(userData.followers);
$('#following').text(userData.following);
$('#avatar').html('<img src="' + userData.avatar_url + '" alt="" class="rounded-3 img-fluid" style="width: 12rem; height: auto;">');
if(userData.location==null){
userData.location = 'Not Available';
}
if(userData.twitter_username==null){
userData.twitter_username = 'Not Available';
}
if(userData.bio==null){
userData.bio = 'No bio available';
}
$('#info').append(`<div class="d-flex flex-column justify-content-center align-items-center p-4">
<div id="avatar">
<img src="` + userData.avatar_url + `" alt="" class="rounded-3 img-fluid" style="width: 12rem; height: auto;">
</div>
<p id="name" class="text-white font-weight-bold pt-3 fs-5">`+userData.name+`</p>
<p id="url" class="text-white">
<a href="` + userData.html_url + `" class="text-white">` + userData.html_url + `</a>
</p>
<p id="description" class="text-white text-center fs-6 line-clamp" style="max-width: 10rem;">`+userData.bio+`</p>
<div class="d-flex justify-content-center align-content-center mt-2" style="height: 1.5rem;">
<img src="./assets/images/location-pin.png" alt="" class="img-fluid rounded-5" style="max-width: 50%; max-height: 100%;"><p id="location" class="text-white fs-6 ms-3">`+userData.location+`</p>
</div>
<div class="d-flex justify-content-center align-content-center mt-4" style="height: 2.5rem;">
<img src="./assets/images/xlogo.jpg" alt="twitter logo" class="img-fluid rounded-3" style="max-width: 14%; height:auto; object-fit: contain;">
<p id="twitter_username" class="text-white fs-6 ms-3 pt-1">`+userData.twitter_username+`</p>
</div></div>`)
const starredResponse = await fetch('https://api.github.com/users/' + username + '/starred?per_page=1');
if(starredResponse.status==404){
alert('User does not exist : Enter correct username');
window.location.href = "index.html";
throw new Error('User does not exist : Enter correct username');
}
if(starredResponse.status==403 || starredResponse.status==429){
alert('API rate limit exceeded');
window.location.href = "index.html";
throw new Error('API rate limit exceeded');
}
if(starredResponse.status==401){
alert('Unauthorized');
window.location.href = "index.html";
throw new Error('Unauthorized');
}
if(starredResponse.status==500){
alert('Internal Server Error');
window.location.href = "index.html";
throw new Error('Internal Server Error');
}
const linkHeader = starredResponse.headers.get('Link');
if(linkHeader) {
const lastLink = linkHeader.split(', ').find(s => s.endsWith('rel="last"'));
const lastPageNumber = lastLink.match(/&page=(\d+)/)[1];
$('#starred').text(lastPageNumber);
}else{
$('#starred').text(0);
}
fetchRepositories(page);
} catch (error) {
console.error('Error:', error);
}
}
window.onload = function() {
fetchUserData();
};
if (!localStorage.getItem('firstLoad')) {
localStorage.setItem('firstLoad', true);
setTimeout(function () {
window.location.reload();
}, 500); // 1000 milliseconds = 1 second
} else {
localStorage.removeItem('firstLoad');
}
console.log(name);
reposArray.forEach(function(repo) {
topicss = repo.topics;
});
var page = localStorage.getItem('currentPage') ? localStorage.getItem('currentPage') : 1;
var reposPerPage = localStorage.getItem('reposPerPage') ? localStorage.getItem('reposPerPage') : 10;
function fetchRepositories(page) {
var apiUrl = 'https://api.github.com/users/' + username + '/repos?per_page=' + reposPerPage + '&page=' + page;
fetch(apiUrl)
.then(response => {
if (!response.ok) {
if(response.status==404){
alert('User does not exist : Enter correct username');
window.location.href = "index.html";
throw new Error('User does not exist : Enter correct username');
}
if(response.status==403 || response.status==429){
alert('API rate limit exceeded');
window.location.href = "index.html";
throw new Error('API rate limit exceeded');
}
if(response.status==401){
alert('Unauthorized');
window.location.href = "index.html";
throw new Error('Unauthorized');
}
if(response.status==500){
alert('Internal Server Error');
window.location.href = "index.html";
throw new Error('Internal Server Error');
}
}else{
const linkHeader = response.headers.get('Link');
if(linkHeader) {
if(page==1){
const lastLink = linkHeader.split(', ').find(s => s.endsWith('rel="last"'));
endPageNumber = parseInt(lastLink.match(/&page=(\d+)/)[1]);
localStorage.setItem('endPageNumber', endPageNumber);
console.log(endPageNumber);
}
}
}
return response.json();
})
.then(data => {
$('#repos').empty();
if(data.length==0){
$('#repos').append('<div class="text-white text-center fs-4">No repositories available</div>');
}else{
reposArray = data;
console.log(reposArray);
$.each(reposArray, function(i, repo){
var randomPairKey = Object.keys(colorPairs)[Math.floor(Math.random() * Object.keys(colorPairs).length)];
var randomPair = colorPairs[randomPairKey];
if(repo.language==null){
repo.language = 'Other';
}
if(repo.description==null){
repo.description = 'No description available';
}
$('#repos').append(
'<div id="repo" class="me-4 mb-3 text-white bg-dark px-3 rounded-3" style="width: 30%;"><div class="d-flex align-items-center pt-2"><img src="./assets/images/repo-plain.png" alt="" class="img-fluid me-3" style="width:8%; height:100%"><a href="'+repo.html_url+'" class="fs-4 pt-2 mb-3 line-clamp-1 text-overflow-ellipsis" style="max-width:100%">'+ repo.name +'</a></div><div><p class="mb-3 line-clamp-2" style="font-size: smaller;">'+ repo.description +'.</p></div><div id="language" class="d-flex align-items-center"><div class="rounded-circle" style="height: 1.2rem; width: 1.2rem; background: linear-gradient(60deg,' + randomPair[0] + ', '+randomPair[1] +')"></div><p class="ms-2 mb-0 text-white" style="font-size:0.8rem">'+repo.language+'</p></div><div id="topics'+i+'" class="gap-md-4 gap-lg-3 mt-4 mb-4" style=" display: flex; flex-wrap: wrap; width: 100%;"></div></div>'
)
console.log(repo.topics);
if(repo.topics.length==0){
var randomPairKey = Object.keys(colorPairs)[Math.floor(Math.random() * Object.keys(colorPairs).length)];
var randomPair = colorPairs[randomPairKey];
$('#topics'+i).append('<div class="topics-child p-1 m-0 rounded-2 d-flex justify-content-center align-items-center min-w-auto overflow-hidden" style=" background: linear-gradient(#202124, #202124) padding-box, linear-gradient(60deg,' + randomPair[0] + ', '+randomPair[1] +') border-box; box-shadow: 0 0 10px '+ randomPair[1] +';max-width:max-content;" >' + '<p class="px-3 m-0 fw-bold" style="font-size: x-small;">No topics available</p></div>');
}else{
console.log(repo.topics);
$.each(repo.topics, function(index, value) {
var randomPairKey = Object.keys(colorPairs)[Math.floor(Math.random() * Object.keys(colorPairs).length)];
var randomPair = colorPairs[randomPairKey];
$('#topics'+i).append('<div class="topics-child p-1 m-0 rounded-2 d-flex justify-content-center align-items-center min-w-auto overflow-hidden"style=" background: linear-gradient(#202124, #202124) padding-box, linear-gradient(60deg,' + randomPair[0] + ', '+randomPair[1] +') border-box; box-shadow: 0 0 10px '+ randomPair[1] +';max-width:max-content;" >' + '<p class="px-3 m-0 fw-bold" style="font-size: x-small">'+ value +'</p>' + '</div>');
});
}
})
$('#prevPageBtn').prop('disabled', page === 1);
updatePagination(page)
}
})
.catch(error => console.error('Error:', error));
}
var totalPagesToShow = 10;
var mobilePagesToShow = 5; // Number of pages to display in the pagination container
var paginationContainer = $('#paginationContainer');
function updatePagination(currentPage) {
paginationContainer.empty();
var isMobile = window.innerWidth < 768;
var pagesToShow = isMobile ? mobilePagesToShow : totalPagesToShow;
let startPage = 1;
if(page >pagesToShow){
startPage = page - pagesToShow + 1;
}
let endPage = startPage + pagesToShow - 1;
for (var i = startPage; i <= endPage && i <= endPageNumber; i++) {
var pageButton = $('<button id="'+ i +'" class="pagee btn btn-dark mx-1">' + i + '</button>');
if(i == page) {
pageButton.addClass('active border border-white');
}
pageButton.on('click', function() {
$('.pagee').removeClass('active border border-white');
page = parseInt($(this).text());
$(this).addClass('active border border-white');
fetchRepositories(page);
localStorage.setItem('currentPage', page);
});
paginationContainer.append(pageButton);
}
}
$('#nextPageBtn').on('click', function() {
page++;
fetchRepositories(page);
localStorage.setItem('currentPage', page);
$('#nextPageBtn').prop('disabled', page === endPageNumber);
updatePagination(page);
});
$('#prevPageBtn').on('click', function() {
page--;
fetchRepositories(page);
localStorage.setItem('currentPage', page);
$('#prevPageBtn').prop('disabled', page === 1);
$('#nextPageBtn').prop('disabled', page === endPageNumber);
updatePagination(page);
});
$('#changeButton').on('click', function () {
$('#sliderContainer').toggle();
$('#changeButton').toggle();
});
$('#requestsPerPage').on('input', function () {
reposPerPage = $(this).val();
localStorage.setItem('reposPerPage', reposPerPage);
$('#requestsPerPageValue').text(reposPerPage);
});
$('#submitChanges').on('click', function () {
fetchRepositories(page);
updatePagination(page);
$('#sliderContainer').toggle();
$('#changeButton').toggle();
});
$('#searchButton').on('click', function () {
$('#searches').show();
var searchQuery = $('#searchBox').val();
searchQuery = searchQuery.replace(/[^\w\s]/gi, '');
var searchUrl = 'https://api.github.com/search/repositories?q=' + searchQuery + '+user:' + username;
fetch(searchUrl)
.then(response => {
if (!response.ok) {
if(response.status==404){
alert('User does not exist : Enter correct username');
window.location.href = "index.html";
throw new Error('User does not exist : Enter correct username');
}
if(response.status==403 || response.status==429){
alert('API rate limit exceeded');
window.location.href = "index.html";
throw new Error('API rate limit exceeded');
}
if(response.status==401){
alert('Unauthorized');
window.location.href = "index.html";
throw new Error('Unauthorized');
}
if(response.status==500){
alert('Internal Server Error');
window.location.href = "index.html";
throw new Error('Internal Server Error');
}
}
return response.json();
})
.then(data => {
$('#searchesContainer').empty();
searchArray = data.items;
console.log(searchArray);
$.each(searchArray, function(i, repo){
$('#searchesContainer').append(
'<div id="repo" class="me-4 mb-3 text-white bg-dark px-3 rounded-3" style="width: 30%;"><div class="d-flex align-items-center pt-2"><img src="./assets/images/repo-plain.png" alt="" class="img-fluid me-3" style="width:8%; height:100%"><a href="'+repo.html_url+'" class="fs-4 pt-2 mb-3 line-clamp-1 text-overflow-ellipsis" style="max-width:100%">'+ repo.name +'</a></div><div><p class="mb-2 line-clamp-2" style="font-size: smaller;">'+ repo.description +'.</p></div><div id="topics'+i+'" class="gap-md-4 gap-lg-3 mt-4 mb-4" style=" display: flex; flex-wrap: wrap; width: 100%;">'+ repo.topics +'</div></div>'
)
console.log(repo.topics);
if(repo.topics.length==0){
var randomPairKey = Object.keys(colorPairs)[Math.floor(Math.random() * Object.keys(colorPairs).length)];
var randomPair = colorPairs[randomPairKey];
$('#topics'+i).append('<div class="topics-child p-1 m-0 rounded-2 d-flex justify-content-center align-items-center min-w-auto overflow-hidden" style=" background: linear-gradient(#202124, #202124) padding-box, linear-gradient(60deg,' + randomPair[0] + ', '+randomPair[1] +') border-box; box-shadow: 0 0 10px '+ randomPair[1] +';max-width:max-content;" >' + '<p class="px-3 m-0 fw-bold" style="font-size: x-small;">No topics available</p></div>');}else{
console.log(repo.topics);
$.each(repo.topics, function(index, value) {
var randomPairKey = Object.keys(colorPairs)[Math.floor(Math.random() * Object.keys(colorPairs).length)];
var randomPair = colorPairs[randomPairKey];
$('#topics'+i).append('<div class="topics-child p-1 m-0 rounded-2 d-flex justify-content-center align-items-center min-w-auto overflow-hidden"style=" background: linear-gradient(#202124, #202124) padding-box, linear-gradient(60deg,' + randomPair[0] + ', '+randomPair[1] +') border-box; box-shadow: 0 0 10px '+ randomPair[1] +';max-width:max-content;" >' + '<p class="px-3 m-0 fw-bold" style="font-size: x-small">'+ value +'</p>' + '</div>');
});
}
})
})
.catch(error => console.error('Error:', error));
});
$('#hide-search').on('click', function () {
$('#searches').toggle();
})
$('#searchBox').on('keypress', function (e) {
if (e.which == 13) {
$('#searchButton').click();
}
});
var lastPage = localStorage.getItem('currentPage');
if (lastPage) {
page = parseInt(lastPage);
}
fetchRepositories(page);
});