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

Refactor code to improve readability and maintainability #832

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
64 changes: 23 additions & 41 deletions JS/index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,28 @@
//fetching data
async function fetchData() {
await fetch(
"https://api.github.com/repos/SauravMukherjee44/CodeIN-Community-Website/contributors"
) //api for the get request
.then((response) => response.json())
.then((data) => {
/* check if data is null or undefined using nullish collision js(2020)
if undefined return no contributors */
let contributors = data ?? "No contributors";
// preventing the printing of contributers data in console log
// console.log(data);
let contributorsName = [];
let contributorImageURL = [];
let contributorGitHubURL = [];
let contributor = [];
// getting contributors img and url
for (let i = 0; i < contributors.length; i++) {
contributorImageURL[i] = contributors[i].avatar_url;
contributorGitHubURL[i] = contributors[i].html_url;
contributorsName[i] = contributors[i].login;
contributor[i] = `<a href="${contributorGitHubURL[i]}" target=”_blank”>
<img src="${contributorImageURL[i]}" alt="${contributorsName[i]}">
<span>${contributorsName[i]}</span>
</a>`;
}
// console.log(contributor);
// preventing the printing of contributers data in console log
// getting dom elements
let contributorBox = document.querySelector("#contributor");
try {
const response = await fetch("https://api.github.com/repos/SauravMukherjee44/CodeIN-Community-Website/contributors");
const data = await response.json();

// Check if data is null or undefined using nullish coalescing operator
const contributors = data ?? [];

contributorBox.innerHTML = contributor.join("");
})
.catch((e) => {
console.error(e);
});
// Map contributors data to HTML elements
const contributorsHTML = contributors.map(contributor => `
<a href="${contributor.html_url}" target="_blank">
<img src="${contributor.avatar_url}" alt="${contributor.login}">
<span>${contributor.login}</span>
</a>`
).join("");

// Render contributors on the webpage
document.querySelector("#contributor").innerHTML = contributorsHTML;
} catch (error) {
console.error("Error fetching contributors:", error);
}
}

//call the function after 1s
setTimeout(() => {
fetchData();
}, 1000);
// Call fetchData() function after 1 second
setTimeout(fetchData, 1000);

const d=new Date();
let year=d.getFullYear();
document.querySelector(".year").innerHTML=year;
// Set current year
document.querySelector(".year").textContent = new Date().getFullYear();