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

Update contributors.js #145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
41 changes: 24 additions & 17 deletions js/contributors.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
const cont = document.getElementById('contributor');
const repoUrl = 'https://api.github.com/repos/sahil-sagwekar2652/GitHub-Automation-scripts/contributors';

async function fetchContributors(pageNumber) {
const perPage = 100;
const url = `https://api.github.com/repos/sahil-sagwekar2652/GitHub-Automation-scripts/contributors?page=${pageNumber}&per_page=${perPage}`;
const url = `${repoUrl}?page=${pageNumber}&per_page=${perPage}`;

const response = await fetch(url);

if (!response.ok) {
throw new Error(`Failed to fetch contributors data. Status code: ${response.status}`);
}

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

function createContributorCard(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);

return contributorCard;
}

// 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++;
}

// Display contributors in the honeycomb-like layout
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);

const contributorCard = createContributorCard(contributor);
cont.appendChild(contributorCard);
});
} catch (error) {
Expand Down