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
81 lines (66 loc) · 2.15 KB
/
App.js
File metadata and controls
81 lines (66 loc) · 2.15 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
'use strict';
/* global Util, Repository, Contributor */
class App {
constructor(url) {
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');
// ...
try {
// ...
const repos = await Util.fetchJSON(url);
this.repos = repos.map(repo => new Repository(repo));
// ...
} catch (error) {
this.renderError(error);
}
}
/**
* Removes all child elements from a container element
* @param {*} container Container element to clear
*/
clearContainer(container) {
while (container.firstChild) {
container.removeChild(container.firstChild);
}
}
/**
* Fetch contributor information for the selected repository and render the
* repo and its contributors as HTML elements in the DOM.
* @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('container');
this.clearContainer(container);
const leftDiv = Util.createAndAppend('div', container);
const rightDiv = Util.createAndAppend('div', container);
const contributorList = Util.createAndAppend('ul', 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 DOM.
* @param {Error} error An Error object describing the error.
*/
renderError(error) {
// Replace this comment with your code
}
}
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
window.onload = () => new App(HYF_REPOS_URL);