|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +{ |
| 4 | + function fetchJSON(url) { |
| 5 | + return new Promise((resolve, reject) => { |
| 6 | + const xhr = new XMLHttpRequest(); |
| 7 | + xhr.open('GET', url); |
| 8 | + xhr.responseType = 'json'; |
| 9 | + xhr.onload = () => { |
| 10 | + if (xhr.status < 400) { |
| 11 | + resolve(xhr.response); |
| 12 | + } else { |
| 13 | + reject(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); |
| 14 | + } |
| 15 | + }; |
| 16 | + xhr.onerror = () => reject(new Error('Network request failed')); |
| 17 | + xhr.send(); |
| 18 | + }); |
| 19 | + } |
| 20 | + |
| 21 | + function createAndAppend(name, parent, options = {}) { |
| 22 | + const elem = document.createElement(name); |
| 23 | + parent.appendChild(elem); |
| 24 | + Object.keys(options).forEach((key) => { |
| 25 | + const value = options[key]; |
| 26 | + if (key === 'text') { |
| 27 | + elem.innerText = value; |
| 28 | + } else { |
| 29 | + elem.setAttribute(key, value); |
| 30 | + } |
| 31 | + }); |
| 32 | + return elem; |
| 33 | + } |
| 34 | + const root = document.getElementById('root'); |
| 35 | + |
| 36 | + function repo(data) { |
| 37 | + data.sort((a, b) => a.name.localeCompare(b.name)); |
| 38 | + const root = document.getElementById('root'); |
| 39 | + const div = createAndAppend("div", root, { id: "mainCon" }); |
| 40 | + createAndAppend("p", div, { text: "HYF Repositories", id: "HYF_Repositories" }); |
| 41 | + const select = createAndAppend("select", div); |
| 42 | + for (let i = 0; i < data.length; i++) { |
| 43 | + createAndAppend("option", select, { text: data[i].name, value: i, class: "options" }); |
| 44 | + } |
| 45 | + select.addEventListener("change", () => repoInfo(data, select)); |
| 46 | + repoInfo(data, select); |
| 47 | + } |
| 48 | + |
| 49 | + function repoInfo(data, select) { |
| 50 | + if (document.getElementById("info")) { |
| 51 | + document.getElementById("info").remove(); |
| 52 | + } |
| 53 | + const link = data[select.value].html_url; |
| 54 | + const name = data[select.value].name; |
| 55 | + const description = data[select.value].description; |
| 56 | + const forks = data[select.value].forks; |
| 57 | + const newDate = new Date(data[select.value].updated_at); |
| 58 | + const div = createAndAppend("div", root, { id: "info" }); |
| 59 | + const ul = createAndAppend("ul", div, { id: "repository_info" }); |
| 60 | + const li = createAndAppend("li", ul, { text: "Name: ", class: "repository_details" }); |
| 61 | + createAndAppend("a", li, { text: name, href: link, target: "_blank" }); |
| 62 | + createAndAppend("li", ul, { text: "Description: " + description, class: "repository_details" }); |
| 63 | + createAndAppend("li", ul, { text: "Forks: " + forks, class: "repository_details" }); |
| 64 | + createAndAppend("li", ul, { text: "Updated: " + newDate.toLocaleString('en-GB', { timeZone: 'UTC' }), class: "repository_details" }); |
| 65 | + fetchJSON(data[select.value].contributors_url) |
| 66 | + .then((data) => { contributors(data); }); |
| 67 | + } |
| 68 | + function contributors(data) { |
| 69 | + if (document.getElementById("contributors")) { |
| 70 | + document.getElementById("contributors").remove(); |
| 71 | + } |
| 72 | + |
| 73 | + const div = createAndAppend("div", root, { id: "contributors" }); |
| 74 | + const h1 = createAndAppend("h1", div, { text: "Contributors" }); |
| 75 | + const ul = createAndAppend("ul", div, { id: "contributors_info" }); |
| 76 | + for (let i = 0; i < data.length; i++) { |
| 77 | + let li = createAndAppend("li", ul, { id: "contributor" }); |
| 78 | + let img = createAndAppend("img", li, { src: data[i].avatar_url }); |
| 79 | + let contname = data[i].login; |
| 80 | + let p = createAndAppend("p", li, { id: "name" }); |
| 81 | + let link = data[i].html_url; |
| 82 | + createAndAppend("a", p, { text: contname, href: link, target: "_blank", id: "links" }); |
| 83 | + let emptyDiv = createAndAppend("div", li, { id: "contributions" }); |
| 84 | + let p1 = createAndAppend("p", emptyDiv, { text: data[i].contributions, id: "numbers" }); |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + function renderError(error) { |
| 90 | + console.log(error); |
| 91 | + } |
| 92 | + |
| 93 | + function main(url) { |
| 94 | + fetchJSON(url) |
| 95 | + .then(data => repo(data)) |
| 96 | + .catch(err => renderError(err)); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
| 101 | + window.onload = () => main(HYF_REPOS_URL); |
| 102 | +} |
0 commit comments