|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | { |
4 | | - function fetchJSON(url, cb) { |
5 | | - return new Promise(url, (resolve, reject) => { |
| 4 | + function fetchJSON(url) { |
| 5 | + return new Promise((resolve, reject) => { |
6 | 6 | const xhr = new XMLHttpRequest(); |
7 | 7 | xhr.open('GET', url); |
8 | 8 | xhr.responseType = 'json'; |
|
13 | 13 | reject(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); |
14 | 14 | } |
15 | 15 | }; |
16 | | - xhr.onerror = () => cb(new Error('Network request failed')); |
17 | 16 | xhr.send(); |
18 | 17 | }); |
19 | 18 | } |
|
90 | 89 | //const repolist = getElementByClassName('left-div'); |
91 | 90 | } |
92 | 91 | //right |
93 | | - |
94 | 92 | function createContributor(element) { |
95 | | - fetchJSON(element.contributors_url, (err, data) => { |
| 93 | + fetchJSON(element.contributors_url).then(data => { |
96 | 94 | const root = document.getElementById('container'); |
97 | | - if (err) { |
98 | | - createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
99 | | - } else { |
100 | | - let contributors = createAndAppend('div', root, { |
101 | | - id: 'contributors', |
102 | | - class: 'rightDiv whiteFrame', |
| 95 | + let contributors = createAndAppend('div', root, { |
| 96 | + id: 'contributors', |
| 97 | + class: 'rightDiv whiteFrame', |
| 98 | + }); |
| 99 | + createAndAppend('p', contributors, { class: 'contributorsHeader', text: 'Contributors' }); |
| 100 | + |
| 101 | + let ul = createAndAppend('ul', contributors, { class: 'contributorsList' }); |
| 102 | + for (let i = 0; i < data.length; i++) { |
| 103 | + let li = createAndAppend('li', ul, { class: 'contributorItem' }); |
| 104 | + let img = createAndAppend('img', li, { |
| 105 | + src: data[i].avatar_url, |
| 106 | + class: 'contributorsAvatar', |
| 107 | + height: 48, |
| 108 | + }); |
| 109 | + let login = createAndAppend('a', li, { |
| 110 | + text: data[i].login, |
| 111 | + href: data[i].html_url, |
| 112 | + target: '_blank', |
| 113 | + class: 'contributorName', |
| 114 | + }); |
| 115 | + let badge = createAndAppend('div', li, { |
| 116 | + text: data[i].contributions, |
| 117 | + class: 'contributorBadge', |
103 | 118 | }); |
104 | | - createAndAppend('p', contributors, { class: 'contributorsHeader', text: 'Contributors' }); |
105 | | - |
106 | | - let ul = createAndAppend('ul', contributors, { class: 'contributorsList' }); |
107 | | - for (let i = 0; i < data.length; i++) { |
108 | | - let li = createAndAppend('li', ul, { class: 'contributorItem' }); |
109 | | - let img = createAndAppend('img', li, { |
110 | | - src: data[i].avatar_url, |
111 | | - class: 'contributorsAvatar', |
112 | | - height: 48, |
113 | | - }); |
114 | | - let login = createAndAppend('a', li, { |
115 | | - text: data[i].login, |
116 | | - href: data[i].html_url, |
117 | | - target: '_blank', |
118 | | - class: 'contributorName', |
119 | | - }); |
120 | | - let badge = createAndAppend('div', li, { |
121 | | - text: data[i].contributions, |
122 | | - class: 'contributorBadge', |
123 | | - }); |
124 | | - } |
125 | 119 | } |
126 | 120 | }); |
127 | 121 | } |
128 | 122 |
|
129 | 123 | //main for run program |
130 | 124 | function main(url) { |
131 | | - return new Promise((resolve, reject) => { |
132 | | - const root = document.getElementById('root'); |
133 | | - fetchJSON( |
134 | | - url, |
135 | | - (err, data) |
136 | | - .then(res => res.json()) |
137 | | - .then(data => { |
138 | | - resolve(data); |
139 | | - |
140 | | - /* |
141 | | - data.sort(function (item1, item2) { |
142 | | - if (item1.name.toUpperCase() < item2.name.toUpperCase()) return -1; |
143 | | - if (item1.name > item2.name) return 1; |
144 | | - return 0; |
145 | | - });*/ |
146 | | - |
147 | | - data.sort((item1, item2) => item1.localCompare(item2)); |
148 | | - |
149 | | - //Show Header |
150 | | - selectInfo(data); |
151 | | - //Create and show Container left-right |
152 | | - createAndAppend('div', root, { id: 'container', class: 'container' }); |
153 | | - //left-side |
154 | | - createRepo(data[0]); |
155 | | - //right-side |
156 | | - createContributor(data[0]); |
157 | | - |
158 | | - //change value if select repo in drop down |
159 | | - |
160 | | - document.getElementById('RepoList').onchange = function() { |
161 | | - let selectedItem = this.options[this.selectedIndex].value; |
162 | | - let table = document.getElementById('RepositoryOverview'); |
163 | | - table.parentNode.removeChild(table); |
164 | | - let contributors = document.getElementById('contributors'); |
165 | | - contributors.parentNode.removeChild(contributors); |
166 | | - |
167 | | - createRepo(data[selectedItem]); |
168 | | - createContributor(data[selectedItem]); |
169 | | - }; |
170 | | - }) |
171 | | - .catch(err => reject(err)), |
172 | | - ); |
173 | | - }); |
| 125 | + const root = document.getElementById('root'); |
| 126 | + fetchJSON(url) |
| 127 | + .catch(reject => { |
| 128 | + createAndAppend('div', root, { text: reject.message, class: 'alert-error' }); |
| 129 | + }) |
| 130 | + .then(data => { |
| 131 | + data.sort(function(item1, item2) { |
| 132 | + if (item1.name.toUpperCase() < item2.name.toUpperCase()) return -1; |
| 133 | + if (item1.name > item2.name) return 1; |
| 134 | + return 0; |
| 135 | + }); |
| 136 | + //Show Header |
| 137 | + const root = document.getElementById('root'); |
| 138 | + selectInfo(data); |
| 139 | + //Create and show Container left-right |
| 140 | + createAndAppend('div', root, { id: 'container', class: 'container' }); |
| 141 | + createRepo(data[0]); |
| 142 | + createContributor(data[0]); |
| 143 | + //change value if select repo in drop down |
| 144 | + document.getElementById('RepoList').onchange = function() { |
| 145 | + let selectedItem = this.options[this.selectedIndex].value; |
| 146 | + let table = document.getElementById('RepositoryOverview'); |
| 147 | + table.parentNode.removeChild(table); |
| 148 | + let contributors = document.getElementById('contributors'); |
| 149 | + contributors.parentNode.removeChild(contributors); |
| 150 | + |
| 151 | + createRepo(data[selectedItem]); |
| 152 | + createContributor(data[selectedItem]); |
| 153 | + }; |
| 154 | + }); |
174 | 155 | } |
175 | 156 |
|
176 | 157 | const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
|
0 commit comments