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
78 lines (65 loc) · 2.3 KB
/
Copy pathApp.js
File metadata and controls
78 lines (65 loc) · 2.3 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
'use strict';
/* global Util, Repository, Contributor */
class App {
constructor(url) {
this.mainContainer = null;
this.initialize(url);
}
/**
* Initialization
* @param {string} url The GitHub URL for obtaining the organization's repositories.
*/
async initialize(url) {
// Add code here to initialize your app
// 1. Create the fixed HTML elements of your page
// 2. Make an initial XMLHttpRequest using Util.fetchJSON() to populate your <select> element
const root = document.getElementById('root');
const header = Util.createAndAppend('header', root, { class: 'header' });
this.mainContainer = Util.createAndAppend('div', root, { id: 'container' });
try {
const repos = await Util.fetchJSON(url);
this.repos = repos.map(repo => new Repository(repo));
// TODO: add your own code here
} catch (error) {
this.renderError(error);
}
}
/**
* Removes all child elements from a container element
* @param {*} container Container element to clear
*/
clearContainer() {
while (this.mainContainer.firstChild) {
this.mainContainer.removeChild(this.mainContainer.firstChild);
}
}
/**
* Fetch contributor information for the selected repository and render the
* repo and its contributors as HTML elements in the DOM.
* @param {object} repo The selected repository object
*/
async selectRepository(repo) {
try {
this.clearContainer();
const contributors = await repo.fetchContributors();
const repoContainer = Util.createAndAppend('div', this.mainContainer);
const contributorContainer = Util.createAndAppend('div', this.mainContainer);
const contributorList = Util.createAndAppend('ul', contributorContainer);
repo.render(repoContainer);
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); // TODO: replace with your own code
}
}
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
window.onload = () => new App(HYF_REPOS_URL);