Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adding some homework files
  • Loading branch information
chichiglacierz committed Aug 25, 2019
commit b31ceb0e004b0eb1ccd74e8b647f5f802bc132e0
32 changes: 31 additions & 1 deletion homework/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,37 @@
</head>

<body>
<div id="root"></div>
<header>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wrapping the entire app in <header>, it seems only parts of the app should be in header?

<div id="root">
<!-- <p>
HYF Repositories
</p> -->
</div>
</header>

<!-- <div id="info-container">
<table>
<tbody>
<tr>
<th>Repository:</th>
<td>databases</td>
</tr>
<tr>
<th>Description:</th>
<td>Course structure for relational databases using MySQL as an example</td>
</tr>
<tr>
<th>Forks:</th>
<td>184</td>
</tr>

<th>Updated:</th>
<td>place for date</td>
</tbody>
</table>
</div>
<div id="contributor-div"></div>-->

<script src="./index.js"></script>
</body>
</html>
134 changes: 127 additions & 7 deletions homework/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';

{
function fetchJSON(url, cb) {
const xhr = new XMLHttpRequest();
Expand All @@ -15,7 +14,6 @@
xhr.onerror = () => cb(new Error('Network request failed'));
xhr.send();
}

function createAndAppend(name, parent, options = {}) {
const elem = document.createElement(name);
parent.appendChild(elem);
Expand All @@ -29,19 +27,141 @@
});
return elem;
}

function main(url) {
fetchJSON(url, (err, data) => {
const root = document.getElementById('root');
if (err) {
createAndAppend('div', root, { text: err.message, class: 'alert-error' });
} else {
createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) });
//createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) });
const select = createAndAppend('select', root);
createAndAppend('option', select, { text: 'Choose your favorite repo' });
data.forEach(repo => {
const name = repo.name;
createAndAppend('option', select, { text: name });
});
const repoInfo = createAndAppend('div', root);
const contribs = createAndAppend('div', root);
select.addEventListener('change', evt => {
const selectedRepo = evt.target.value;
const repo = data.filter(r => r.name == selectedRepo)[0];
console.log(repo);
repoInfo.innerHTML = '';
contribs.innerHTML = '';
const addInfo = (label, value) => {
const container = createAndAppend('div', repoInfo);
createAndAppend('span', container, { text: label });
createAndAppend('span', container, { text: value });
};
addInfo('Name: ', repo.name);
addInfo('Description: ', repo.description);
addInfo('Number of forks: ', repo.forks);
addInfo('Updated: ', repo.updated_at);
//or instead of using the addInfo function we can use these lines:
// createAndAppend('div', repoInfo, { text: `Descriptions: ${repo.description}` });
// createAndAppend('div', repoInfo, { text: `Number of Forks: ${repo.forks}` });
// createAndAppend('div', repoInfo, { text: `Updated at:${repo.updated_at}` });
const contribsUrl = repo.contributors_url;
fetchJSON(contribsUrl, (err, contribData) => {
if (err) {
createAndAppend('div', root, { text: err.message, class: 'alert-error' });
} else{
contribData.forEach(contributor => {
createAndAppend('div', contribs, { text: contributor.login });
});
};
});
}
});
}
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
window.onload = () => main(HYF_REPOS_URL);
}

const REPOS_URL = 'https://api.github.com/orgs/foocoding/repos?per_page=100';
// 'use strict';

window.onload = () => main(REPOS_URL);
}
// {
// function fetchJSON(url, cb) {
// const xhr = new XMLHttpRequest();
// xhr.open('GET', url);
// xhr.responseType = 'json';
// xhr.onload = () => {
// if (xhr.status < 400) {
// cb(null, xhr.response);
// } else {
// cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`));
// }
// };
// xhr.onerror = () => cb(new Error('Network request failed'));
// xhr.send();
// }

// function createAndAppend(name, parent, options = {}) {
// // name: the elem/thing that we want to create.
// //parent: where we shall append the child
// //options. what we want to change/add like attributes,class,text,id
// const elem = document.createElement(name);
// parent.appendChild(elem);
// Object.keys(options).forEach(key => {
// const value = options[key];
// if (key === 'text') {
// elem.textContent = value;
// } else {
// elem.setAttribute(key, value);
// }
// });
// return elem;
// }

// function main(url) {
// fetchJSON(url, (err, data) => {
// const root = document.getElementById('root');
// if (err) {
// createAndAppend('div', root, { text: err.message, class: 'alert-error' });
// } else {
// //createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) });

// const select = createAndAppend('select', root);
// createAndAppend('option', select, { text: 'Choose your favorite repo below:' });
// data.forEach(repo => {
// const name = repo.name;
// createAndAppend('option', select, { text: name });
// });

// const repoInfo = createAndAppend('div', root);
// const contribs = createAndAppend('div', root);
// select.addEventListener('change', evt => {
// const selectedRepo = evt.target.value;
// const repo = data.filter(r => r.name == selectedRepo)[0]; // ask about this part here
// console.log(repo);
// repoInfo.innerHTML = '';
// contribs.innerHTML = '';

// const addInfo = (label, value) => {
// const container = createAndAppend('div', repoInfo);
// createAndAppend('span', container, { text: label });
// createAndAppend('span', container, { text: value });
// };
// addInfo('Name: ', repo.name);
// addInfo('Description: ', repo.description);
// addInfo('Number of forks: ', repo.forks);
// addInfo('Updated: ', repo.updated_at);
// //or instead of using the addInfo function we can use these lines:
// // createAndAppend('div', repoInfo, { text: `Descriptions: ${repo.description}` });
// // createAndAppend('div', repoInfo, { text: `Number of Forks: ${repo.forks}` });
// // createAndAppend('div', repoInfo, { text: `Updated at:${repo.updated_at}` });
// const contribsUrl = repo.contributors_url;
// fetchJSON(contribsUrl, (err, contribData) => {
// contribData.forEach(contributor => {
// createAndAppend('div', contribs, { text: contributor.login });
// });
// });
// });
// }
// });
// }
// }

// const REPOS_URL = 'https://api.github.com/orgs/foocoding/repos?per_page=100';

// window.onload = () => main(REPOS_URL);
16 changes: 14 additions & 2 deletions homework/style.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
.alert-error {
color: red;
}
color: red;
}
header {
width: 800px;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's usually not a good idea to hard-code a specific width, try to display as a fraction of the avaliable space instead. It breaks on mobile :/

height: 70px;
background-color: purple;
text-emphasis-color: white;
text-align: left;
}
.info-container {
text-align: left;
width: 40;
height: 30;
}