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
143 lines (131 loc) · 4.74 KB
/
index.js
File metadata and controls
143 lines (131 loc) · 4.74 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
138
139
140
141
142
143
'use strict';
{
function fetchJSON(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.onload = () => {
if (xhr.status < 400) {
resolve(xhr.response);
} else {
reject(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`));
}
};
xhr.send();
});
}
function createAndAppend(name, parent, options = {}) {
const elem = document.createElement(name);
parent.appendChild(elem);
Object.keys(options).forEach(key => {
const value = options[key];
if (key === 'text') {
elem.textContent = value;
} else {
elem.setAttribute(key, value);
}
});
return elem;
}
// Header
function selectOptions(nameOptions) {
const repoSelect = document.getElementById('repoSelect');
for (let i = 0; i < nameOptions.length; i++) {
createAndAppend('option', repoSelect, { value: i, text: nameOptions[i].name });
}
}
// Create table on left within main div
function displayInfo(element) {
const container = document.getElementById('container');
const divInfo = createAndAppend('div', container, {
id: 'leftSide',
class: 'left-div whiteframe',
});
// Table info
createAndAppend('table', divInfo, { id: 'table' });
const table = document.getElementById('table');
createAndAppend('tbody', table, { id: 'tbody' });
function createTableRow(label, description) {
const tableR = createAndAppend('tr', table);
createAndAppend('td', tableR, { text: label, class: 'label' });
createAndAppend('td', tableR, { text: description });
}
createTableRow('Repository: ', element.name);
createTableRow('Description: ', element.description);
createTableRow('Forks : ', element.forks);
const newDate = new Date(element.updated_at).toLocaleString();
createTableRow('Updated: ', newDate);
}
// Contributors
function contributorsList(element) {
fetchJSON(element.contributors_url).then(data => {
const container = document.getElementById('container');
createAndAppend('div', container, {
id: 'rightSide',
class: 'right-div whiteframe',
});
const rightSide = document.getElementById('rightSide');
createAndAppend('h7', rightSide, {
text: 'Contributions',
class: 'contributor-header',
});
createAndAppend('ul', rightSide, {
id: 'list',
class: 'contributor-list',
});
const list = document.getElementById('list');
for (let i = 0; i < data.length; i++) {
const contributorURL = createAndAppend('a', list, {
href: data[i].html_url,
target: '_blank',
});
const contributorItem = createAndAppend('li', contributorURL, {
class: 'contributor-item',
});
createAndAppend('img', contributorItem, {
src: data[i].avatar_url,
class: 'contributor-avatar',
});
const contributorData = createAndAppend('div', contributorItem, {
class: 'contributor-data',
});
createAndAppend('div', contributorData, { text: data[i].login });
createAndAppend('div', contributorData, {
text: data[i].contributions,
class: 'contributor-badge',
});
}
});
}
// Main Function
function main(url) {
const root = document.getElementById('root');
fetchJSON(url)
.catch(reject => {
createAndAppend('div', root, { text: reject.message, class: 'alert-error' });
})
.then(data => {
createAndAppend('header', root, { id: 'top', class: 'header' });
const top = document.getElementById('top');
createAndAppend('h7', top, { id: 'title', text: 'HYF Repositories' });
createAndAppend('select', top, { id: 'repoSelect', class: 'repo-selector' });
createAndAppend('div', root, { id: 'container' });
data.sort((a, b) => a.name.localeCompare(b.name));
selectOptions(data);
displayInfo(data[0]);
contributorsList(data[0]);
document.getElementById('repoSelect').onchange = function startListener() {
const selectedItem = this.options[this.selectedIndex].value;
const leftSideInfo = document.getElementById('leftSide');
leftSideInfo.parentNode.removeChild(leftSideInfo);
const contributors = document.getElementById('rightSide');
contributors.parentNode.removeChild(contributors);
displayInfo(data[selectedItem]);
contributorsList(data[selectedItem]);
};
});
}
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
window.onload = () => main(HYF_REPOS_URL);
}