Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5448828

Browse files
committed
Split up View into multiple classes
1 parent 0f1308a commit 5448828

File tree

9 files changed

+168
-104
lines changed

9 files changed

+168
-104
lines changed

homework-classes/App.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@
1616
},
1717
};
1818

19-
const { Model, View } = window;
19+
const { Model, HeaderView, RepoView, ContributorsView, ErrorView } = window;
20+
const { createAndAppend } = window.Util;
2021

2122
class App {
2223
constructor(account) {
24+
const root = document.getElementById('root');
25+
const header = createAndAppend('header', root, { class: 'header' });
26+
this.mainContainer = createAndAppend('main', root, {
27+
id: 'main-container',
28+
});
29+
2330
const model = new Model(account);
24-
this.view = new View(model, account);
31+
model.subscribe(this);
32+
model.subscribe(new HeaderView(model, account, header));
33+
model.subscribe(new RepoView(this.mainContainer));
34+
model.subscribe(new ContributorsView(this.mainContainer));
35+
model.subscribe(new ErrorView(this.mainContainer));
36+
model.fetchData();
37+
}
38+
39+
update() {
40+
this.mainContainer.innerHTML = '';
2541
}
2642
}
2743

homework-classes/ContributorsView.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
{
4+
const { createAndAppend } = window.Util;
5+
6+
class ContributorsView {
7+
constructor(mainContainer) {
8+
this.mainContainer = mainContainer;
9+
}
10+
11+
update(state) {
12+
if (!state.error) {
13+
this.render(state.contributors);
14+
}
15+
}
16+
17+
/**
18+
* Renders the list of contributors
19+
* @param {Object[]} contributors An array of contributor objects
20+
*/
21+
render(contributors) {
22+
// TODO: replace this comment and the console.log with your own code
23+
console.log('renderContributors', contributors);
24+
}
25+
}
26+
27+
window.ContributorsView = ContributorsView;
28+
}

homework-classes/ErrorView.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
{
4+
const { createAndAppend } = window.Util;
5+
6+
class ErrorView {
7+
constructor(mainContainer) {
8+
this.mainContainer = mainContainer;
9+
}
10+
11+
update(state) {
12+
if (state.error) {
13+
this.render(state.error);
14+
}
15+
}
16+
17+
/**
18+
* Renders an error for the 'error' message type.
19+
* @param {Error} error An Error object
20+
*/
21+
render(error) {
22+
// TODO: replace this comment and the console.log with your own code
23+
console.log('renderError', error);
24+
}
25+
}
26+
27+
window.ErrorView = ErrorView;
28+
}

homework-classes/HeaderView.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
{
4+
const { createAndAppend } = window.Util;
5+
6+
class HeaderView {
7+
constructor(model, account, header) {
8+
this.model = model;
9+
this.account = account;
10+
this.header = header;
11+
this.select = null;
12+
}
13+
14+
update(state) {
15+
if (!this.select && !this.error) {
16+
this.render(state.repos);
17+
}
18+
}
19+
20+
/**
21+
* Renders the data for the 'select' message type. Create a <select> element
22+
* and its <option> children.
23+
* @param {Object[]} repos An array of repository objects.
24+
*/
25+
render(repos) {
26+
this.select = createAndAppend('select', this.header);
27+
// TODO: replace this comment and the console.log with your own code
28+
console.log('renderSelect', repos);
29+
}
30+
}
31+
32+
window.HeaderView = HeaderView;
33+
}

homework-classes/RepoView.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
{
4+
const { createAndAppend } = window.Util;
5+
6+
class RepoView {
7+
constructor(mainContainer) {
8+
this.mainContainer = mainContainer;
9+
}
10+
11+
update(state) {
12+
if (!state.error) {
13+
this.render(state.selectedRepo);
14+
}
15+
}
16+
17+
/**
18+
* Renders the repository details.
19+
* @param {Object} repo A repository object.
20+
*/
21+
render(repo) {
22+
// TODO: replace this comment and the console.log with your own code
23+
console.log('renderRepoDetails', repo);
24+
}
25+
}
26+
27+
window.RepoView = RepoView;
28+
}

homework-classes/Util.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
{
4+
class Util {
5+
/**
6+
* Creates an element, optionally setting its attributes, and appends
7+
* the element to a parent.
8+
* @param {string} name The tag name of the element to create.
9+
* @param {HTMLElement} parent The parent element.
10+
* @param {Object} options An object with attribute names and values.
11+
*/
12+
static createAndAppend(name, parent, options = {}) {
13+
const elem = document.createElement(name);
14+
parent.appendChild(elem);
15+
Object.entries(options).forEach(([key, value]) => {
16+
if (key === 'text') {
17+
elem.textContent = value;
18+
} else {
19+
elem.setAttribute(key, value);
20+
}
21+
});
22+
return elem;
23+
}
24+
}
25+
26+
window.Util = Util;
27+
}

homework-classes/View.js

Lines changed: 0 additions & 101 deletions
This file was deleted.

homework-classes/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
</head>
2222
<body>
2323
<div id="root"></div>
24+
<script src="./Util.js"></script>
2425
<script src="./Observable.js"></script>
2526
<script src="./Model.js"></script>
26-
<script src="./View.js"></script>
27+
<script src="./HeaderView.js"></script>
28+
<script src="./RepoView.js"></script>
29+
<script src="./ContributorsView.js"></script>
30+
<script src="./ErrorView.js"></script>
2731
<script src="./App.js"></script>
2832
</body>
2933
</html>

homework-classes/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ body {
3131
display: flex;
3232
flex-direction: row;
3333
align-items: center;
34+
height: 56px;
3435
}
3536

3637
.repo-select {

0 commit comments

Comments
 (0)