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

Skip to content

Commit c94839a

Browse files
authored
Create HomeWork.js
1 parent fcdfc82 commit c94839a

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

Week6/HomeWork.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
(function(){
2+
var GitHubUrl = 'https://api.github.com';
3+
4+
// This function performs an http request to a server
5+
// Return a promise https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
6+
function Ajax ( method, url ){
7+
8+
// For promise take a first argument a function
9+
// and the first two parameters are two function
10+
// The first is resolve that can be call when the async operation goes well
11+
// and the second is reject that can be call when something goes wrong
12+
return new Promise(function(resolve, reject){
13+
var req = new XMLHttpRequest();
14+
req.addEventListener('load', function(){
15+
// Our request is loaded
16+
try{
17+
resolve( JSON.parse(this.responseText) )
18+
}catch(e){
19+
reject( new Error(e) )
20+
}
21+
});
22+
req.open(method, url);
23+
24+
// This is needed to autheticate your requests
25+
// For don't get limits on api
26+
req.setRequestHeader(
27+
'Authorization',
28+
'token 9d232e8e02c4b19142be0d89a0480a8109fd257b'
29+
);
30+
31+
req.send();
32+
})
33+
}
34+
35+
// We declare a function that fetch the repositories of one organization
36+
// And returns the Promise the above function returns
37+
function fetchReposFromOrganization( organizationName ){
38+
return Ajax( 'GET', [GitHubUrl, 'orgs', organizationName, 'repos'].join('/'));
39+
}
40+
41+
var fetchedRepos = [];
42+
43+
// Here we create a list
44+
function renderListOfRepositories ( res ){
45+
var html = [];
46+
var promisesForContributors = []
47+
48+
// Here we loop trough the repos and we create some html
49+
for(var k=0; k<res.length; k++){
50+
var repo = res[k];
51+
var repoName = repo.full_name;
52+
var description = repo.description;
53+
var link = repo.html_url;
54+
55+
// Since this information is handy later on to get the container
56+
// Where to render the list of contributors we create a reference in another variable.
57+
fetchedRepos.push(repo.id);
58+
59+
// This is just string concatenation with some html
60+
html.push([
61+
'<li id="repo-', repo.id, '" >',
62+
'<a href="', link, '" />',
63+
'<h3>', repoName, '</h3>',
64+
'<p>', description, '</p>',
65+
'</a>',
66+
'<div class="contributors" ></div>',
67+
'</li>'
68+
].join(''));
69+
70+
// We create here a new promise for fetching the contributors of the repo and we push
71+
// to an Array of only promises
72+
promisesForContributors.push(fetchContributors(repo.owner.login, repo.name))
73+
}
74+
// Once the loop is done we paste in the html the result of our template
75+
document.getElementById('app').innerHTML = html.join('')
76+
77+
// And we ask to resolve the Array of promises trough Promise.all
78+
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
79+
return Promise.all(promisesForContributors);
80+
}
81+
82+
// This get used above for fetch the Contributors (line 73)
83+
function fetchContributors( owner, repo ){
84+
return Ajax( 'GET', [GitHubUrl, 'repos', owner, repo, 'contributors'].join('/'));
85+
}
86+
87+
// Alike the renderListOfRepositories this renders the list of Contributors
88+
function renderContributors( repoContributors ){
89+
// The first array is the repo posiotion (equivalent to the promise relsolved order)
90+
for(var i=0; i<repoContributors.length; i++){
91+
var contributors = repoContributors[i];
92+
93+
// We can have more then one contributor for repository so we iterate on them to template them
94+
var htmlContributors = ['<ul>'];
95+
for(var k=0; k<contributors.length; k++){
96+
var contributor = contributors[k];
97+
htmlContributors.push(`<li><img src="${contributor.avatar_url}" /><b>${contributor.login}</b></li>`)
98+
}
99+
htmlContributors.push('</ul>');
100+
// We find the reference (#repo-${id}) for the DOM element and we paste the template of contributors
101+
document.querySelector(`#repo-${fetchedRepos[i]} .contributors`).innerHTML = htmlContributors.join('');
102+
}
103+
104+
}
105+
106+
// Perform an Ajax function and return the result to the next then()
107+
fetchReposFromOrganization('hackyourfuture')
108+
// This get the repos list
109+
.then(renderListOfRepositories)
110+
// Render the list of contributors
111+
.then(renderContributors);
112+
113+
// PROFIT \o/
114+
115+
})();

0 commit comments

Comments
 (0)