-
Notifications
You must be signed in to change notification settings - Fork 12
Week2 #9
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?
Week2 #9
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
'use strict'; | ||
|
||
{ | ||
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 fetchJSON(url) { | ||
return new Promise((resolve, reject) => { | ||
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. Looks good! |
||
const xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url); | ||
xhr.responseType = 'json'; | ||
xhr.onload = () => { | ||
if (xhr.status < 400) { | ||
resolve(xhr.response); | ||
} else { | ||
reject(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); | ||
} | ||
}; | ||
xhr.onerror = () => reject(new Error('Network request failed')); | ||
xhr.send(); | ||
}); | ||
} | ||
|
||
function createAndAppend(name, parent, options = {}) { | ||
|
@@ -30,18 +32,103 @@ | |
return elem; | ||
} | ||
|
||
function main(url) { | ||
fetchJSON(url, (err, data) => { | ||
const root = document.getElementById('root'); | ||
if (err) { | ||
function renderContributors(contributors, contributorsContainer) { | ||
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. Nice naming of the function and arguments! |
||
createAndAppend('p', contributorsContainer, { text: 'Contributors', id: 'contributors' }); | ||
contributors.forEach(contributor => { | ||
const contributorsDetailsDiv = createAndAppend('div', contributorsContainer, { | ||
class: 'contributor-details', | ||
}); | ||
createAndAppend('img', contributorsDetailsDiv, { | ||
src: contributor.avatar_url, | ||
alt: contributor.login, | ||
class: 'image', | ||
}); | ||
createAndAppend('a', contributorsDetailsDiv, { | ||
text: contributor.login, | ||
href: contributor.html_url, | ||
target: '_blank', | ||
class: 'contributor-data', | ||
}); | ||
createAndAppend('p', contributorsDetailsDiv, { | ||
text: contributor.contributions, | ||
class: 'contributor-badge', | ||
}); | ||
}); | ||
} | ||
|
||
function fetchAndRenderData(selectedRepo, repoContainer, contributorsContainer, root) { | ||
repoContainer.innerHTML = ''; | ||
contributorsContainer.innerHTML = ''; | ||
|
||
const updatedAt = new Date(selectedRepo.updated_at); | ||
createAndAppend('span', repoContainer, { text: 'Repository: ', class: 'repo-child' }); | ||
createAndAppend('a', repoContainer, { | ||
text: `${selectedRepo.name}`, | ||
href: selectedRepo.html_url, | ||
target: '_blank', | ||
class: 'repo-child right-cell', | ||
}); | ||
createAndAppend('p', repoContainer, { | ||
text: `Description: ${selectedRepo.description}`, | ||
class: 'repo-child', | ||
}); | ||
createAndAppend('p', repoContainer, { | ||
text: `Fork: ${selectedRepo.forks}`, | ||
class: 'repo-child', | ||
}); | ||
createAndAppend('p', repoContainer, { | ||
text: `Updated: ${updatedAt.toLocaleString()}`, | ||
class: 'repo-child', | ||
}); | ||
|
||
fetchJSON(selectedRepo.contributors_url) | ||
.then(contributors => { | ||
renderContributors(contributors, contributorsContainer); | ||
}) | ||
.catch(err => { | ||
createAndAppend('div', root, { text: err.message, class: 'alert-error' }); | ||
} else { | ||
createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) }); | ||
} | ||
}); | ||
} | ||
|
||
function dropDown(root, repos) { | ||
const header = createAndAppend('div', root, { id: 'header' }); | ||
createAndAppend('p', header, { text: 'HYF Repositories', class: 'header' }); | ||
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, | ||
}); | ||
}); | ||
|
||
const mainContainer = createAndAppend('div', root, { id: 'main' }); | ||
const repoContainer = createAndAppend('div', mainContainer, { id: 'repo-container' }); | ||
const contributorsContainer = createAndAppend('div', mainContainer, { | ||
id: 'contributor-container', | ||
}); | ||
|
||
select.addEventListener('change', () => { | ||
const selectedRepo = repos[select.value]; | ||
fetchAndRenderData(selectedRepo, repoContainer, contributorsContainer, root); | ||
}); | ||
fetchAndRenderData(repos[0], repoContainer, contributorsContainer, root); | ||
} | ||
|
||
function main(url) { | ||
const root = document.getElementById('root'); | ||
fetchJSON(url) | ||
.then(repos => { | ||
dropDown(root, repos); | ||
}) | ||
.catch(err => { | ||
createAndAppend('div', root, { text: err.message, class: 'alert-error' }); | ||
}); | ||
} | ||
|
||
const REPOS_URL = 'https://api.github.com/orgs/foocoding/repos?per_page=100'; | ||
const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; | ||
|
||
window.onload = () => main(REPOS_URL); | ||
window.onload = () => main(HYF_REPOS_URL); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,64 @@ | ||
#root { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-start; | ||
justify-content: center; | ||
} | ||
@media screen and (max-width: 768px) { | ||
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. I don't think this works, did you try it? |
||
.h1.h1 { | ||
width: 100%; | ||
} | ||
.right-div { | ||
width: 100%; | ||
} | ||
.left-div { | ||
width: 100%; | ||
} | ||
} | ||
.alert-error { | ||
color: red; | ||
} | ||
color: red; | ||
} | ||
.h1 { | ||
text-align: center; | ||
} | ||
.container { | ||
display: flex; | ||
flex-direction: row; | ||
align-items: flex-start; | ||
margin: 10px; | ||
} | ||
|
||
.right-div { | ||
color: green; | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
flex-wrap: wrap; | ||
margin: 10px; | ||
flex-grow: 1; | ||
height: 200px; | ||
width: 200px; | ||
} | ||
|
||
.left-div { | ||
color: brown; | ||
display: flex; | ||
flex-wrap: wrap; | ||
flex-direction: column; | ||
flex-grow: 1; | ||
|
||
align-items: right; | ||
margin: 10px; | ||
} | ||
header { | ||
width: 800px; | ||
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. 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; | ||
} |
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.
Why wrapping the entire app in
<header>
, it seems only parts of the app should be in header?