|
| 1 | +"use strict"; |
| 2 | +const HYF_REPOS_URL = "https://api.github.com/orgs/HackYourFuture/repos"; |
| 3 | +function fetchJSON(endPoint, callback) { |
| 4 | + fetch(endPoint) |
| 5 | + .then((response) => { |
| 6 | + if (response.status >= 200 && response.status < 400) { |
| 7 | + return response.json(); |
| 8 | + } else { |
| 9 | + throw "HTTP ERROR!"; |
| 10 | + } |
| 11 | + }) |
| 12 | + .then((jsonData) => { |
| 13 | + callback(jsonData, arguments[2]); |
| 14 | + }) |
| 15 | + .catch((error) => { |
| 16 | + document.getElementById("root").innerHTML = error; |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +function renderRepositories(jsonData, filteredItem) { |
| 21 | + const repositoryInfoList = jsonData.map((element) => { |
| 22 | + const repositoryInfo = {}; |
| 23 | + repositoryInfo.Repositories = element.name; |
| 24 | + repositoryInfo.Description = element.description; |
| 25 | + repositoryInfo.Forks = element.forks; |
| 26 | + repositoryInfo.Updated = new Date(element.updated_at).toLocaleString(); |
| 27 | + return repositoryInfo; |
| 28 | + }); |
| 29 | + |
| 30 | + // if not selected repository, get the first element from the array |
| 31 | + let filteredJSONData = jsonData[0]; |
| 32 | + let repository = repositoryInfoList.slice(0, 1); |
| 33 | + // if selected repository, filteredItem is exist |
| 34 | + if (filteredItem) { |
| 35 | + filteredJSONData = jsonData.filter((item) => item.name === filteredItem)[0]; |
| 36 | + |
| 37 | + repository = repositoryInfoList.filter( |
| 38 | + (item) => item.Repositories === filteredItem |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // following codes show the repository information |
| 43 | + const repo = document.getElementById("repo"); |
| 44 | + repo.innerHTML = ""; |
| 45 | + |
| 46 | + const repoDiv = document.createElement("div"); |
| 47 | + repoDiv.className = "repoInfo"; |
| 48 | + |
| 49 | + repository.forEach((element) => { |
| 50 | + Object.keys(element).forEach(function (key) { |
| 51 | + const item = document.createElement("item"); |
| 52 | + item.className = "item"; |
| 53 | + |
| 54 | + const label = document.createElement("label"); |
| 55 | + label.innerText = key + ": "; |
| 56 | + |
| 57 | + const value = document.createElement("p"); |
| 58 | + if (key === "Repositories") { |
| 59 | + const valueLink = document.createElement("a"); |
| 60 | + valueLink.href = filteredJSONData.html_url; |
| 61 | + valueLink.target = "_blank"; |
| 62 | + valueLink.innerText = element[key]; |
| 63 | + |
| 64 | + value.appendChild(valueLink); |
| 65 | + } else { |
| 66 | + value.innerText = element[key]; |
| 67 | + } |
| 68 | + |
| 69 | + item.appendChild(label); |
| 70 | + item.appendChild(value); |
| 71 | + repoDiv.appendChild(item); |
| 72 | + }); |
| 73 | + repo.appendChild(repoDiv); |
| 74 | + }); |
| 75 | + |
| 76 | + // this code calls the contributors who contributed to this repository |
| 77 | + fetchJSON(filteredJSONData.contributors_url, renderContributors); |
| 78 | +} |
| 79 | + |
| 80 | +function renderContributors(contributorJSON) { |
| 81 | + // following codes show the contributors information |
| 82 | + const contributorList = document.getElementById("contributors"); |
| 83 | + contributorList.innerHTML = ""; |
| 84 | + |
| 85 | + const header = document.createElement("h3"); |
| 86 | + header.innerHTML = "Contributions"; |
| 87 | + contributorList.appendChild(header); |
| 88 | + |
| 89 | + contributorJSON.forEach((contributor) => { |
| 90 | + const contributorDiv = document.createElement("div"); |
| 91 | + contributorDiv.className = "contributor"; |
| 92 | + |
| 93 | + const contributorInfoDiv = document.createElement("div"); |
| 94 | + contributorInfoDiv.className = "contributor-info"; |
| 95 | + |
| 96 | + const image = document.createElement("img"); |
| 97 | + image.src = contributor.avatar_url; |
| 98 | + |
| 99 | + const name = document.createElement("p"); |
| 100 | + const nameHref = document.createElement("a"); |
| 101 | + nameHref.href = contributor.html_url; |
| 102 | + nameHref.target = "_blank"; |
| 103 | + nameHref.innerText = contributor.login; |
| 104 | + name.appendChild(nameHref); |
| 105 | + |
| 106 | + contributorInfoDiv.appendChild(image); |
| 107 | + contributorInfoDiv.appendChild(name); |
| 108 | + |
| 109 | + const contributionsCount = document.createElement("p"); |
| 110 | + contributionsCount.className = "contributor-count"; |
| 111 | + contributionsCount.innerText = contributor.contributions; |
| 112 | + |
| 113 | + contributorDiv.appendChild(contributorInfoDiv); |
| 114 | + contributorDiv.appendChild(contributionsCount); |
| 115 | + |
| 116 | + contributorList.appendChild(contributorDiv); |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +function renderSelect(jsonData) { |
| 121 | + // following codes list the repository names |
| 122 | + jsonData.sort((a, b) => a.name.localeCompare(b.name)); |
| 123 | + let selectList = document.getElementById("selectRepository"); |
| 124 | + for (let i = 0; i < jsonData.length; i++) { |
| 125 | + let option = document.createElement("option"); |
| 126 | + option.value = i; |
| 127 | + option.innerHTML = jsonData[i].name; |
| 128 | + selectList.appendChild(option); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +window.onload = () => { |
| 133 | + // At start-up my application will display information about the first repository |
| 134 | + fetchJSON(HYF_REPOS_URL, renderRepositories); |
| 135 | + |
| 136 | + // At start-up, it will be load select information |
| 137 | + fetchJSON(HYF_REPOS_URL, renderSelect); |
| 138 | + |
| 139 | + // this code will be worked if select option is changed |
| 140 | + const repositories = document.getElementById("selectRepository"); |
| 141 | + let selectedOption = ""; |
| 142 | + repositories.addEventListener("change", (event) => { |
| 143 | + selectedOption = event.target.selectedOptions[0].text; |
| 144 | + fetchJSON(HYF_REPOS_URL, renderRepositories, selectedOption); |
| 145 | + }); |
| 146 | +}; |
0 commit comments