Skip to content

Commit 5ff9dbc

Browse files
Week 03 homework is done
1 parent ebbe121 commit 5ff9dbc

6 files changed

Lines changed: 967 additions & 0 deletions

File tree

‎Week3/homework/part2/App.js‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"use strict";
2+
3+
/* global Util, Repository, Contributor */
4+
5+
class App {
6+
constructor(url) {
7+
this.initialize(url);
8+
}
9+
10+
/**
11+
* Initialization
12+
* @param {string} url The GitHub URL for obtaining the organization's repositories.
13+
*/
14+
async initialize(url) {
15+
// Add code here to initialize your app
16+
// 1. Create the fixed HTML elements of your page
17+
18+
//Design the page by using DOM manipulation by calling a function from Util :
19+
Util.createPage();
20+
21+
// 2. Make an initial XMLHttpRequest using Util.fetchJSON() to populate your <select> element
22+
23+
Util.fetchJSON(HYF_REPOS_URL);
24+
25+
// ...
26+
27+
try {
28+
// ...
29+
const repos = await Util.fetchJSON(url);
30+
Util.fillCombobox(repos);
31+
Util.comboChangeListener(repos);
32+
Util.dataRender(repos);
33+
// ...
34+
} catch (error) {
35+
this.renderError(error);
36+
}
37+
}
38+
39+
/**
40+
* Removes all child elements from a container element
41+
* @param {*} container Container element to clear
42+
*/
43+
clearContainer(container) {
44+
while (container.firstChild) {
45+
container.removeChild(container.firstChild);
46+
}
47+
}
48+
49+
/**
50+
* Fetch contributor information for the selected repository and render the
51+
* repo and its contributors as HTML elements in the DOM.
52+
* @param {number} index The array index of the repository.
53+
*/
54+
async fetchContributorsAndRender(index) {
55+
try {
56+
const repo = this.repos[index];
57+
const contributors = await repo.fetchContributors();
58+
59+
const container = document.getElementById("container");
60+
this.clearContainer(container);
61+
62+
const leftDiv = Util.createAndAppend("div", container);
63+
const rightDiv = Util.createAndAppend("div", container);
64+
65+
const contributorList = Util.createAndAppend("ul", rightDiv);
66+
67+
repo.render(leftDiv);
68+
69+
contributors
70+
.map(contributor => new Contributor(contributor))
71+
.forEach(contributor => contributor.render(contributorList));
72+
} catch (error) {
73+
this.renderError(error);
74+
}
75+
}
76+
77+
/**
78+
* Render an error to the DOM.
79+
* @param {Error} error An Error object describing the error.
80+
*/
81+
renderError(error) {
82+
alert(error);
83+
}
84+
}
85+
86+
const HYF_REPOS_URL =
87+
"https://api.github.com/orgs/HackYourFuture/repos?per_page=100";
88+
89+
window.onload = () => new App(HYF_REPOS_URL);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
/* global Util */
4+
5+
// eslint-disable-next-line no-unused-vars
6+
class Contributor {
7+
constructor(data) {
8+
this.data = data;
9+
}
10+
11+
/**
12+
* Render the contributor info to the DOM.
13+
* @param {HTMLElement} contributorList The parent element in which to render the contributor.
14+
*/
15+
render(container) {
16+
Util.getContributors(repos);
17+
}
18+
}

‎Week3/homework/part2/Repository.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
/* global Util */
4+
5+
// eslint-disable-next-line no-unused-vars
6+
class Repository {
7+
constructor(data) {
8+
this.data = data;
9+
}
10+
11+
/**
12+
* Render the repository info to the DOM.
13+
* @param {HTMLElement} parent The parent element in which to render the repository.
14+
*/
15+
render(container) {
16+
Util.fillCombobox(this.data);
17+
}
18+
19+
/**
20+
* Returns an array of contributors as a promise
21+
*/
22+
fetchContributors() {
23+
return Util.fetchJSON(this.data.contributors_url);
24+
}
25+
26+
/**
27+
* Returns the name of the repository
28+
*/
29+
name() {
30+
return this.data.name;
31+
}
32+
}

0 commit comments

Comments
 (0)