Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3354941

Browse files
committed
upload changes
1 parent a7647b3 commit 3354941

File tree

4 files changed

+151
-4
lines changed

4 files changed

+151
-4
lines changed

homework/src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
cb(null, xhr.response);
1111
} else {
1212
cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`));
13-
console.log("Hello error");
1413
}
1514
};
1615
xhr.onerror = () => cb(new Error('Network request failed'));
@@ -32,13 +31,15 @@
3231
}
3332

3433
function main(url) {
34+
const root = document.getElementById('root');
3535
while (root.firstChild) {
3636
root.removeChild(root.firstChild);
3737
}
3838

3939
fetchJSON( HYF_REPOS_URL, (err, data) => {
4040
if (err) {
4141
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'})
4243
}
4344
let newArray = [];
4445
let forkArray = [];
@@ -61,7 +62,7 @@
6162
}
6263

6364

64-
const root = document.getElementById('root');
65+
6566
createAndAppend('h1', root, { text: "Hack Your Future Repositories", class: 'title' });
6667
createAndAppend('h3', root, { text: "Select a repository: ", class: 'subtitle'});
6768
const selectList = createAndAppend('select', root, {id: "mySelect" });

homework/src/index2Promises.js renamed to homework/src/index2Fetch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ function main(url) {
1818
fetch(HYF_REPOS_URL)
1919
.then(response => response.json())
2020
.then(json => createStuff(json))
21-
.catch(err => createAndAppend('div', root, { text: err.message, class: 'alert-error' }))
21+
.catch(err => createAndAppend('div', root, { text: err.message, class: 'alert-error' }),
22+
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'}));
2223

2324
} //end main
2425

homework/src/index3Promise.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
'use strict';
2+
{
3+
function fetchJSON(url) {
4+
return new Promise(function(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.onerror = () => reject(new Error('Network request failed'));
16+
xhr.send();
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;
27+
} else {
28+
elem.setAttribute(key, value);
29+
}
30+
});
31+
return elem;
32+
}
33+
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
40+
41+
function createStuff(data){
42+
fetchJSON('https://api.github.com/repos/HackYourFuture/alumni/contributors')
43+
.then(data => createStuff2(data))
44+
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+
136+
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
137+
window.onload = () => main(HYF_REPOS_URL);
138+
139+
}

homework/src/style.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.alert-error {
2-
color: red;
2+
color: white;
33
background-color:black;
44
font-size: 4rem;
55
}
@@ -120,6 +120,12 @@ img{
120120
display: flex;
121121
justify-content: center;
122122
}
123+
#catImage{
124+
display: block;
125+
margin-left: auto;
126+
margin-right: auto;
127+
width: 50%;
128+
}
123129

124130
#link {
125131
font-size: 20px;

0 commit comments

Comments
 (0)