Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the API request on fetching contributors #1093

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
324 changes: 176 additions & 148 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,200 +1,228 @@
const hamburger = document.querySelector(".hamburger");
const navMenu = document.querySelector(".nav-menu");

document.addEventListener("DOMContentLoaded", function () {
setTimeout(function () {
document.querySelector("body").classList.add("loaded");
}, 500)
document.addEventListener("DOMContentLoaded", async function () {
setTimeout(function () {
document.querySelector("body").classList.add("loaded");
}, 500);
});

hamburger.addEventListener("click", mobileMenu);
function mobileMenu() {
hamburger.classList.toggle("active");
navMenu.classList.toggle("active");
hamburger.classList.toggle("active");
navMenu.classList.toggle("active");
}

const navLink = document.querySelectorAll(".nav-link");
navLink.forEach(n => n.addEventListener("click", closeMenu));
navLink.forEach((n) => n.addEventListener("click", closeMenu));

function closeMenu() {
hamburger.classList.remove("active");
navMenu.classList.remove("active");
hamburger.classList.remove("active");
navMenu.classList.remove("active");
}

const cont = document.getElementById('contributor');
const owner = 'Rakesh9100';
const repoName = 'CalcDiverse';
const cont = document.getElementById("contributor");
const owner = "Rakesh9100";
const repoName = "CalcDiverse";

async function fetchContributors(pageNumber) {
const perPage = 100;
const url = `https://api.github.com/repos/${owner}/${repoName}/contributors?page=${pageNumber}&per_page=${perPage}`;
const apiUrl =
"https://script.googleusercontent.com/macros/echo?user_content_key=HIngl5N6XqT87RP5_NGfOvh4Owd0vsFxGl4j7WfN5JA7XmZ3wfLP6Lm9KtE8MvYr-xqtib3jFCNoJ3gLd--RXPTM6yeCqYRMm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnJlUuZlL1ANkgTfzluynVq_ujwIhHMAx6EPfzKkWJ1uCshAjBuwXySyQgwTqiBx63rp_lIW_4lqd8qNYusW-W_j7amvZZ0XS2Q&lib=MVYp2QNGGJIwIlwc0BFDww2ojRkgaZCOe";

const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch contributors data. Status code: ${response.status}`);
async function getkey() {
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
console.log(data); // Log the response data
return data.apik[0].apikey;
} catch (error) {
console.error("Error fetching data:", error);
}

const contributorsData = await response.json();
return contributorsData;
}
const token = await getkey();
const perPage = 100;
const url = `https://api.github.com/repos/${owner}/${repoName}/contributors?page=${pageNumber}&per_page=${perPage}`;

const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) {
throw new Error(
`Failed to fetch contributors data. Status code: ${response.status}`
);
}

const contributorsData = await response.json();
return contributorsData;
}

// Function to fetch all contributors
async function fetchAllContributors() {
let allContributors = [];
let pageNumber = 1;

try {
while (true) {
const contributorsData = await fetchContributors(pageNumber);
if (contributorsData.length === 0) {
break;
}
allContributors = allContributors.concat(contributorsData);
pageNumber++;
}
allContributors.forEach((contributor) => {
const contributorCard = document.createElement('div');
contributorCard.classList.add('contributor-card');

const avatarImg = document.createElement('img');
avatarImg.src = contributor.avatar_url;
avatarImg.alt = `${contributor.login}'s Picture`;

const loginLink = document.createElement('a');
loginLink.href = contributor.html_url;
loginLink.appendChild(avatarImg);

contributorCard.appendChild(loginLink);

cont.appendChild(contributorCard);
});
} catch (error) {
console.error(error);
let allContributors = [];
let pageNumber = 1;

try {
while (true) {
const contributorsData = await fetchContributors(pageNumber);
if (contributorsData.length === 0) {
break;
}
allContributors = allContributors.concat(contributorsData);
pageNumber++;
}
allContributors.forEach((contributor) => {
const contributorCard = document.createElement("div");
contributorCard.classList.add("contributor-card");

const avatarImg = document.createElement("img");
avatarImg.src = contributor.avatar_url;
avatarImg.alt = `${contributor.login}'s Picture`;

const loginLink = document.createElement("a");
loginLink.href = contributor.html_url;
loginLink.appendChild(avatarImg);

contributorCard.appendChild(loginLink);

cont.appendChild(contributorCard);
});
} catch (error) {
console.error(error);
}
}

fetchAllContributors();

let calcScrollValue = () => {
let scrollProg = document.getElementById("progress");
let pos = document.documentElement.scrollTop;
let calcHeight =
document.documentElement.scrollHeight -
document.documentElement.clientHeight;
let scrollValue = Math.round((pos * 100) / calcHeight);
if (pos > 100) {
scrollProg.style.display = "grid";
} else {
scrollProg.style.display = "none";
}
scrollProg.addEventListener("click", () => {
document.documentElement.scrollTop = 0;
});
scrollProg.style.background = `conic-gradient(#0063ba ${scrollValue}%, #d499de ${scrollValue}%)`;
let scrollProg = document.getElementById("progress");
let pos = document.documentElement.scrollTop;
let calcHeight =
document.documentElement.scrollHeight -
document.documentElement.clientHeight;
let scrollValue = Math.round((pos * 100) / calcHeight);
if (pos > 100) {
scrollProg.style.display = "grid";
} else {
scrollProg.style.display = "none";
}
scrollProg.addEventListener("click", () => {
document.documentElement.scrollTop = 0;
});
scrollProg.style.background = `conic-gradient(#0063ba ${scrollValue}%, #d499de ${scrollValue}%)`;
};

window.addEventListener('scroll', function () {
var scrollToTopButton = document.getElementById('progress');
if (window.pageYOffset > 200) {
scrollToTopButton.style.display = 'block';
} else {
scrollToTopButton.style.display = 'none';
}
window.addEventListener("scroll", function () {
var scrollToTopButton = document.getElementById("progress");
if (window.pageYOffset > 200) {
scrollToTopButton.style.display = "block";
} else {
scrollToTopButton.style.display = "none";
}
});

window.onscroll = calcScrollValue;
window.onload = calcScrollValue;

// Function to filter calculators
function filterCalculators() {
var input, filter, calculators, i;
input = document.getElementById('calculatorSearch');
filter = input.value.toUpperCase();
calculators = document.querySelectorAll('.container .box');
var noResults = document.getElementById('noResults');
var hasResults = false;

for (i = 0; i < calculators.length; i++) {
var calculator = calculators[i];
var h2 = calculator.querySelector('h2');
var calculatorName = h2.textContent || h2.innerText;

if (calculatorName.toUpperCase().indexOf(filter) > -1) {
calculator.style.display = "flex";
hasResults = true;
} else {
calculator.style.display = "none";
}
}

if (hasResults) {
noResults.style.display = 'none';
var input, filter, calculators, i;
input = document.getElementById("calculatorSearch");
filter = input.value.toUpperCase();
calculators = document.querySelectorAll(".container .box");
var noResults = document.getElementById("noResults");
var hasResults = false;

for (i = 0; i < calculators.length; i++) {
var calculator = calculators[i];
var h2 = calculator.querySelector("h2");
var calculatorName = h2.textContent || h2.innerText;

if (calculatorName.toUpperCase().indexOf(filter) > -1) {
calculator.style.display = "flex";
hasResults = true;
} else {
noResults.style.display = 'block';
calculator.style.display = "none";
}
}

if (hasResults) {
noResults.style.display = "none";
} else {
noResults.style.display = "block";
}
}

document.addEventListener('DOMContentLoaded', function() {
document.getElementById('noResults').style.display = 'none';
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("noResults").style.display = "none";
});

// Voice command in search bar feature
const searchBar = document.querySelector("#searchBar");
const searchBarInput = searchBar.querySelector("input");

// The speech recognition interface lives on the browser’s window object
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; // if none exists -> undefined
const SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition; // if none exists -> undefined

if (SpeechRecognition) {
console.log("Your Browser supports speech Recognition");

const recognition = new SpeechRecognition();
recognition.continuous = true;
// recognition.lang = "en-US";

searchBar.insertAdjacentHTML("beforeend", '<button type="button"><i class="fas fa-microphone"></i></button>');
// searchBarInput.style.paddingRight = "50px";

const micBtn = searchBar.querySelector("button");
const micIcon = micBtn.firstElementChild;

micBtn.addEventListener("click", micBtnClick);
function micBtnClick() {
if (micIcon.classList.contains("fa-microphone")) { // Start Voice Recognition
recognition.start(); // First time you have to allow access to mic!
}
else {
recognition.stop();
}
}

recognition.addEventListener("start", startSpeechRecognition); // <=> recognition.onstart = function() {...}
function startSpeechRecognition() {
micIcon.classList.remove("fa-microphone");
micIcon.classList.add("fa-microphone-slash");
searchFormInput.focus();
console.log("Voice activated, SPEAK");
}

recognition.addEventListener("end", endSpeechRecognition); // <=> recognition.onend = function() {...}
function endSpeechRecognition() {
micIcon.classList.remove("fa-microphone-slash");
micIcon.classList.add("fa-microphone");
searchBarInput.focus();
console.log("Speech recognition service disconnected");
}

recognition.addEventListener("result", resultOfSpeechRecognition); // <=> recognition.onresult = function(event) {...} - Fires when you stop talking
function resultOfSpeechRecognition(event) {
const current = event.resultIndex;
const transcript = event.results[current][0].transcript;
newtranscript = transcript.endsWith('.') ? transcript.slice(0, -1) : transcript;
console.log(newtranscript)
searchBarInput.value = newtranscript;
filterCalculators();
console.log("Your Browser supports speech Recognition");

const recognition = new SpeechRecognition();
recognition.continuous = true;
// recognition.lang = "en-US";

searchBar.insertAdjacentHTML(
"beforeend",
'<button type="button"><i class="fas fa-microphone"></i></button>'
);
// searchBarInput.style.paddingRight = "50px";

const micBtn = searchBar.querySelector("button");
const micIcon = micBtn.firstElementChild;

micBtn.addEventListener("click", micBtnClick);
function micBtnClick() {
if (micIcon.classList.contains("fa-microphone")) {
// Start Voice Recognition
recognition.start(); // First time you have to allow access to mic!
} else {
recognition.stop();
}
}
else {
console.log("Your Browser does not support speech Recognition");
info.textContent = "Your Browser does not support Speech Recognition";
}

recognition.addEventListener("start", startSpeechRecognition); // <=> recognition.onstart = function() {...}
function startSpeechRecognition() {
micIcon.classList.remove("fa-microphone");
micIcon.classList.add("fa-microphone-slash");
searchFormInput.focus();
console.log("Voice activated, SPEAK");
}

recognition.addEventListener("end", endSpeechRecognition); // <=> recognition.onend = function() {...}
function endSpeechRecognition() {
micIcon.classList.remove("fa-microphone-slash");
micIcon.classList.add("fa-microphone");
searchBarInput.focus();
console.log("Speech recognition service disconnected");
}

recognition.addEventListener("result", resultOfSpeechRecognition); // <=> recognition.onresult = function(event) {...} - Fires when you stop talking
function resultOfSpeechRecognition(event) {
const current = event.resultIndex;
const transcript = event.results[current][0].transcript;
newtranscript = transcript.endsWith(".")
? transcript.slice(0, -1)
: transcript;
console.log(newtranscript);
searchBarInput.value = newtranscript;
filterCalculators();
}
} else {
console.log("Your Browser does not support speech Recognition");
info.textContent = "Your Browser does not support Speech Recognition";
}