forked from HackYourFuture/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.js
More file actions
78 lines (66 loc) · 2.06 KB
/
Copy pathUtil.js
File metadata and controls
78 lines (66 loc) · 2.06 KB
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
//contains general functions that App use
class Util{
}
Util.fetchJSON = function(url){
console.log('calling fetch json');
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status < 400) {
resolve(xhr.response);
} else {
reject(new Error(xhr.statusText));
}
}
};
xhr.send();
});
}
Util.greeting = function(){
console.log('Hello');
};
// Add options to select element
Util.addSelectElementOptions = function (arr){
console.log('calling addSelectElementOptions');
let selectElement = document.getElementById("repositories");
arr.forEach(rep => {
let option = document.createElement('option');
option.text = rep.name;
option.value = rep.id;
selectElement.appendChild(option);
});
}
//Function that works if select element change
Util.checkSelectChanging = function(arr) {
try {
console.log('calling checkSelectChanging');
const selectElement = document.getElementById("repositories");
selectElement.addEventListener("change", async function(){
const selectValue = selectElement.value;
const repo = arr.filter(repo => repo.id == selectValue)[0];
Util.renderRepositoryInfo(repo);
const repoContributersUrl = repo.contributors_url;
const contributersList = await Util.fetchJSON(repoContributersUrl);
Util.renderRepositoryContributers(contributersList);
});
}catch(err) {
console.log(err);
}
}
Util.renderRepositoryInfo = function(arr){
console.log('calling renderRepositoryInfo');
const repository = new Repository(arr);
repository.render();
}
Util.renderRepositoryContributers = function(response){
console.log('calling renderRepositoryContributers');
const repoContributers = document.querySelector('#repo_contributors');
repoContributers.innerHTML =``;
response.forEach(function(item){
const contributor = new Contributor(item);
contributor.render();
});
}