|
| 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.textContent = value; |
| 28 | + } else { |
| 29 | + elem.setAttribute(key, value); |
| 30 | + } |
| 31 | + }); |
| 32 | + return elem; |
| 33 | + } |
| 34 | + |
| 35 | + // Display repository options in the header |
| 36 | + function selectOptions(nameOption) { |
| 37 | + const selectRepoHYF = document.getElementById('selectRepoHYF'); |
| 38 | + for (let i = 0; i < nameOption.length; i++) { |
| 39 | + createAndAppend('option', selectRepoHYF, { value: i, text: nameOption[i].name }); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Information on left side inside a table |
| 44 | + function displayInformation(element) { |
| 45 | + const container = document.getElementById('container'); |
| 46 | + const infoDiv = createAndAppend('div', container, { |
| 47 | + id: 'leftArea', |
| 48 | + class: 'left-div whiteframe', |
| 49 | + }); |
| 50 | + createAndAppend('table', infoDiv, { id: 'table' }); |
| 51 | + const table = document.getElementById('table'); |
| 52 | + createAndAppend('tbody', table, { id: 'tbody' }); |
| 53 | + function createTableRow(label, description) { |
| 54 | + const tRow = createAndAppend('tr', table); |
| 55 | + createAndAppend('td', tRow, { text: label, class: 'label' }); |
| 56 | + createAndAppend('td', tRow, { text: description }); |
| 57 | + } |
| 58 | + |
| 59 | + createTableRow('Repository: ', element.name); |
| 60 | + createTableRow('Description: ', element.description); |
| 61 | + createTableRow('Forks : ', element.forks); |
| 62 | + const date2 = new Date(element.updated_at).toLocaleString(); |
| 63 | + createTableRow('Updated: ', date2); |
| 64 | + } |
| 65 | + |
| 66 | + // Show contributors |
| 67 | + function contributorsList(element) { |
| 68 | + fetchJSON(element.contributors_url).then(data => { |
| 69 | + const container = document.getElementById('container'); |
| 70 | + createAndAppend('div', container, { |
| 71 | + id: 'rightArea', |
| 72 | + class: 'right-div whiteframe', |
| 73 | + }); |
| 74 | + const rightArea = document.getElementById('rightArea'); |
| 75 | + createAndAppend('p', rightArea, { |
| 76 | + text: 'Contributions', |
| 77 | + class: 'contributor-header', |
| 78 | + }); |
| 79 | + createAndAppend('ul', rightArea, { |
| 80 | + id: 'contList', |
| 81 | + class: 'contributor-list', |
| 82 | + }); |
| 83 | + let contributorURL; |
| 84 | + let contributorItem; |
| 85 | + let contributorData; |
| 86 | + const contList = document.getElementById('contList'); |
| 87 | + for (let i = 0; i < data.length; i++) { |
| 88 | + contributorURL = createAndAppend('a', contList, { |
| 89 | + href: data[i].html_url, |
| 90 | + target: '_blank', |
| 91 | + }); |
| 92 | + contributorItem = createAndAppend('li', contributorURL, { |
| 93 | + class: 'contributor-item', |
| 94 | + }); |
| 95 | + |
| 96 | + createAndAppend('img', contributorItem, { |
| 97 | + src: data[i].avatar_url, |
| 98 | + class: 'contributor-avatar', |
| 99 | + }); |
| 100 | + contributorData = createAndAppend('div', contributorItem, { |
| 101 | + class: 'contributor-data', |
| 102 | + }); |
| 103 | + createAndAppend('div', contributorData, { text: data[i].login }); |
| 104 | + createAndAppend('div', contributorData, { |
| 105 | + text: data[i].contributions, |
| 106 | + class: 'contributor-badge', |
| 107 | + }); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + async function main(url) { |
| 113 | + const root = document.getElementById('root'); |
| 114 | + try { |
| 115 | + const data = await fetchJSON(url); |
| 116 | + data.sort((a, b) => a.name.localeCompare(b.name)); |
| 117 | + createAndAppend('header', root, { id: 'topArea', class: 'header' }); |
| 118 | + const topArea = document.getElementById('topArea'); |
| 119 | + createAndAppend('p', topArea, { id: 'title', text: 'HYF Repositories' }); |
| 120 | + createAndAppend('select', topArea, { id: 'selectRepoHYF', class: 'repo-selector' }); |
| 121 | + createAndAppend('div', root, { id: 'container' }); |
| 122 | + selectOptions(data); |
| 123 | + displayInformation(data[0]); |
| 124 | + contributorsList(data[0]); |
| 125 | + |
| 126 | + document.getElementById('selectRepoHYF').onchange = function startListener() { |
| 127 | + const selectedItem = this.options[this.selectedIndex].value; |
| 128 | + const infoLeft = document.getElementById('leftArea'); |
| 129 | + infoLeft.parentNode.removeChild(infoLeft); |
| 130 | + const contributors = document.getElementById('rightArea'); |
| 131 | + contributors.parentNode.removeChild(contributors); |
| 132 | + |
| 133 | + displayInformation(data[selectedItem]); |
| 134 | + contributorsList(data[selectedItem]); |
| 135 | + }; |
| 136 | + } catch (err) { |
| 137 | + createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
| 142 | + |
| 143 | + window.onload = () => main(HYF_REPOS_URL); |
| 144 | +} |
0 commit comments