|
| 1 | +'use strict'; |
| 2 | +async function main(){ |
| 3 | + try { |
| 4 | + console.log('main!'); |
| 5 | + const HyfReposHttps = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
| 6 | + const reposList = await fetchJSON(HyfReposHttps); |
| 7 | + xhrCallback(reposList); |
| 8 | + }catch(err) { |
| 9 | + renderError(err) |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +function fetchJSON(url) { |
| 15 | + console.log('calling fetch json'); |
| 16 | + return new Promise((resolve, reject) => { |
| 17 | + const xhr = new XMLHttpRequest(); |
| 18 | + xhr.open('GET', url); |
| 19 | + xhr.responseType = 'json'; |
| 20 | + xhr.onreadystatechange = () => { |
| 21 | + if (xhr.readyState === 4) { |
| 22 | + if (xhr.status < 400) { |
| 23 | + resolve(xhr.response); |
| 24 | + } else { |
| 25 | + reject(new Error(xhr.statusText)); |
| 26 | + } |
| 27 | + } |
| 28 | + }; |
| 29 | + xhr.send(); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function renderError(err) { |
| 34 | + console.error(err.message); |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +// Callback that handles response from server |
| 39 | +function xhrCallback(data){ |
| 40 | + console.log('calling xhrcallback'); |
| 41 | + addSelectElementOptions(data); |
| 42 | + checkSelectChanging(data); |
| 43 | +} |
| 44 | + |
| 45 | +// Add options to select element |
| 46 | +function addSelectElementOptions(arr){ |
| 47 | + console.log('calling addSelectElementOptions'); |
| 48 | + let selectElement = document.getElementById("repositories"); |
| 49 | + arr.forEach(rep => { |
| 50 | + let option = document.createElement('option'); |
| 51 | + option.text = rep.name; |
| 52 | + option.value = rep.id; |
| 53 | + selectElement.appendChild(option); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +//Function that works if select element change |
| 58 | +function checkSelectChanging (arr) { |
| 59 | + try { |
| 60 | + console.log('calling checkSelectChanging'); |
| 61 | + let selectElement = document.getElementById("repositories"); |
| 62 | + selectElement.addEventListener("change", async function(){ |
| 63 | + const selectValue = selectElement.value; |
| 64 | + const repo = arr.filter(repo => repo.id == selectValue)[0]; |
| 65 | + renderRepositoryInfo(repo); |
| 66 | + const repoContributersUrl = repo.contributors_url; |
| 67 | + const contributersList = await fetchJSON(repoContributersUrl); |
| 68 | + renderRepositoryContributers(contributersList); |
| 69 | + }); |
| 70 | + }catch(err) { |
| 71 | + renderError(err) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +function renderRepositoryInfo(selectedRepository){ |
| 77 | + // let repo = arr.filter(repo => repo.id == value)[0]; |
| 78 | + console.log('calling renderRepositoryInfo'); |
| 79 | + const repositoriesInfoElement = document.querySelector('#repo_info'); |
| 80 | + // while( repositoriesInfoElement.hasChildNodes()){ |
| 81 | + // repositoriesInfoElement.removeChild(repositoriesInfoElement.firstChild); |
| 82 | + // } |
| 83 | + repositoriesInfoElement.innerHTML =``; |
| 84 | + repositoriesInfoElement.innerHTML =`<div class="repoContainer"> |
| 85 | + <strong>Repository: </strong><span><a href=${selectedRepository.html_url}>${selectedRepository.name}</a></span><br> |
| 86 | + <strong>Description: </strong><span>${selectedRepository.description}</span><br> |
| 87 | + <strong>Forks: </strong><span>${selectedRepository.forks}</span><br> |
| 88 | + <strong>Updated: </strong><span>${selectedRepository.updated_at}</span><br> |
| 89 | + </div>`; |
| 90 | + // const repoContainer = document.createElement('div'); |
| 91 | + // repoContainer.setAttribute('class', 'repoContainer'); |
| 92 | + |
| 93 | + // const repoLink = document.createElement('a'); |
| 94 | + // repoLink.setAttribute('target', '_blank'); |
| 95 | + // repoLink.href = selectedRepository.html_url; |
| 96 | + // repoLink.innerText = selectedRepository.name; |
| 97 | + |
| 98 | + // const repoDescription = document.createElement('h3'); |
| 99 | + // repoDescription.innerText = "Description: " + selectedRepository.description; |
| 100 | + |
| 101 | + |
| 102 | + // const repoForks = document.createElement('h3'); |
| 103 | + // repoForks.innerText = "Forks: " + selectedRepository.forks |
| 104 | + |
| 105 | + // const repoUpdate = document.createElement('h3'); |
| 106 | + // repoUpdate.innerText = "Last Updated: " + selectedRepository.updated_at; |
| 107 | + |
| 108 | + // repoContainer.appendChild(repoLink); |
| 109 | + // repoContainer.appendChild(repoDescription); |
| 110 | + // repoContainer.appendChild(repoForks); |
| 111 | + // repoContainer.appendChild(repoUpdate); |
| 112 | + // repositoriesInfoElement.appendChild(repoContainer); |
| 113 | +} |
| 114 | + |
| 115 | +function renderRepositoryContributers(response){ |
| 116 | + console.log('calling renderRepositoryContributers'); |
| 117 | + const repoContributers = document.querySelector('#repo_contributors'); |
| 118 | + repoContributers.innerHTML =``; |
| 119 | + response.forEach(function(item){ |
| 120 | + repoContributers.innerHTML += `<div class="contributorContainer"> |
| 121 | + <div class="contributorList"><h3>${item.login}</h3></div> |
| 122 | + <div class="contributorList"><img src=${item.avatar_url}></div> |
| 123 | + <div class="contributorList"><h5>${item.contributions}</h4></div> |
| 124 | + </div>`; |
| 125 | + }); |
| 126 | + // const contributorsListElement = document.querySelector('#contributorList'); |
| 127 | + // const contributorsListElement = document.querySelector('#repo_contributors'); |
| 128 | + // while( contributorsListElement.hasChildNodes()){ |
| 129 | + // contributorsListElement.removeChild(contributorsListElement.firstChild); |
| 130 | + // } |
| 131 | + |
| 132 | + // contributers.forEach(contributor => { |
| 133 | + // const contributorContainer = document.createElement('div'); |
| 134 | + // contributorContainer.setAttribute('class', 'contributorContainer'); |
| 135 | + // const listElement = document.createElement('h2'); |
| 136 | + // listElement.innerText = contributor.login; |
| 137 | + // const imgElement = document.createElement('img'); |
| 138 | + // imgElement.src = contributor.avatar_url; |
| 139 | + // const txtElement = document.createElement('h3'); |
| 140 | + // txtElement.innerText = contributor.contributions; |
| 141 | + // contributorContainer.appendChild(listElement); |
| 142 | + // contributorContainer.appendChild(imgElement); |
| 143 | + // contributorContainer.appendChild(txtElement); |
| 144 | + // contributorsListElement.appendChild(contributorContainer); |
| 145 | + // }); |
| 146 | +} |
0 commit comments