|  | 
|  | 1 | +const form = document.querySelector("form"); | 
|  | 2 | +const input = document.querySelector("input"); | 
|  | 3 | +const reposContainer = document.querySelector(".repos"); | 
|  | 4 | +const mainContainer = document.querySelector(".main-container"); | 
|  | 5 | + | 
|  | 6 | +const API = "https://api.github.com/users/"; | 
|  | 7 | + | 
|  | 8 | +async function fetchData(username) { | 
|  | 9 | +  try { | 
|  | 10 | +    const response = await fetch(`${API}${username}`); | 
|  | 11 | +    if (!response.ok) throw new Error(response.statusText); | 
|  | 12 | + | 
|  | 13 | +    const { | 
|  | 14 | +      avatar_url, | 
|  | 15 | +      bio, | 
|  | 16 | +      blog, | 
|  | 17 | +      company, | 
|  | 18 | +      followers, | 
|  | 19 | +      following, | 
|  | 20 | +      location, | 
|  | 21 | +      login, | 
|  | 22 | +      twitter_username, | 
|  | 23 | +    } = await response.json(); | 
|  | 24 | + | 
|  | 25 | +    const html = ` | 
|  | 26 | +    <div | 
|  | 27 | +      class="user-avatar" | 
|  | 28 | +      style="background: url(${avatar_url}) no-repeat center/cover" | 
|  | 29 | +    ></div> | 
|  | 30 | +    <p class="user-name">${login}</p> | 
|  | 31 | +    <button class="follow">Follow</button> | 
|  | 32 | +    <p class="user-bio">${bio}</p> | 
|  | 33 | +    <div class="followers-info"> | 
|  | 34 | +      <a href="#"> | 
|  | 35 | +        <i class="fa-solid fa-person"></i> | 
|  | 36 | +        <span class="followers">${followers}</span> follower | 
|  | 37 | +      </a> | 
|  | 38 | +
 | 
|  | 39 | +      <a href='#'> | 
|  | 40 | +        <span class="following">${following} </span> following | 
|  | 41 | +      </a> | 
|  | 42 | +
 | 
|  | 43 | +      <div class="icon-container"> | 
|  | 44 | +        <i class="fa-regular fa-building"></i> | 
|  | 45 | +        <a href="#" class="company">${company}</a> | 
|  | 46 | +      </div> | 
|  | 47 | +      <div class="icon-container"> | 
|  | 48 | +        <i class="fa-sharp fa-solid fa-location-dot"></i> | 
|  | 49 | +        <a href="#" class="location">${location}</a> | 
|  | 50 | +      </div> | 
|  | 51 | +      <div class="icon-container"> | 
|  | 52 | +        <i class="fa-regular fa-solid fa-link"></i> | 
|  | 53 | +        <a href="#" class="blog">${blog}</a> | 
|  | 54 | +      </div> | 
|  | 55 | +      <div class="icon-container"> | 
|  | 56 | +        <i class="fa-brands fa-twitter"></i> | 
|  | 57 | +        <a href="#" class="twitter_username">@${twitter_username}</a> | 
|  | 58 | +      </div> | 
|  | 59 | +    </div> | 
|  | 60 | +    `; | 
|  | 61 | + | 
|  | 62 | +    const section = document.createElement("section"); | 
|  | 63 | +    section.classList.add("about-user"); | 
|  | 64 | +    section.innerHTML = html; | 
|  | 65 | +    mainContainer.insertAdjacentElement("afterbegin", section); | 
|  | 66 | +  } catch (error) { | 
|  | 67 | +    console.error(error); | 
|  | 68 | +  } | 
|  | 69 | +} | 
|  | 70 | + | 
|  | 71 | +async function fetchRepos(username) { | 
|  | 72 | +  try { | 
|  | 73 | +    const response = await fetch(`${API}${username}/subscriptions`); | 
|  | 74 | +    if (!response.ok) throw new Error(response.statusText); | 
|  | 75 | +    const data = await response.json(); | 
|  | 76 | + | 
|  | 77 | +    data.forEach( | 
|  | 78 | +      ({ | 
|  | 79 | +        name, | 
|  | 80 | +        description, | 
|  | 81 | +        forks_count, | 
|  | 82 | +        language, | 
|  | 83 | +        watchers_count, | 
|  | 84 | +        git_url, | 
|  | 85 | +      }) => { | 
|  | 86 | +        const modifiedUrl = git_url | 
|  | 87 | +          .replace(/^git:/, "http:") | 
|  | 88 | +          .replace(/\.git$/, ""); | 
|  | 89 | + | 
|  | 90 | +        const singleElement = document.createElement("div"); | 
|  | 91 | +        singleElement.classList.add("repo-card"); | 
|  | 92 | +        const html = ` | 
|  | 93 | +        <a href=${modifiedUrl} class="repo-title">${name}</a> | 
|  | 94 | +        <p class="repo-subtitle">${description}</p> | 
|  | 95 | +        <div class="popularity"> | 
|  | 96 | +            <p class="technology-used">${language}</p> | 
|  | 97 | +            <p class="stars"><i class="fa-regular fa-star"></i>${watchers_count}</p> | 
|  | 98 | +            <img src="./git-fork_1.svg" alt="Fork SVG" class="fork-svg"> | 
|  | 99 | +            <span class="forked">${forks_count}</span> | 
|  | 100 | +        </div> | 
|  | 101 | +     | 
|  | 102 | +        <p class="pill">Public</p> | 
|  | 103 | +        `; | 
|  | 104 | +        singleElement.innerHTML = html; | 
|  | 105 | +        reposContainer.append(singleElement); | 
|  | 106 | +      } | 
|  | 107 | +    ); | 
|  | 108 | +  } catch (error) { | 
|  | 109 | +    console.error(error); | 
|  | 110 | +  } | 
|  | 111 | +} | 
|  | 112 | + | 
|  | 113 | +form.addEventListener("submit", async (e) => { | 
|  | 114 | +  e.preventDefault(); | 
|  | 115 | +  const val = input.value; | 
|  | 116 | + | 
|  | 117 | +  if (val) { | 
|  | 118 | +    try { | 
|  | 119 | +      await fetchData(val); | 
|  | 120 | +      await fetchRepos(val); | 
|  | 121 | +    } catch (error) { | 
|  | 122 | +      console.log(error); | 
|  | 123 | +    } finally { | 
|  | 124 | +      input.value = ""; | 
|  | 125 | +    } | 
|  | 126 | +  } | 
|  | 127 | + | 
|  | 128 | +  document | 
|  | 129 | +    .querySelector("input") | 
|  | 130 | +    .addEventListener("click", () => location.reload()); | 
|  | 131 | +}); | 
0 commit comments