-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
750e99d
61470a8
e1416da
72d6c58
ed9bd03
2edb0b3
68a393c
423f278
8d40a9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
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> |
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) { | ||
let eachPersonUl = createAndAppend('ul', contribDiv) | ||
createAndAppend('IMG', eachPersonUl, { src: avatar, width: "42", id: 'avatar' }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't add |
||
createAndAppend('li', eachPersonUl, { class: "badge", id: 'contbadge', text: `${contributions}` }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always use |
||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,18 @@ | |
padding: 5px; | ||
} | ||
|
||
.badge { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -33,6 +45,11 @@ | |
color: #444; | ||
} | ||
|
||
#contributorsDiv.IMG { | ||
padding: 20px; | ||
margin: 20px; | ||
} | ||
|
||
#repo { | ||
background-color: whitesmoke; | ||
border-color: #2E2C2F; | ||
|
@@ -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; | ||
|
There was a problem hiding this comment.
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!