-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.js
327 lines (276 loc) · 10.3 KB
/
script.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
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
document.addEventListener("DOMContentLoaded", function () {
const searchButton = document.getElementById("searchButton");
const repositoriesContainer = document.getElementById("repositories");
const rateLimitMessage = document.getElementById("rateLimitMessage");
const tourButton = document.getElementById("tourButton");
const loader = document.querySelector(".loader");
// Create a pagination
const paginationContainer = document.createElement("div");
paginationContainer.id = "paginationContainer";
// const loadMoreButton = document.createElement('button');
// loadMoreButton.textContent = 'Load More';
// loadMoreButton.id = 'loadMoreButton';
// loadMoreButton.style.display = 'none';
repositoriesContainer.after(paginationContainer);
let currentPage = 1;
let currentQuery = "";
let totalPages = 1;
startTour();
tourButton.addEventListener("click", startTour);
searchButton.addEventListener("click", fetchRepositories);
// loadMoreButton.addEventListener("click", loadMoreRepositories);
async function fetchRepositories(event, page = 1) {
event.preventDefault();
// Remove any existing repositories from the previous search
repositoriesContainer.innerHTML = "";
const language = document.getElementById("customLanguage").value;
const topic = document.getElementById("topic").value;
const keyword = document.getElementById("keyword").value;
const sortOptions = Array.from(
document.querySelectorAll('input[name="sort"]:checked')
).map((el) => el.value);
let query = "https://api.github.com/search/repositories?q=";
const filters = [];
if (language) filters.push(`language:${language}`);
if (topic) filters.push(`topic:${topic}`);
if (keyword) filters.push(keyword);
query += filters.join("+");
if (sortOptions.length > 0) {
query += `&sort=${sortOptions.join(",")}&order=desc`;
}
query += `&page=${page}&per_page=10`;
currentQuery = query;
showLoader();
try {
const response = await fetch(query);
const data = await response.json();
if (data) {
const repositories = data.items;
// Because Github API only allows fetching up to 1000 results, we need to limit the total pages to 100
// https://docs.github.com/en/rest/search/search?apiVersion=2022-11-28
totalPages = 100;
// Getting the rate limit headers from response
const rateLimitRemain = response.headers.get("X-RateLimit-Remaining");
const rateLimitReset = response.headers.get("X-RateLimit-Reset");
const rateResetInSeconds =
convertRateLimitResetToSeconds(rateLimitReset); // Convert the rate limit reset time to seconds
const rateLimitRemainInt = parseInt(rateLimitRemain); // Ensure that the rate limit is an integer, value returned is a string
// Alert user if the rate limit is almost reached (< 5)
if (rateLimitRemainInt < 5) {
rateLimitMessage.innerHTML = `Reminder: You have ${rateLimitRemain} ${
rateLimitRemain > 1 ? "requests" : "request"
} left.`;
}
// If the rate limit is 0 or exceeded, show an error message
if (rateLimitRemainInt === 0) {
throw new Error(
`Rate limit exceeded. Please try again after ${rateResetInSeconds} ${
rateResetInSeconds > 1 ? "seconds" : "second"
}.`
);
}
if (repositories.length === 0 && page === 1) {
repositoriesContainer.innerHTML = "<h2>No repositories found.</h2>";
loadMoreButton.style.display = "none";
} else {
if (page === 1) {
repositoriesContainer.innerHTML = "";
}
displayRepositories(repositories);
renderPagination();
// loadMoreButton.style.display =
// repositories.length === 10 ? "block" : "none";
}
}
} catch (error) {
console.error("Error fetching repositories:", error);
repositoriesContainer.innerHTML = `<h2>An error occurred while fetching repositories: ${error.message}</h2>`;
rateLimitMessage.innerHTML = "";
} finally {
hideLoader();
}
}
// async function loadMoreRepositories() {
// currentPage++;
// await fetchRepositories({ preventDefault: () => {} }, currentPage);
// }
function displayRepositories(repositories) {
repositories.forEach((repo, index) => {
const repoCard = document.createElement("div");
repoCard.className = "repository-card";
repoCard.style.opacity = "0";
repoCard.style.transform = "translateY(20px)";
repoCard.innerHTML = `
<div class="flip-card-inner">
<div class="flip-card-front">
<img src="${repo.owner.avatar_url}" alt="${
repo.owner.login
}" class="avatar">
<h3>${repo.name}</h3>
<p>★ ${repo.stargazers_count}</p>
<small>Forks: ${repo.forks_count}</small>
<small>Language: ${repo.language}</small>
</div>
<div class="flip-card-back">
<p>${repo.description || "No description available."}</p>
<div class="details">
<p>Owner: ${repo.owner.login}</p>
<p>Open Issues: ${repo.open_issues_count}</p>
<p>Bio: ${repo.owner.bio || "No bio available."}</p>
</div>
</div>
`;
repositoriesContainer.appendChild(repoCard);
setTimeout(() => {
repoCard.style.opacity = "1";
repoCard.style.transform = "translateY(0)";
}, index * 100);
});
}
function createButton(text, className, isDisabled, onClick) {
const button = document.createElement("button");
button.textContent = text;
if (isDisabled === true) {
className += " disabled";
}
button.className = className;
button.disabled = isDisabled;
button.addEventListener("click", onClick);
return button;
}
function renderPagination() {
paginationContainer.innerHTML = "";
if (totalPages !== 1) {
// Create "Previous" button
const previousButton = createButton(
"Previous",
"pagination-button",
currentPage === 1,
() => {
if (currentPage > 1) {
currentPage--;
fetchRepositories({ preventDefault: () => {} }, currentPage);
}
}
);
paginationContainer.appendChild(previousButton);
const createPageButton = (page) => {
const pageButton = createButton(
page,
"pagination-button",
page === currentPage,
() => {
currentPage = page;
fetchRepositories({ preventDefault: () => {} }, currentPage);
}
);
paginationContainer.appendChild(pageButton);
};
createPageButton(1);
// Show ellipsis if there are more than 3 pages between the first page and the current page
if (currentPage > 4) {
const ellipsis = document.createElement("span");
ellipsis.textContent = "...";
paginationContainer.appendChild(ellipsis);
}
// Show a few pages around the current page
for (
let i = Math.max(2, currentPage - 2);
i <= Math.min(totalPages - 1, currentPage + 2);
i++
) {
createPageButton(i);
}
// Show ellipsis if there are more than 3 pages between the current page and the last page
if (currentPage < totalPages - 3) {
const ellipsis = document.createElement("span");
ellipsis.textContent = "...";
paginationContainer.appendChild(ellipsis);
}
// Always show the last page
if (totalPages > 1) {
createPageButton(totalPages);
}
const nextButton = createButton(
"Next",
"pagination-button",
currentPage === totalPages,
() => {
if (currentPage < totalPages) {
currentPage++;
fetchRepositories({ preventDefault: () => {} }, currentPage);
}
}
);
paginationContainer.appendChild(nextButton);
}
}
function convertRateLimitResetToSeconds(rateLimitReset) {
// Get the current Unix time (in seconds)
const currentTime = Math.floor(Date.now() / 1000);
// Calculate the remaining time in seconds
const timeRemainingInSeconds = rateLimitReset - currentTime;
return timeRemainingInSeconds;
}
function showLoader() {
loader.style.display = "inline-block";
repositoriesContainer.style.display = "none";
searchButton.disabled = true;
searchButton.textContent = "Searching...";
}
function hideLoader() {
loader.style.display = "none";
repositoriesContainer.style.display = "flex";
searchButton.disabled = false;
searchButton.textContent = "Search";
}
function startTour() {
const tour = new Shepherd.Tour({
useModalOverlay: true,
defaultStepOptions: {
cancelIcon: {
enabled: true,
},
classes: "shadow-md bg-purple-dark",
scrollTo: { behavior: "smooth", block: "center" },
},
});
tour.addStep({
id: "language",
text: "Select the programming language you are interested in.",
attachTo: { element: "#customLanguage", on: "bottom" },
buttons: [{ text: "Next", action: tour.next }],
});
tour.addStep({
id: "topic",
text: "Optionally enter a topic to narrow down your search.",
attachTo: { element: "#topic", on: "bottom" },
buttons: [{ text: "Next", action: tour.next }],
});
tour.addStep({
id: "keyword",
text: "Optionally enter keyword to narrow down your search.",
attachTo: { element: "#keyword", on: "bottom" },
buttons: [{ text: "Next", action: tour.next }],
});
tour.addStep({
id: "sort",
text: "Choose to Sort repositories by Commits, Issues, Pull Requests, Forks and Stars",
attachTo: { element: "#stars", on: "bottom" },
buttons: [{ text: "Next", action: tour.next }],
});
tour.addStep({
id: "search",
text: "Click the Search button to fetch top repositories matching your criteria.",
attachTo: { element: "#searchButton", on: "bottom" },
buttons: [{ text: "Finish", action: tour.complete }],
});
tour.start();
}
document
.getElementById("myForm")
.addEventListener("submit", function (event) {
event.preventDefault();
console.log("Form submission prevented!");
});
});