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

Skip to content

Added week 2 homework #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Binary file added .DS_Store
Binary file not shown.
Binary file added Week2/.DS_Store
Binary file not shown.
Binary file added apps/.DS_Store
Binary file not shown.
Binary file added homework/.DS_Store
Binary file not shown.
81 changes: 0 additions & 81 deletions homework/App.js

This file was deleted.

19 changes: 0 additions & 19 deletions homework/Contributor.js

This file was deleted.

33 changes: 0 additions & 33 deletions homework/Repository.js

This file was deleted.

1 change: 1 addition & 0 deletions homework/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Util {
}

static fetchJSON(url) {

return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
Expand Down
37 changes: 0 additions & 37 deletions homework/index.html

This file was deleted.

3 changes: 0 additions & 3 deletions homework/style.css

This file was deleted.

Binary file added homework/week-2/.DS_Store
Binary file not shown.
File renamed without changes
39 changes: 39 additions & 0 deletions homework/week-2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="theme-color" content="#000000" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no" />
<link rel="apple-touch-icon" href="./hyf.png" />
<link rel="shortcut icon" type="image/png" href="./hyf.png" />
<title>HYF-GITHUB</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,700" rel="stylesheet" />
<link rel="stylesheet" href="./newStyle.css" />
</head>

<body>
<div class="header" id="selector_header">
<form name="chooseRepo">
<select onchange class="selector" id="repoSelect" name="repoSelect" selected=""> </select>
</form>
</div>
<div class="flex-wrapper">
<div id="repo">
Repo facts
</div>
<div id="contribDiv">
<b>Contributions</b>
<div id=contributorsDiv>
</div>
</div>
</div>
<div id="root"></div>
<script src="./indexscript.js"></script>

</body>

</html>
118 changes: 118 additions & 0 deletions homework/week-2/indexscript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict';

//fetch the data from the URL and convert to json
// create the select element and populate with options. These should be the name of the repos in the json.
// when the user changes the selector go to the URL for that repo and get the json
// create a ul showing relevant data
// go to the URL of each contributor in the repo and fetch the data
// create a ul showing relevant data

const repoSelector = document.querySelector('#repoSelect');
const contribDiv = document.querySelector('#contributorsDiv');
const repoDiv = document.querySelector('#repo');
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';


function createAndAppend(name, parent, options = {}) {
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 buildSelect(data, repoSelector) {

data
.map(repo => repo.name)
.map(repo => repo.toUpperCase())
.sort()
.forEach(name => {
createAndAppend('OPTION', repoSelector, { text: name, value: name });
})
}

function main(url) {

fetch(url)
.then(response => {
if (response.ok) return response
throw new Error(response.statusText)
})
.then(response => response.json())
.then(repoSelector.innerHTML = '')
.then(json => buildSelect(json, repoSelector))
.catch(error => createAndAppend('div', root, { text: error, class: 'alert-error' }))

const selectElement = document.querySelector('.selector');

selectElement.addEventListener('change', event => {
let repo = document.getElementById('repoSelect').value;
let repoURL = `https://api.github.com/repos/HackYourFuture/${repo}`;
let contribURL = `https://api.github.com/repos/HackYourFuture/${repo}/contributors`;

displayPage(repoURL, contribURL);

})

}

function displayContrib(contributor, contributions, avatar) {

Choose a reason for hiding this comment

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

Nice splitting up in functions!

let eachPersonUl = createAndAppend('ul', contribDiv)
createAndAppend('IMG', eachPersonUl, { src: avatar, width: "42", id: 'avatar' })

Choose a reason for hiding this comment

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

You shouldn't add img elements as a direct child inside ul/ol. Only li is allowed, so you should wrap the img inside of an li.

createAndAppend('li', eachPersonUl, { class: "badge", id: 'contbadge', text: `${contributions}` })

Choose a reason for hiding this comment

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

Id-attributes must be unique on the entire page, here and on the line above you add multiple elements with the same id. You should use class attribute instead (classes can be used on multiple places in the document).

createAndAppend('li', eachPersonUl, { text: `${contributor}` })
}

function displayRepo(repository, description, forks, updated) {
let eachRepoUl = createAndAppend('ul', repoDiv)
createAndAppend('li', eachRepoUl, { text: `Repository: ${repository}` })
createAndAppend('li', eachRepoUl, { text: `Description: ${description}` })
createAndAppend('li', eachRepoUl, { text: `Forks: ${forks}` })
createAndAppend('li', eachRepoUl, { text: `Updated: ${updated}` })
}

function displayPage(repoURL, contribURL) {
fetch(repoURL)
.then(response => {
if (response.ok) return response
throw new Error(response.statusText)
})
.then(response => response.json())
.then(data => {
repoDiv.innerHTML = '';
let repository = data.name;

Choose a reason for hiding this comment

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

Always use const instead of let when the value doesn't change.

let description = data.description;
let forks = data.forks;
let updated = data.updated_at.split("T", 1);
displayRepo(repository, description, forks, updated);
})
.catch(error => createAndAppend('div', root, { text: error, class: 'alert-error' }));

fetch(contribURL)
.then(response => {
if (response.ok) return response
throw new Error(response.statusText)
})
.then(response => response.json())
.then(data => {
contribDiv.innerHTML = '';
data.forEach(object => {
let contributor = object.login;
let contributions = object.contributions;
let avatar = object.avatar_url;
displayContrib(contributor, contributions, avatar);
});
})
.catch(error => createAndAppend('div', root, { text: error, class: 'alert-error' }))
}



window.onload = () => main(HYF_REPOS_URL)
22 changes: 22 additions & 0 deletions homework/newStyle.css → homework/week-2/newStyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
padding: 5px;
}

.badge {

Choose a reason for hiding this comment

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

This doesn't work well when there are more than 100 contributions, don't forget to test with larger amounts of data!

background: grey;
float: right;
color: white;
width: 18px;
height: 18px;
text-align: center;
line-height: 18px;
border-radius: 50%;
padding: 10px;
}

.flex-wrapper {
font-family: Abel;
background-color: grey;
Expand All @@ -33,6 +45,11 @@
color: #444;
}

#contributorsDiv.IMG {
padding: 20px;
margin: 20px;
}

#repo {
background-color: whitesmoke;
border-color: #2E2C2F;
Expand All @@ -51,6 +68,11 @@
margin: 20px;
}

ul {
list-style-type: none;
padding: 10px;
}

@media screen and (max-width: 600px) {
.flex-wrapper {
-webkit-flex-direction: column;
Expand Down
Loading