Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 758606c

Browse filesBrowse files
committed
upload week2 homework Promises
1 parent 3354941 commit 758606c
Copy full SHA for 758606c

File tree

Expand file treeCollapse file tree

1 file changed

+123
-119
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+123
-119
lines changed
Open diff view settings
Collapse file

‎homework/src/index.js‎

Copy file name to clipboard
+123-119Lines changed: 123 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,139 @@
11
'use strict';
2-
32
{
4-
function fetchJSON(url, cb) {
5-
const xhr = new XMLHttpRequest();
3+
function fetchJSON(url) {
4+
return new Promise(function(resolve, reject){
5+
const xhr = new XMLHttpRequest();
66
xhr.open('GET', url);
77
xhr.responseType = 'json';
88
xhr.onload = () => {
99
if (xhr.status < 400) {
10-
cb(null, xhr.response);
10+
resolve(xhr.response);
1111
} else {
12-
cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`));
12+
reject(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`));
1313
}
14-
};
15-
xhr.onerror = () => cb(new Error('Network request failed'));
14+
}
15+
xhr.onerror = () => reject(new Error('Network request failed'));
1616
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.innerText = value;
17+
}); // end promise
18+
} //end fetchJSON
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.innerText = value;
2627
} else {
27-
elem.setAttribute(key, value);
28+
elem.setAttribute(key, value);
2829
}
29-
});
30-
return elem;
31-
}
32-
33-
function main(url) {
34-
const root = document.getElementById('root');
35-
while (root.firstChild) {
36-
root.removeChild(root.firstChild);
37-
}
38-
39-
fetchJSON( HYF_REPOS_URL, (err, data) => {
40-
if (err) {
41-
createAndAppend('div', root, { text: err.message, class: 'alert-error' });
42-
createAndAppend('img', root, {id: 'catImage', src: 'https://us.123rf.com/450wm/photodeti/photodeti1702/photodeti170200132/72587923-cat-holding-stop-sign-isolated-on-white-background-.jpg?ver=6'})
43-
}
44-
let newArray = [];
45-
let forkArray = [];
46-
let languageArray = [];
47-
let descriptionArray = [];
48-
let updatedAt = [];
49-
let htmlArray = [];
50-
data.sort((a, b) => (a.name).localeCompare(b.name));
51-
30+
});
31+
return elem;
32+
}
5233

53-
for (let i = 0; i < data.length; i++){
54-
newArray.push(data[i].name);
55-
descriptionArray.push(data[i].description);
56-
forkArray.push(data[i].forks);
57-
languageArray.push(data[i].language);
58-
updatedAt.push(data[i].updated_at);
59-
htmlArray.push(data[i].html_url);
60-
var date = new Date ((data[i].updated_at));
61-
date = date.toUTCString();
62-
}
63-
64-
65-
66-
createAndAppend('h1', root, { text: "Hack Your Future Repositories", class: 'title' });
67-
createAndAppend('h3', root, { text: "Select a repository: ", class: 'subtitle'});
68-
const selectList = createAndAppend('select', root, {id: "mySelect" });
69-
const headerDiv = createAndAppend('div', root, {class: 'headerdiv'});
70-
createAndAppend('h3', headerDiv, { text: "Repository Information", class: 'subtitle', id: 'repoHeader' });
71-
createAndAppend('h3', headerDiv, { text: "Contributors", class: 'subtitle', id:'contributorHeader' });
72-
const container = createAndAppend('div', root, {class: 'container'});
73-
const card = createAndAppend('div', container, { class: 'card'});
74-
const ul = createAndAppend('ul', card, {id: "myUl", });
75-
const contributorsCard = createAndAppend('div', container, {class: 'card'});
76-
const contributorsUl = createAndAppend('ul', contributorsCard, {id: 'contributorsUl'});
77-
const Index0Name = createAndAppend ('li', ul, {text: "Repository: ", class: 'nameInContainer'});
78-
const Index0Link = createAndAppend ('a', Index0Name, {text: newArray[0], target: "_blank", href: htmlArray[0]});
79-
const Index0Description = createAndAppend('li', ul, {text: "Description: " + descriptionArray[0], class:"descriptionInContainer"});
80-
const Index0Fork = createAndAppend ('li', ul, {text: "Number of Forks: " + forkArray[0], class: 'forksInContainer'});
81-
const Index0Language = createAndAppend ('li', ul, {text: "Language: " + languageArray[0], class: 'updatedAtInContainer'});
82-
const Index0UpdatedAt = createAndAppend ('li', ul, {text: "Updated at: " + date, class: 'updatedAtInContainer'})
83-
fetchJSON('https://api.github.com/repos/HackYourFuture/' + newArray[0] + '/contributors', (err, data) => {
84-
for (let i = 0; i < data.length; i++){
85-
let Image0Link = createAndAppend('li', contributorsUl, {})
86-
let contributor0Name = createAndAppend('img', Image0Link, {src: data[i].avatar_url, class: 'imageSrc'});
87-
let contributor0Link = createAndAppend('a', Image0Link, {text: data[i].login, target: "_blank", href: data[i].html_url, id: 'link'});
88-
let contributor0Badge = createAndAppend('li', Image0Link, {text:"Contributions: " + data[i].contributions, class: 'badge'});
89-
} //end for
90-
}); //end fetchJSON
91-
92-
93-
data.forEach((repo) => {
94-
for (let i = 0; i < newArray.length; i++) {
95-
createAndAppend('option', selectList, {id: "myOption", value: i, text: newArray[i]});
96-
}
97-
});
98-
99-
function removeNodes(container){
100-
while (ul.hasChildNodes()) {
101-
ul.removeChild(ul.firstChild);
102-
}
103-
while (contributorsUl.hasChildNodes()) {
104-
contributorsUl.removeChild(contributorsUl.firstChild);
105-
}
106-
} //end removeNodes
107-
108-
selectList.onchange = function(selectedIndex){
109-
fetchJSON('https://api.github.com/repos/HackYourFuture/' + newArray[this.selectedIndex] + '/contributors', (err, data) => {
110-
for (let i = 0; i < data.length; i++){
111-
let ImageLink = createAndAppend('li', contributorsUl, {})
112-
let contributorName = createAndAppend('img', ImageLink, {src: data[i].avatar_url, class: 'imageSrc'});
113-
let contributorLink = createAndAppend('a', ImageLink, {text: data[i].login, target: "_blank", href: data[i].html_url, id: 'link'});
114-
let contributorBadge = createAndAppend('li', ImageLink, {text:"Contributions: " + data[i].contributions, class: 'badge'});
115-
} //end for
116-
}); //end fetchJSON
117-
let repoName = createAndAppend('li', ul, { text: "Repository: ", class: 'nameInContainer', function: removeNodes()});
118-
createAndAppend('a', repoName, { text: newArray[this.selectedIndex], id: 'linkInContainer', target: "_blank", href: htmlArray[this.selectedIndex]});
119-
createAndAppend('li', ul, {text: "Description: " + descriptionArray[this.selectedIndex], class: 'descriptionInContainer'});
120-
createAndAppend('li', ul, { text: "Number of Forks: " + forkArray[this.selectedIndex], class: 'forksInContainer'});
121-
createAndAppend('li', ul, { text: "Language: " + languageArray[this.selectedIndex], class: 'languageInContainer'});
122-
let dates = new Date (updatedAt[this.selectedIndex]);
123-
dates = dates.toUTCString();
124-
createAndAppend('li', ul, {text: "Updated at: " + dates, class: 'updatedAtInContainer'});
34+
function main(url) {
35+
fetchJSON(HYF_REPOS_URL)
36+
.then(data => createStuff(data))
37+
.catch(err => createAndAppend('div', root, { text: err.message, class: 'alert-error' }),
38+
createAndAppend('img', root, {id: 'catImage', src: 'https://us.123rf.com/450wm/photodeti/photodeti1702/photodeti170200132/72587923-cat-holding-stop-sign-isolated-on-white-background-.jpg?ver=6'}));
39+
} //end main
12540

126-
}// end of onchange
127-
128-
}); //end of Fetch
129-
} //end main
130-
41+
function createStuff(data){
42+
fetchJSON('https://api.github.com/repos/HackYourFuture/alumni/contributors')
43+
.then(data => createStuff2(data))
13144

45+
let newArray = [];
46+
let forkArray = [];
47+
let languageArray = [];
48+
let descriptionArray = [];
49+
let updatedAt = [];
50+
let htmlArray = [];
51+
const root = document.getElementById('root');
52+
data.sort((a, b) => (a.name).localeCompare(b.name));
53+
54+
for (let i = 0; i < data.length; i++){
55+
newArray.push(data[i].name);
56+
descriptionArray.push(data[i].description);
57+
forkArray.push(data[i].forks);
58+
languageArray.push(data[i].language);
59+
updatedAt.push(data[i].updated_at);
60+
htmlArray.push(data[i].html_url);
61+
var date = new Date ((data[i].updated_at));
62+
date = date.toUTCString();
63+
}
64+
65+
while (root.firstChild) {
66+
root.removeChild(root.firstChild);
67+
}
68+
69+
createAndAppend('h1', root, { text: "Hack Your Future Repositories", class: 'title' });
70+
createAndAppend('h3', root, { text: "Select a repository: ", class: 'subtitle'});
71+
const selectList = createAndAppend('select', root, {id: "mySelect" });
72+
const headerDiv = createAndAppend('div', root, {class: 'headerdiv'});
73+
createAndAppend('h3', headerDiv, { text: "Repository Information", class: 'subtitle', id: 'repoHeader' });
74+
createAndAppend('h3', headerDiv, { text: "Contributors", class: 'subtitle', id:'contributorHeader' });
75+
const container = createAndAppend('div', root, {class: 'container'});
76+
const card = createAndAppend('div', container, { class: 'card'});
77+
const ul = createAndAppend('ul', card, {id: "myUl", });
78+
const contributorsCard = createAndAppend('div', container, {class: 'card'});
79+
const contributorsUl = createAndAppend('ul', contributorsCard, {id: 'contributorsUl'});
80+
const Index0Name = createAndAppend ('li', ul, {text: "Repository: ", class: 'nameInContainer'});
81+
const Index0Link = createAndAppend ('a', Index0Name, {text: newArray[0], target: "_blank", href: htmlArray[0]});
82+
const Index0Description = createAndAppend('li', ul, {text: "Description: " + descriptionArray[0], class:"descriptionInContainer"});
83+
const Index0Fork = createAndAppend ('li', ul, {text: "Number of Forks: " + forkArray[0], class: 'forksInContainer'});
84+
const Index0Language = createAndAppend ('li', ul, {text: "Language: " + languageArray[0], class: 'updatedAtInContainer'});
85+
const Index0UpdatedAt = createAndAppend ('li', ul, {text: "Updated at: " + date, class: 'updatedAtInContainer'})
86+
87+
function createStuff2(data){
88+
for (let i = 0; i < data.length; i++){
89+
let Image0Link = createAndAppend('li', contributorsUl, {})
90+
let contributor0Name = createAndAppend('img', Image0Link, {src: data[i].avatar_url, class: 'imageSrc'});
91+
let contributor0Link = createAndAppend('a', Image0Link, {text: data[i].login, target: "_blank", href: data[i].html_url, id: 'link'});
92+
let contributor0Badge = createAndAppend('li', Image0Link, {text:"Contributions: " + data[i].contributions, class: 'badge'});
93+
} //end for
94+
95+
data.forEach((repo) => {
96+
for (let i = 0; i < newArray.length; i++) {
97+
createAndAppend('option', selectList, {id: "myOption", value: i, text: newArray[i]});
98+
}
99+
});
100+
101+
function removeNodes(container){
102+
while (ul.hasChildNodes()) {
103+
ul.removeChild(ul.firstChild);
104+
}
105+
while (contributorsUl.hasChildNodes()) {
106+
contributorsUl.removeChild(contributorsUl.firstChild);
107+
}
108+
} //end removeNodes
109+
110+
selectList.onchange = function(selectedIndex){
111+
fetchJSON('https://api.github.com/repos/HackYourFuture/' + newArray[this.selectedIndex] + '/contributors')
112+
.then(data => createStuff3(data))
113+
114+
let repoName = createAndAppend('li', ul, { text: "Repository: ", class: 'nameInContainer', function: removeNodes()});
115+
createAndAppend('a', repoName, { text: newArray[this.selectedIndex], id: 'linkInContainer', target: "_blank", href: htmlArray[this.selectedIndex]});
116+
createAndAppend('li', ul, {text: "Description: " + descriptionArray[this.selectedIndex], class: 'descriptionInContainer'});
117+
createAndAppend('li', ul, { text: "Number of Forks: " + forkArray[this.selectedIndex], class: 'forksInContainer'});
118+
createAndAppend('li', ul, { text: "Language: " + languageArray[this.selectedIndex], class: 'languageInContainer'});
119+
let dates = new Date (updatedAt[this.selectedIndex]);
120+
dates = dates.toUTCString();
121+
createAndAppend('li', ul, {text: "Updated at: " + dates, class: 'updatedAtInContainer'});
122+
123+
}// end of onchange
124+
}// end createStuff2
125+
} //end createStuff
126+
127+
function createStuff3(data){
128+
for (let i = 0; i < data.length; i++){
129+
let ImageLink = createAndAppend('li', contributorsUl, {})
130+
let contributorName = createAndAppend('img', ImageLink, {src: data[i].avatar_url, class: 'imageSrc'});
131+
let contributorLink = createAndAppend('a', ImageLink, {text: data[i].login, target: "_blank", href: data[i].html_url, id: 'link'});
132+
let contributorBadge = createAndAppend('li', ImageLink, {text:"Contributions: " + data[i].contributions, class: 'badge'});
133+
} //end for
134+
}//end createStuff3
135+
132136
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
133137
window.onload = () => main(HYF_REPOS_URL);
134-
135-
}
138+
139+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.