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
Prev Previous commit
Next Next commit
working on index.js file
  • Loading branch information
chichiglacierz committed Aug 31, 2019
commit 9e5e42baaf62fa5e58a7a57c17fa49ccab213083
30 changes: 1 addition & 29 deletions homework/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,8 @@

<body>
<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>
<div id="root"></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>
172 changes: 102 additions & 70 deletions homework/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,21 @@
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status < 400) {
resolve(xhr.response);
} else {
reject(new Error(xhr.statusText));
}
xhr.onload = () => {
if (xhr.status < 400) {
resolve(xhr.response);
} else {
reject(new Error(xhr.statusText));
}
console.log();
};
xhr.onerror = () => reject(new Error('Network Request failed'));
console.log();
xhr.send();
});
}
const REPOS_URL = 'https://api.github.com/orgs/foocoding/repos?per_page=100';

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 => {
Expand All @@ -38,70 +34,106 @@
return elem;
}

function main(url) {
fetchJSON(url, (err, data) => {
const root = document.getElementById('root');
function fetchContributors(contributors, contribContainer) {
createAndAppend('p', contribContainer, { text: 'Contributors', id: 'contributors' });
contributors.forEach(contributor => {
const contribDiv = createAndAppend('div', contribContainer, { class: 'contrib-info' });
createAndAppend('img', contribDiv, {
src: contributor.avatar_url,
text: contributor.login,
height: 150,
width: 150,
class: 'image',
});
createAndAppend('a', contribDiv, {
text: contributor.login,
href: contributor.html_url,
target: 'blank',
class: 'contrib-link',
});
createAndAppend('p', contribDiv, {
text: contributor.contributions,
class: 'contributor-badge',
});
});
}

function fetchAndAddContribData(repoInfo, repoContainer, contribContainer, root) {
repoContainer.innerHTML = repo.name;
contribContainer.innerHTML = repo.name;

createAndAppend('span', repoContainer, { text: 'Repository', class: 'repository' });
createAndAppend('a', repoContainer, {
text: '${repoInfo.name}',
href: repoInfo.html_url,
target: '_blank',
class: 'repo-link',
});

createAndAppend('p', repoInfo, {
text: 'Description: ${repoInfo.description}',
class: 'repo-child',
});

if (err) {
createAndAppend('p', repoInfo, {
text: 'Fork: ${repoInfo.forks}',
class: 'repo-child',
});

createAndAppend('p', repoInfo, {
text: 'Updated: ${Updated: ${updatedAt.toLocaleString()}',
class: 'repo-child',
});

fetchJSON(repoInfo.contributors_url)
.then(contributors => {
addContributors(contributors, contribContainer);
})
.catch(err => {
createAndAppend('div', root, { text: err.message, class: 'alert-error' });
} else {
//createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) });
const header = createAndAppend('header', root, { class: 'header' });
const h1 = createAndAppend('h1', header, { text: 'FooCoding Repos', class: 'h1' });

const select = createAndAppend('select', root, { class: 'select' });
createAndAppend('option', select, { text: 'Choose your favorite repo below:' });

data.forEach(repo => {
const name = repo.name;
createAndAppend('option', select, { text: name });
//console.log(name);
});

const repoInfo = createAndAppend('div', root, { class: 'left-div' });
const contribs = createAndAppend('div', root, { class: 'right-div' });
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.name);
repoInfo.innerHTML = repo.name;
contribs.innerHTML = repo.name;

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 });
//createAndAppend('a', contribs, { href: contributor.html.url, setAttribute: 'href' });
createAndAppend('img', contribs, {
src: contributor.avatar_url,
height: 150,
width: 150,
id: 'img',
});
});
});
});
}
});





function populateSelect(root, repos) {
const header = createAndAppend('header', root, { class: 'header' });
createAndAppend('p', header, { text: 'HYF Repositories', id: 'p' });
const select = createAndAppend('select', header, { id: 'select' });

repos.sort((a, b) => a.name.localeCompare(b.name));

repos.forEach((repo, index) => {
createAndAppend('option', select, { text: repo.name, value: index });
});
//console.log(populateSelect);

const mainContainer = createAndAppend('div', root, { id: 'main' });
const repoContainer = createAndAppend('div', mainContainer, { id: 'repo-container' });
const contribContainer = createAndAppend('div', mainContainer, {
id: 'contributor-container',
});



}
}


const header = createAndAppend('header', root, { class: 'header' });
const h1 = createAndAppend('h1', header, { text: 'FooCoding Repos', class: 'h1' });

const select = createAndAppend('select', root, { class: 'select' });
createAndAppend('option', select, { text: 'Choose your favorite repo below:' });

data.forEach(repo => {
const name = repo.name;
createAndAppend('option', select, { text: name });
});
}
const REPOS_URL = 'https://api.github.com/orgs/foocoding/repos?per_page=100';

window.onload = () => main(REPOS_URL);

}