|
| 1 | +// API USED => https://api.github.com/user/user_name |
| 2 | + |
| 3 | +let input_user = document.querySelector("#input"); |
| 4 | + |
| 5 | +const userImg = document.querySelector(".main-info"); |
| 6 | +// const name = document.querySelector("#name"); |
| 7 | +// const userName =document.querySelector("#username"); |
| 8 | +const bio = document.querySelector("#bio"); |
| 9 | +const repos = document.querySelector("#repo"); |
| 10 | +const followers = document.querySelector("#followers"); |
| 11 | +const following = document.querySelector("#following"); |
| 12 | + |
| 13 | +const fetchUser = (user_name) => { |
| 14 | + fetch(`https://api.github.com/users/${user_name}`) |
| 15 | + .then((data) => data.json()) |
| 16 | + .then((jsonData) => { |
| 17 | + if (jsonData.message == "Not found") { |
| 18 | + alert("User Not Found"); |
| 19 | + return; |
| 20 | + // console.log("Error" + jsonData.message); |
| 21 | + } else { |
| 22 | + userImg.innerHTML = ` |
| 23 | + <img src="${jsonData.avatar_url}" alt="avatar" id="prof-img"> |
| 24 | + <span class="name" id="name">${jsonData.name}</span> |
| 25 | + <a href="${jsonData.html_url}" id="username">@${jsonData.login}</a> |
| 26 | + `; |
| 27 | + bio.innerHTML = jsonData.bio; |
| 28 | + repos.innerHTML = jsonData.public_repos; |
| 29 | + followers.innerHTML = jsonData.followers; |
| 30 | + following.innerHTML = jsonData.following; |
| 31 | + } |
| 32 | + }) |
| 33 | + .catch((err) => { |
| 34 | + console.log("Catch" + err.message); |
| 35 | + }); |
| 36 | +}; |
| 37 | + |
| 38 | +const getUser = () => { |
| 39 | + let user_name = input_user.value.trim(); |
| 40 | + // trim will replace before and after spaces |
| 41 | + |
| 42 | + if (user_name.length == 0) { |
| 43 | + alert("Please enter a valid github username"); |
| 44 | + } else { |
| 45 | + fetchUser(user_name); |
| 46 | + } |
| 47 | + |
| 48 | + input_user.value = " "; |
| 49 | +}; |
0 commit comments