-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week 6 Home Work.js
229 lines (96 loc) · 4.54 KB
/
Week 6 Home Work.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
(function(){
var GitHubUrl = 'https://api.github.com';
// This function performs an http request to a server
// Return a promise https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
function Ajax ( method, url ){
// For promise take a first argument a function
// and the first two parameters are two function
// The first is resolve that can be call when the async operation goes well
// and the second is reject that can be call when something goes wrong
return new Promise(function(resolve, reject){
var req = new XMLHttpRequest();
req.addEventListener('load', function(){
// Our request is loaded
try{
resolve( JSON.parse(this.responseText) )
}catch(e){
reject( new Error(e) )
}
});
req.open(method, url);
// This is needed to autheticate your requests
// For don't get limits on api
req.setRequestHeader(
'Authorization',
'token 9d232e8e02c4b19142be0d89a0480a8109fd257b'
);
req.send();
})
}
// We declare a function that fetch the repositories of one organization
// And returns the Promise the above function returns
function fetchReposFromOrganization( organizationName ){
return Ajax( 'GET', [GitHubUrl, 'orgs', organizationName, 'repos'].join('/'));
}
var fetchedRepos = [];
// Here we create a list
function renderListOfRepositories ( res ){
var html = [];
var promisesForContributors = []
// Here we loop trough the repos and we create some html
for(var k=0; k<res.length; k++){
var repo = res[k];
var repoName = repo.full_name;
var description = repo.description;
var link = repo.html_url;
// Since this information is handy later on to get the container
// Where to render the list of contributors we create a reference in another variable.
fetchedRepos.push(repo.id);
// This is just string concatenation with some html
html.push([
'<li id="repo-', repo.id, '" >',
'<a href="', link, '" />',
'<h3>', repoName, '</h3>',
'<p>', description, '</p>',
'</a>',
'<div class="contributors" ></div>',
'</li>'
].join(''));
// We create here a new promise for fetching the contributors of the repo and we push
// to an Array of only promises
promisesForContributors.push(fetchContributors(repo.owner.login, repo.name))
}
// Once the loop is done we paste in the html the result of our template
document.getElementById('app').innerHTML = html.join('')
// And we ask to resolve the Array of promises trough Promise.all
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
return Promise.all(promisesForContributors);
}
// This get used above for fetch the Contributors (line 73)
function fetchContributors( owner, repo ){
return Ajax( 'GET', [GitHubUrl, 'repos', owner, repo, 'contributors'].join('/'));
}
// Alike the renderListOfRepositories this renders the list of Contributors
function renderContributors( repoContributors ){
// The first array is the repo posiotion (equivalent to the promise relsolved order)
for(var i=0; i<repoContributors.length; i++){
var contributors = repoContributors[i];
// We can have more then one contributor for repository so we iterate on them to template them
var htmlContributors = ['<ul>'];
for(var k=0; k<contributors.length; k++){
var contributor = contributors[k];
htmlContributors.push(`<li><img src="${contributor.avatar_url}" /><b>${contributor.login}</b></li>`)
}
htmlContributors.push('</ul>');
// We find the reference (#repo-${id}) for the DOM element and we paste the template of contributors
document.querySelector(`#repo-${fetchedRepos[i]} .contributors`).innerHTML = htmlContributors.join('');
}
}
// Perform an Ajax function and return the result to the next then()
fetchReposFromOrganization('hackyourfuture')
// This get the repos list
.then(renderListOfRepositories)
// Render the list of contributors
.then(renderContributors);
// PROFIT \o/
})();