From 7945d7bf79a3e7283ccec4d0e304c0778e9b42ef Mon Sep 17 00:00:00 2001 From: Hemant Chaudhary <104961126+hemant933@users.noreply.github.com> Date: Sat, 23 Dec 2023 17:42:54 +0530 Subject: [PATCH] Update contributors.js --- js/contributors.js | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/js/contributors.js b/js/contributors.js index 1297e0b..fa6d9c1 100644 --- a/js/contributors.js +++ b/js/contributors.js @@ -1,19 +1,36 @@ 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; @@ -21,28 +38,18 @@ async function fetchAllContributors() { 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) {