|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -{ |
4 |
| - function fetchJSON(url, cb) { |
5 |
| - const xhr = new XMLHttpRequest(); |
6 |
| - xhr.open('GET', url); |
7 |
| - xhr.responseType = 'json'; |
8 |
| - xhr.onload = () => { |
9 |
| - if (xhr.status < 400) { |
10 |
| - cb(null, xhr.response); |
11 |
| - } else { |
12 |
| - cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); |
13 |
| - } |
14 |
| - }; |
15 |
| - xhr.onerror = () => cb(new Error('Network request failed')); |
16 |
| - xhr.send(); |
17 |
| - } |
18 |
| - |
19 |
| - function createAndAppend(name, parent, options = {}) { |
20 |
| - const elem = document.createElement(name); |
21 |
| - parent.appendChild(elem); |
22 |
| - Object.keys(options).forEach(key => { |
23 |
| - const value = options[key]; |
24 |
| - if (key === 'text') { |
25 |
| - elem.textContent = value; |
26 |
| - } else { |
27 |
| - elem.setAttribute(key, value); |
28 |
| - } |
29 |
| - }); |
30 |
| - return elem; |
31 |
| - } |
32 |
| - |
33 |
| - function main(url) { |
34 |
| - fetchJSON(url, (err, data) => { |
35 |
| - const root = document.getElementById('root'); |
36 |
| - if (err) { |
37 |
| - createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
38 |
| - } else { |
39 |
| - createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) }); |
40 |
| - } |
| 3 | +const repoSelector = document.querySelector('#repoSelect'); |
| 4 | +const contribDiv = document.querySelector('#contribDiv'); |
| 5 | +const repoDiv = document.querySelector('#repo'); |
| 6 | + |
| 7 | +function fetchJSON(url, cb) { |
| 8 | + const xhr = new XMLHttpRequest(); |
| 9 | + xhr.open('GET', url); |
| 10 | + xhr.setRequestHeader = 'Catsudemo https://api.github.com'; |
| 11 | + xhr.responseType = 'json'; |
| 12 | + xhr.onload = () => { |
| 13 | + if (xhr.status < 400) { |
| 14 | + cb(null, xhr.response); |
| 15 | + } else { |
| 16 | + cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); |
| 17 | + } |
| 18 | + }; |
| 19 | + xhr.onerror = () => cb(new Error('Network request failed')); |
| 20 | + xhr.send(); |
| 21 | +} |
| 22 | + |
| 23 | +function createAndAppend(name, parent, options = {}) { |
| 24 | + const elem = document.createElement(name); |
| 25 | + parent.appendChild(elem); |
| 26 | + Object.keys(options).forEach(key => { |
| 27 | + const value = options[key]; |
| 28 | + if (key === 'text') { |
| 29 | + elem.textContent = value; |
| 30 | + } else { |
| 31 | + elem.setAttribute(key, value); |
| 32 | + } |
| 33 | + }); |
| 34 | + return elem; |
| 35 | +} |
| 36 | + |
| 37 | +function initPage(data, repoSelector) { |
| 38 | + buildSelect(data, repoSelector); |
| 39 | +} |
| 40 | + |
| 41 | +function buildSelect(data, repoSelector) { |
| 42 | + data |
| 43 | + .map(repo => repo.name) |
| 44 | + .sort() |
| 45 | + .forEach(name => { |
| 46 | + createAndAppend('OPTION', repoSelector, { text: name, value: name }); |
41 | 47 | });
|
42 |
| - } |
| 48 | +} |
43 | 49 |
|
44 |
| - const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
| 50 | +function main(url) { |
| 51 | + fetchJSON(url, (err, data) => { |
| 52 | + const root = document.getElementById('root'); |
| 53 | + if (err) { |
| 54 | + createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
| 55 | + } else { |
| 56 | + // createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) }); |
| 57 | + // buildSelect(data,repoSelector) |
| 58 | + initPage(data, repoSelector); |
| 59 | + } |
| 60 | + }); |
| 61 | +} |
45 | 62 |
|
46 |
| - window.onload = () => main(HYF_REPOS_URL); |
| 63 | +const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
| 64 | + |
| 65 | +function displayContrib(contributor, contributions, avatar) { |
| 66 | + const eachPersonUl = document.createElement('ul'); |
| 67 | + const contributorName = document.createElement('li'); |
| 68 | + contributorName.innerText = `Username: ${contributor}`; |
| 69 | + eachPersonUl.appendChild(contributorName); |
| 70 | + const eachContributions = document.createElement('li'); |
| 71 | + eachContributions.innerText = `Contributions: ${contributions}`; |
| 72 | + eachPersonUl.appendChild(eachContributions); |
| 73 | + contribDiv.appendChild(eachPersonUl); |
| 74 | + const eachAvatar = document.createElement('IMG'); |
| 75 | + eachAvatar.innerhtml = `<img src=${avatar}'>`; |
| 76 | + contribDiv.appendChild(eachAvatar); |
| 77 | +} |
| 78 | + |
| 79 | +function displayRepo(repository, description, forks, updated) { |
| 80 | + const eachRepoUl = document.createElement('ul'); |
| 81 | + const eachName = document.createElement('li'); |
| 82 | + eachName.innerText = `Repository: ${repository}`; |
| 83 | + eachRepoUl.appendChild(eachName); |
| 84 | + const eachDescription = document.createElement('li'); |
| 85 | + eachDescription.innerText = `Contributions: ${description}`; |
| 86 | + eachRepoUl.appendChild(eachDescription); |
| 87 | + const eachForks = document.createElement('li'); |
| 88 | + eachForks.innerText = `Forks: ${forks}`; |
| 89 | + eachRepoUl.appendChild(eachForks); |
| 90 | + const eachUpdated = document.createElement('li'); |
| 91 | + eachUpdated.innerText = `Updated: ${updated}`; |
| 92 | + eachRepoUl.appendChild(eachUpdated); |
| 93 | + repoDiv.appendChild(eachRepoUl); |
47 | 94 | }
|
| 95 | + |
| 96 | +const selectElement = document.querySelector('.selector'); |
| 97 | + |
| 98 | +selectElement.addEventListener('change', event => { |
| 99 | + const result = document.querySelector('.result'); |
| 100 | + let repo = document.getElementById('repoSelect').value; |
| 101 | + let repoURL = `https://api.github.com/repos/HackYourFuture/${repo}`; |
| 102 | + let contribURL = `https://api.github.com/repos/HackYourFuture/${repo}/contributors`; |
| 103 | + |
| 104 | + main(HYF_REPOS_URL); |
| 105 | + |
| 106 | + fetchJSON(repoURL, (err, data) => { |
| 107 | + const root = document.getElementById('root'); |
| 108 | + if (err) { |
| 109 | + createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
| 110 | + } else { |
| 111 | + repoDiv.innerHTML = ''; |
| 112 | + let repository = data.name; |
| 113 | + let description = data.description; |
| 114 | + let forks = data.forks; |
| 115 | + let updated = data.updated_at; |
| 116 | + displayRepo(repository, description, forks, updated); |
| 117 | + } |
| 118 | + console.log('Here is the repodata'); |
| 119 | + }); |
| 120 | + |
| 121 | + fetchJSON(contribURL, (err, data) => { |
| 122 | + const root = document.getElementById('root'); |
| 123 | + if (err) { |
| 124 | + createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
| 125 | + } else { |
| 126 | + contribDiv.innerHTML = ''; |
| 127 | + data.forEach(object => { |
| 128 | + let contributor = object.login; |
| 129 | + let contributions = object.contributions; |
| 130 | + let avatar = object.avatar_url; |
| 131 | + displayContrib(contributor, contributions, avatar); |
| 132 | + }); |
| 133 | + console.log('Here is the contribdata'); |
| 134 | + } |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +window.onload = () => main(HYF_REPOS_URL); |
0 commit comments