|
1 |
| -'use strict'; |
2 |
| - |
| 1 | +"use strict"; |
3 | 2 | {
|
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(); |
| 3 | + function fetchJSON(url) { |
| 4 | + return new Promise((resolve, reject) => { |
| 5 | + const xhr = new XMLHttpRequest(); |
| 6 | + xhr.open("GET", url); |
| 7 | + xhr.responseType = "json"; |
| 8 | + xhr.onload = () => { |
| 9 | + if (xhr.status < 400) { |
| 10 | + resolve(xhr.response); |
| 11 | + } else { |
| 12 | + reject(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); |
| 13 | + } |
| 14 | + }; |
| 15 | + xhr.send(); |
| 16 | + }); |
17 | 17 | }
|
18 | 18 |
|
19 | 19 | function createAndAppend(name, parent, options = {}) {
|
20 | 20 | const elem = document.createElement(name);
|
21 | 21 | parent.appendChild(elem);
|
22 | 22 | Object.keys(options).forEach(key => {
|
23 | 23 | const value = options[key];
|
24 |
| - if (key === 'text') { |
25 |
| - elem.textContent = value; |
| 24 | + if (key === "text") { |
| 25 | + elem.innerText = value; |
26 | 26 | } else {
|
27 | 27 | elem.setAttribute(key, value);
|
28 | 28 | }
|
29 | 29 | });
|
30 | 30 | return elem;
|
31 | 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('header', root, { id: 'divHead', class: 'header' }); |
| 40 | + createAndAppend('p', divHead, { text: 'HYF Repositories' }); |
| 41 | + |
| 42 | + createAndAppend('select', divHead, { |
| 43 | + id: 'selectElem', |
| 44 | + class: 'selector', |
| 45 | + }); |
| 46 | + createAndAppend("div", root, { id: "container" }); |
| 47 | + createOptions(data); |
| 48 | + displayInformation(data[0]); |
| 49 | + contributorsList(data[0]); |
| 50 | + |
32 | 51 |
|
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) }); |
| 52 | + document.getElementById("selectElem").onchange = function() { |
| 53 | + let selectedItemIndex = this.options[this.selectedIndex].value; |
| 54 | + let infoDiv = document.getElementById("divLeft"); |
| 55 | + infoDiv.parentNode.removeChild(infoDiv); |
| 56 | + let contributors = document.getElementById("divRight"); |
| 57 | + contributors.parentNode.removeChild(contributors); |
| 58 | + |
| 59 | + displayInformation(data[selectedItemIndex]); |
| 60 | + contributorsList(data[selectedItemIndex]); |
| 61 | + }; |
40 | 62 | }
|
41 |
| - }); |
| 63 | + }); |
42 | 64 | }
|
43 | 65 |
|
44 |
| - const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
| 66 | + const HYF_REPOS_URL = |
| 67 | + "https://api.github.com/orgs/HackYourFuture/repos?per_page=100"; |
45 | 68 |
|
46 | 69 | window.onload = () => main(HYF_REPOS_URL);
|
| 70 | + |
| 71 | + function createOptions(wholeData) { |
| 72 | + for (let i = 0; i < wholeData.length; i++) { |
| 73 | + createAndAppend("option", selectElem, { |
| 74 | + value: i, |
| 75 | + text: wholeData[i].name, |
| 76 | + class: "optionsClass" |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + //Contributors list |
| 82 | + function contributorsList(element) { |
| 83 | + fetchJSON(element.contributors_url).then(data => { |
| 84 | + createAndAppend("div", container, { |
| 85 | + id: "divRight", |
| 86 | + class: "right-div whiteframe" |
| 87 | + }); |
| 88 | + createAndAppend("p", divRight, { |
| 89 | + text: "Contributions", |
| 90 | + class: "contributor-header" |
| 91 | + }); |
| 92 | + createAndAppend("ul", divRight, { |
| 93 | + id: "contList", |
| 94 | + class: "contributor-list" |
| 95 | + }); |
| 96 | + let link; |
| 97 | + let listItem; |
| 98 | + let contDataDiv; |
| 99 | + for (let i = 0; i < data.length; i++) { |
| 100 | + link = createAndAppend("a", contList, { |
| 101 | + href: data[i].html_url, |
| 102 | + target: "_blank" |
| 103 | + }); |
| 104 | + listItem = createAndAppend("li", link, { |
| 105 | + class: "contributor-item" |
| 106 | + }); |
| 107 | + |
| 108 | + createAndAppend("img", listItem, { |
| 109 | + src: data[i].avatar_url, |
| 110 | + class: "contributor-avatar" |
| 111 | + }); |
| 112 | + contDataDiv = createAndAppend("div", listItem, { |
| 113 | + class: "contributor-data" |
| 114 | + }); |
| 115 | + createAndAppend("div", contDataDiv, { text: data[i].login }); |
| 116 | + createAndAppend("div", contDataDiv, { |
| 117 | + text: data[i].contributions, |
| 118 | + class: "contributor-badge" |
| 119 | + }); |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + //Info |
| 127 | + function displayInformation(element) { |
| 128 | + let infoDiv = createAndAppend("div", container, { |
| 129 | + id: "Left", |
| 130 | + class: "left-div whiteframe" |
| 131 | + }); |
| 132 | + createAndAppend("table", infoDiv, { id: "table" }); |
| 133 | + createAndAppend("tbody", table, { id: "tbody" }); |
| 134 | + createRow("Repository: ", element.name); |
| 135 | + createRow("Description: ", element.description); |
| 136 | + createRow("Forks : ", element.forks); |
| 137 | + createRow("Updated: ", element.updated_at); |
| 138 | + |
| 139 | + function createRow(label, description) { |
| 140 | + let tRow = createAndAppend("tr", table); |
| 141 | + createAndAppend("td", tRow, { text: label, class: "label" }); |
| 142 | + createAndAppend("td", tRow, { text: description }); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +/* const top =createAndAppend(div,root),{id:"top",text:"HYf Repo",class:} |
| 150 | +const right = createAndAppend(div,root),{id:"right"}); |
| 151 | +let select = createAndAPPEND (div,root),{id:"top",text:"HYf Repo",class:} |
| 152 | +createAndAppend(div,root),{id:"right",text:"hello world",class:} |
| 153 | + |
| 154 | + |
| 155 | +createAndAppend(div,root),{id:"right",text:"hello world",class:} |
| 156 | + |
| 157 | +let name = createAndAppend(div,root),{id:"right",text:"hello world",class:} |
| 158 | + |
| 159 | + |
| 160 | +//Selection, right,id : selectlist same as line 1 |
| 161 | + |
| 162 | +// element and parent |
| 163 | +//3 divs, top , right , and left |
| 164 | +// select element and parent |
| 165 | +//under the fetch Json function |
| 166 | + |
| 167 | +console.log(DataTransferItemList) |
| 168 | +c//reate 4 |
| 169 | +array |
| 170 | +loop over data |
| 171 | + |
| 172 | +console.log(data) |
| 173 | +let repo Name = []; |
| 174 | +let repo desc = []; |
| 175 | +let repoUpdate = []; |
| 176 | +let repo repoFork = []; |
| 177 | +for(let i=0, i<data.length;i++) { |
| 178 | + repoName.push(data[data[i].name]); |
| 179 | + //same for repoDes |
| 180 | + // |
47 | 181 | }
|
| 182 | +hard coded as p or h: and youloop over the data and try to fill it7 * |
0 commit comments