forked from HackYourFuture/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (122 loc) · 5.43 KB
/
index.js
File metadata and controls
137 lines (122 loc) · 5.43 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
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
function main(){
console.log('main!');
const HyfReposHttps = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
fetchJSON(HyfReposHttps)
.then(data => xhrCallback(data))
.catch(err => renderError(err));
}
function fetchJSON(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();
});
}
function renderError(err) {
console.error(err.message);
}
// Callback that handles response from server
function xhrCallback(data){
console.log('calling xhrcallback');
addSelectElementOptions(data);
checkSelectChanging(data);
}
// Add options to select element
function addSelectElementOptions(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
function checkSelectChanging (arr) {
console.log('calling checkSelectChanging');
let selectElement = document.getElementById("repositories");
selectElement.addEventListener("change", function(){
const selectValue = selectElement.value;
const repo = arr.filter(repo => repo.id == selectValue)[0];
renderRepositoryInfo(repo);
const repoContributersUrl = repo.contributors_url;
fetchJSON(repoContributersUrl)
.then(data=> renderRepositoryContributers(data))
.catch(err => renderError(err));
});
}
function renderRepositoryInfo(selectedRepository){
// let repo = arr.filter(repo => repo.id == value)[0];
console.log('calling renderRepositoryInfo');
const repositoriesInfoElement = document.querySelector('#repo_info');
// while( repositoriesInfoElement.hasChildNodes()){
// repositoriesInfoElement.removeChild(repositoriesInfoElement.firstChild);
// }
repositoriesInfoElement.innerHTML =``;
repositoriesInfoElement.innerHTML =`<div class="repoContainer">
<strong>Repository: </strong><span><a href=${selectedRepository.html_url}>${selectedRepository.name}</a></span><br>
<strong>Description: </strong><span>${selectedRepository.description}</span><br>
<strong>Forks: </strong><span>${selectedRepository.forks}</span><br>
<strong>Updated: </strong><span>${selectedRepository.updated_at}</span><br>
</div>`;
// const repoContainer = document.createElement('div');
// repoContainer.setAttribute('class', 'repoContainer');
// const repoLink = document.createElement('a');
// repoLink.setAttribute('target', '_blank');
// repoLink.href = selectedRepository.html_url;
// repoLink.innerText = selectedRepository.name;
// const repoDescription = document.createElement('h3');
// repoDescription.innerText = "Description: " + selectedRepository.description;
// const repoForks = document.createElement('h3');
// repoForks.innerText = "Forks: " + selectedRepository.forks
// const repoUpdate = document.createElement('h3');
// repoUpdate.innerText = "Last Updated: " + selectedRepository.updated_at;
// repoContainer.appendChild(repoLink);
// repoContainer.appendChild(repoDescription);
// repoContainer.appendChild(repoForks);
// repoContainer.appendChild(repoUpdate);
// repositoriesInfoElement.appendChild(repoContainer);
}
function renderRepositoryContributers(response){
console.log('calling renderRepositoryContributers');
const repoContributers = document.querySelector('#repo_contributors');
repoContributers.innerHTML =``;
response.forEach(function(item){
repoContributers.innerHTML += `<div class="contributorContainer">
<div class="contributorList"><h3>${item.login}</h3></div>
<div class="contributorList"><img src=${item.avatar_url}></div>
<div class="contributorList"><h5>${item.contributions}</h4></div>
</div>`;
});
// const contributorsListElement = document.querySelector('#contributorList');
// const contributorsListElement = document.querySelector('#repo_contributors');
// while( contributorsListElement.hasChildNodes()){
// contributorsListElement.removeChild(contributorsListElement.firstChild);
// }
// contributers.forEach(contributor => {
// const contributorContainer = document.createElement('div');
// contributorContainer.setAttribute('class', 'contributorContainer');
// const listElement = document.createElement('h2');
// listElement.innerText = contributor.login;
// const imgElement = document.createElement('img');
// imgElement.src = contributor.avatar_url;
// const txtElement = document.createElement('h3');
// txtElement.innerText = contributor.contributions;
// contributorContainer.appendChild(listElement);
// contributorContainer.appendChild(imgElement);
// contributorContainer.appendChild(txtElement);
// contributorsListElement.appendChild(contributorContainer);
// });
}