diff --git a/components/GithubCard.js b/components/GithubCard.js index 4cc30e4..b03ea00 100644 --- a/components/GithubCard.js +++ b/components/GithubCard.js @@ -5,6 +5,22 @@ // 4️⃣ Pass the data into a function to create a user card. // 5️⃣ Append the created card to the `.cards` container in the DOM. +// https://api.github.com/users/Abdirahman984 + +const apiURL = "https://api.github.com/users/Abdirahman984"; + +function fitchApi() { + // return the promise that resolves to the user data (response.data) + return axios + .get(apiURL) + .then((response) => response.data) + .catch((error) => { + console.log("Error fetching user:", error); + // rethrow so callers can also handle if needed + throw error; + }); +} + // 🛠️ STEP 2: Create a Function to Build the Card // 1️⃣ Write a function that takes a **user object** as a parameter. @@ -22,15 +38,71 @@ //

Bio: {bio}

// // + +function createCard(userObject) { + const mainCard = document.createElement("div"); + mainCard.className = "card"; + + const mainImg = document.createElement("img"); + mainImg.src = userObject.avatar_url; + + const secondDiv = document.createElement("div"); + secondDiv.className = "card-info"; + + const titleH3 = document.createElement("h3"); + titleH3.className = "name"; + titleH3.textContent = userObject.name || userObject.login; // fallback if no name + + const firstPara = document.createElement("p"); + firstPara.className = "username"; + firstPara.textContent = userObject.login; + + const secoPara = document.createElement("p"); + secoPara.textContent = `Location: ${userObject.location || "Not specified"}`; + + const thirdPara = document.createElement("p"); + thirdPara.innerHTML = `Profile: ${userObject.html_url}`; + + const forthPara = document.createElement("p"); + forthPara.textContent = `Followers: ${userObject.followers ?? 0}`; + + const fifthPara = document.createElement("p"); + fifthPara.textContent = `Following: ${userObject.following ?? 0}`; + + const sixthPara = document.createElement("p"); + sixthPara.textContent = `Bio: ${userObject.bio || "No bio"}`; + + secondDiv.append(titleH3, firstPara, secoPara, thirdPara, forthPara, fifthPara, sixthPara); + mainCard.append(mainImg, secondDiv); + + return mainCard; +} + // // 3️⃣ Return the created card element. + + // 🛠️ STEP 3: Add the Card to the DOM // 1️⃣ Call the function with the GitHub data. // 2️⃣ Select the `.cards` container using `document.querySelector('.cards')`. // 3️⃣ Append the created card to the `.cards` container. +function displayGitHub() { + // call API to get main user data + fitchApi() + .then((userData) => { + // STEP 3: create and append the main user card + const card = createCard(userData); + const cardsContainer = document.querySelector(".cards"); + cardsContainer.append(card); + + + +// run it + + // 🛠️ STEP 4: Fetch Followers Data // 1️⃣ Use the `followers_url` from the GitHub user data or @@ -42,6 +114,31 @@ // - Append the card to the `.cards` container. + // STEP 4: fetch followers using the followers_url from userData + return axios.get(userData.followers_url) + .then((followersResponse) => { + // 3️⃣ Log the response data to inspect structure + console.log("Followers response:", followersResponse.data); + + const followersList = followersResponse.data; // array of follower objects + + // 4️⃣ For each follower: create card and append + followersList.forEach((follower) => { + const followerCard = createCard(follower); + cardsContainer.append(followerCard); + }); + }) + .catch((err) => { + console.log("Error fetching followers:", err); + }); + }) + .catch((err) => { + console.log("Error in displayGitHub:", err); + }); +} + +displayGitHub(); + // 🛠️ STRETCH: Add More GitHub Users // 1️⃣ Create an array `followersArray` with at least 5 GitHub usernames. // 2️⃣ Loop through the array and send a GET request for each username. diff --git a/index.html b/index.html index 906769d..3c8e2b3 100644 --- a/index.html +++ b/index.html @@ -1,15 +1,18 @@ - - - - + + + +
- Gabi School Logo + Gabi School Logo

❤️'s

GitHub Logo
@@ -18,6 +21,7 @@ + - \ No newline at end of file +