forked from HackYourFuture/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
77 lines (70 loc) · 2.38 KB
/
App.js
File metadata and controls
77 lines (70 loc) · 2.38 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
'use strict';
class App {
constructor(url) {
this.initialize(url);
}
/**
* @param {string} url The GitHub URL for obtaining the organization's repositories.
*/
async initialize(url) {
const root = document.getElementById('root');
Util.createAndAppend('h1', root, { text: 'REPOSITORIES' });
Util.createAndAppend('select', root, { id: 'dropdown-select' });
Util.createAndAppend('div', root, { id: 'details' });
Util.createAndAppend('div', root, { id: 'contributors' });
try {
const repos = await Util.fetchJSON(url);
this.repos = repos.map(repo => new Repository(repo));
this.repos.forEach(repoDataObj => {
repoDataObj.render(document.getElementById('dropdown-select'));
});
} catch (error) {
this.renderError(error);
}
document.getElementById('dropdown-select').addEventListener('change', event => {
const selectedRepo = event.target.value;
const selectedData = this.repos.filter(
repoData => repoData.repository.name === selectedRepo,
)[0];
const index = this.repos.indexOf(selectedData);
this.fetchContributorsAndRender(index);
});
}
/**
* @param {*} container Container element to clear
*/
static clearContainer(container) {
while (container.firstChild) {
container.removeChild(container.firstChild);
}
}
/**
* @param {number} index The array index of the repository.
*/
async fetchContributorsAndRender(index) {
try {
const repo = this.repos[index];
const contributors = await repo.fetchContributors();
const container = document.getElementById('contributors');
App.clearContainer(container);
const leftDiv = Util.createAndAppend('div', container);
const rightDiv = Util.createAndAppend('div', container);
const contributorList = Util.createAndAppend('div', rightDiv);
repo.render(leftDiv);
contributors
.map(contributor => new Contributor(contributor))
.forEach(contributor => contributor.render(contributorList));
} catch (error) {
this.renderError(error);
}
}
/**
* Render an error to the page.
* @param {Error} error An Error object describing the error.
*/
renderError(error) {
console.error(error);
}
}
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
window.onload = () => new App(HYF_REPOS_URL);