diff --git a/github-repo-fetcher.js b/github-repo-fetcher.js index 8c19ad3..5d4b816 100644 --- a/github-repo-fetcher.js +++ b/github-repo-fetcher.js @@ -1,56 +1,214 @@ +// GitHub API endpoint +const API_URL = 'https://api.github.com'; - document.getElementById('fetchButton').addEventListener('click', function() { - - // Get the input value - var username = document.getElementById('usernameInput').value; +// Selectors +const usernameInput = document.getElementById('usernameInput'); +const fetchButton = document.getElementById('fetchButton'); +const userImage = document.getElementById('userImage'); +const userName = document.getElementById('userName'); +const userLocation = document.getElementById('userLocation'); +const userBio = document.getElementById('userBio'); +const userProfileLink = document.getElementById('userProfileLink'); +const userLinks = document.getElementById('userLinks'); +const userSocialLinks = document.getElementById('userSocialLinks'); +const userFollowers = document.getElementById('userFollowers'); +const userRepos = document.getElementById('userRepos'); +const userStars = document.getElementById('userStars'); +const userPullRequests = document.getElementById('userPullRequests'); +const userIssues = document.getElementById('userIssues'); +const repositoriesDiv = document.getElementById('repositories'); +const loaderContainer = document.getElementById('loaderContainer'); +const loader = document.getElementById('loader'); +const paginationDiv = document.getElementById('pagination'); +const userInfoCard = document.getElementById('userInfo'); - // Check if the username is empty - if (!username) { - console.error('Please enter a GitHub username.'); - return; +let repositoriesData = []; // Array to store fetched repositories +let currentPage = 1; // Variable to track current page + +// Event listener for fetch button click +fetchButton.addEventListener('click', fetchRepositories); + +// Function to fetch repositories +async function fetchRepositories() { + const username = usernameInput.value.trim(); + + if (username === '') { + alert('Please enter a GitHub username.'); + return; + } + + // Clear previous data + clearUserData(); + repositoriesDiv.innerHTML = ''; + paginationDiv.innerHTML = ''; + loaderContainer.style.display = 'flex'; + loader.style.display = 'block'; + + try { + // Fetch user data + const userResponse = await fetch(`${API_URL}/users/${username}`); + if (!userResponse.ok) { + throw new Error('Failed to fetch user data'); } + const userData = await userResponse.json(); + + // Display user information + displayUserInfo(userData); + + // Display user info card + userInfoCard.style.display = 'flex'; + + // Fetch repositories + const repositoriesResponse = await fetch(`${API_URL}/users/${username}/repos`); + if (!repositoriesResponse.ok) { + throw new Error('Failed to fetch repositories'); + } + repositoriesData = await repositoriesResponse.json(); + + // Display first page of repositories + displayRepositories(currentPage); + + } catch (error) { + repositoriesDiv.innerHTML = `
${description ? description : 'No description provided'}
+