|
| 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.send(); |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + function createAndAppend(name, parent, options = {}) { |
| 21 | + const elem = document.createElement(name); |
| 22 | + parent.appendChild(elem); |
| 23 | + Object.keys(options).forEach(key => { |
| 24 | + const value = options[key]; |
| 25 | + if (key === 'text') { |
| 26 | + elem.textContent = value; |
| 27 | + } else { |
| 28 | + elem.setAttribute(key, value); |
| 29 | + } |
| 30 | + }); |
| 31 | + return elem; |
| 32 | + } |
| 33 | + function loadDetail(element) { |
| 34 | + const leftID = document.getElementById('leftID'); |
| 35 | + createAndAppend('ul', leftID, { id: 'leftDetailID', class: 'leftDetailClass' }); |
| 36 | + |
| 37 | + const leftDetailID = document.getElementById('leftDetailID'); |
| 38 | + createAndAppend('li', leftDetailID, { |
| 39 | + id: 'leftListID', |
| 40 | + class: 'leftListClass', |
| 41 | + text: 'Repository: ', |
| 42 | + }); |
| 43 | + |
| 44 | + const leftListID = document.getElementById('leftListID'); |
| 45 | + createAndAppend('a', leftListID, { |
| 46 | + text: element.name, |
| 47 | + href: element.html_url, |
| 48 | + id: 'repositoryNameID', |
| 49 | + class: 'repositoryNameClass', |
| 50 | + }); |
| 51 | + |
| 52 | + createAndAppend('li', leftDetailID, { |
| 53 | + id: 'descList', |
| 54 | + text: `Description: ${element.description}`, |
| 55 | + }); |
| 56 | + createAndAppend('li', leftDetailID, { text: `Forks: ${element.forks}` }); |
| 57 | + const updatedAt = new Date(element.updated_at).toLocaleString(); |
| 58 | + createAndAppend('li', leftDetailID, { text: `Updated: ${updatedAt}` }); |
| 59 | + } |
| 60 | + async function loadContributor(element) { |
| 61 | + const root = document.getElementById('root'); |
| 62 | + try { |
| 63 | + const data = await fetchJSON(element.contributors_url); |
| 64 | + const rightID = document.getElementById('rightID'); |
| 65 | + createAndAppend('p', rightID, { id: 'contTextID', text: 'Contributors' }); |
| 66 | + createAndAppend('ul', rightID, { id: 'contListID', class: 'contListClass' }); |
| 67 | + |
| 68 | + let contURL; |
| 69 | + let contData; |
| 70 | + let contDetail; |
| 71 | + const contListID = document.getElementById('contListID'); |
| 72 | + for (let i = 0; i < data.length; i++) { |
| 73 | + contURL = createAndAppend('a', contListID, { |
| 74 | + href: data[i].html_url, |
| 75 | + target: '_blank', |
| 76 | + id: 'contUrlID', |
| 77 | + class: 'contUrlClass', |
| 78 | + }); |
| 79 | + contData = createAndAppend('li', contURL, { |
| 80 | + id: 'contDataID', |
| 81 | + class: 'contDataClass', |
| 82 | + }); |
| 83 | + |
| 84 | + createAndAppend('img', contData, { |
| 85 | + src: data[i].avatar_url, |
| 86 | + id: 'contImgID', |
| 87 | + class: 'contImgClass', |
| 88 | + }); |
| 89 | + |
| 90 | + contDetail = createAndAppend('div', contData, { |
| 91 | + id: 'contDetailID', |
| 92 | + class: 'contDetailClass', |
| 93 | + }); |
| 94 | + createAndAppend('div', contDetail, { text: data[i].login }); |
| 95 | + createAndAppend('div', contDetail, { |
| 96 | + text: data[i].contributions, |
| 97 | + id: 'contBadgeID', |
| 98 | + class: 'contBadgeClass', |
| 99 | + }); |
| 100 | + } |
| 101 | + } catch (err) { |
| 102 | + createAndAppend('div', root, { |
| 103 | + id: 'err.message', |
| 104 | + class: 'alert-error', |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + async function main(url) { |
| 110 | + const root = document.getElementById('root'); |
| 111 | + try { |
| 112 | + const data = await fetchJSON(url); |
| 113 | + createAndAppend('header', root, { |
| 114 | + id: 'topBoxID', |
| 115 | + class: 'topBoxClass', |
| 116 | + }); |
| 117 | + const topBoxID = document.getElementById('topBoxID'); |
| 118 | + createAndAppend('p', topBoxID, { |
| 119 | + id: 'topTextID', |
| 120 | + class: 'topTextClass', |
| 121 | + text: 'HYF Repositories', |
| 122 | + }); |
| 123 | + |
| 124 | + createAndAppend('div', root, { id: 'mainContainerID', class: 'mainContainerClass' }); |
| 125 | + |
| 126 | + const mainContainerID = document.getElementById('mainContainerID'); |
| 127 | + createAndAppend('div', mainContainerID, { id: 'leftID', class: 'leftClass' }); |
| 128 | + createAndAppend('div', mainContainerID, { id: 'rightID', class: 'rightClass' }); |
| 129 | + createAndAppend('select', topBoxID, { id: 'repsSelectID', Class: 'repsSelectClass' }); |
| 130 | + data.sort((a, b) => a.name.localeCompare(b.name)); |
| 131 | + |
| 132 | + const repsSelectID = document.getElementById('repsSelectID'); |
| 133 | + for (let index = 0; index < data.length; index++) { |
| 134 | + createAndAppend('option', repsSelectID, { |
| 135 | + value: index, |
| 136 | + text: data[index].name, |
| 137 | + id: 'optionID', |
| 138 | + class: 'optionClass', |
| 139 | + }); |
| 140 | + } |
| 141 | + const selector = document.getElementById('repsSelectID'); |
| 142 | + |
| 143 | + loadDetail(data[0]); |
| 144 | + loadContributor(data[0]); |
| 145 | + |
| 146 | + selector.onchange = () => { |
| 147 | + const leftID = document.getElementById('leftID'); |
| 148 | + leftID.parentNode.removeChild(leftID); |
| 149 | + |
| 150 | + const rightID = document.getElementById('rightID'); |
| 151 | + rightID.parentNode.removeChild(rightID); |
| 152 | + |
| 153 | + const i = selector.value; |
| 154 | + |
| 155 | + createAndAppend('div', mainContainerID, { id: 'leftID', class: 'leftClass' }); |
| 156 | + createAndAppend('div', mainContainerID, { id: 'rightID', class: 'rightClass' }); |
| 157 | + loadDetail(data[i]); |
| 158 | + loadContributor(data[i]); |
| 159 | + }; |
| 160 | + } catch (error) { |
| 161 | + createAndAppend('div', root, { text: error.message, class: 'alert-error' }); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + const HYF_REPS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
| 166 | + |
| 167 | + window.onload = () => main(HYF_REPS_URL); |
| 168 | +} |
0 commit comments