-
Notifications
You must be signed in to change notification settings - Fork 0
Week one homework #1
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
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,23 +1,37 @@ | ||
| <!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> | ||
|
|
||
| <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="./style.css"> | ||
| </head> | ||
|
|
||
| <body> | ||
| <div id="root"></div> | ||
| <script src="./index.js"></script> | ||
| </body> | ||
|
|
||
| </html> | ||
| <body> | ||
| <div class="header" id="selector_header"> | ||
| <form name="chooseRepo"> | ||
| <select onchange class="selector" id="repoSelect" name="repoSelect"> </select> | ||
| </form> | ||
| </div> | ||
| <div class="flex-wrapper"> | ||
| <div id="repo"> | ||
| Repo facts | ||
| </div> | ||
| <div id="contribDiv"> | ||
| Contributors | ||
| </div> | ||
| </div> | ||
| <div id="root"></div> | ||
| <script src="./index.js"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,138 @@ | ||
| '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 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 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 repoSelector = document.querySelector('#repoSelect'); | ||
| const contribDiv = document.querySelector('#contribDiv'); | ||
| const repoDiv = document.querySelector('#repo'); | ||
|
|
||
| function fetchJSON(url, cb) { | ||
| const xhr = new XMLHttpRequest(); | ||
| xhr.open('GET', url); | ||
| xhr.setRequestHeader = 'Catsudemo https://api.github.com'; | ||
| 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 = {}) { | ||
| 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 initPage(data, repoSelector) { | ||
| buildSelect(data, repoSelector); | ||
| } | ||
|
|
||
| function buildSelect(data, repoSelector) { | ||
| data | ||
| .map(repo => repo.name) | ||
| .sort() | ||
| .forEach(name => { | ||
| createAndAppend('OPTION', repoSelector, { text: name, value: 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. It seems like you add options to the select box multiple times, see if you can figure out what's causing that! |
||
| }); | ||
| } | ||
| } | ||
|
|
||
| const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; | ||
| 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) }); | ||
| // buildSelect(data,repoSelector) | ||
| initPage(data, repoSelector); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| window.onload = () => main(HYF_REPOS_URL); | ||
| const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; | ||
|
|
||
| function displayContrib(contributor, contributions, avatar) { | ||
| const eachPersonUl = document.createElement('ul'); | ||
|
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 find this function and the const eachPersonUl = document.createElement('ul');
const contributorName = document.createElement('li');
contributorName.innerText = `Username: ${contributor}`;
eachPersonUl.appendChild(contributorName);
const eachContributions = document.createElement('li');
eachContributions.innerText = `Contributions: ${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. Also, as the code provides the |
||
| const contributorName = document.createElement('li'); | ||
| contributorName.innerText = `Username: ${contributor}`; | ||
| eachPersonUl.appendChild(contributorName); | ||
| const eachContributions = document.createElement('li'); | ||
| eachContributions.innerText = `Contributions: ${contributions}`; | ||
| eachPersonUl.appendChild(eachContributions); | ||
| contribDiv.appendChild(eachPersonUl); | ||
| const eachAvatar = document.createElement('IMG'); | ||
| eachAvatar.innerhtml = `<img src=${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. I find this row a bit strange (and it looks like it isn't working as expected). You can actually set the attribute like this (and I think it would work): eachAvatar.setAttribute('src', avatar); |
||
| contribDiv.appendChild(eachAvatar); | ||
| } | ||
|
|
||
| function displayRepo(repository, description, forks, updated) { | ||
| const eachRepoUl = document.createElement('ul'); | ||
| const eachName = document.createElement('li'); | ||
| eachName.innerText = `Repository: ${repository}`; | ||
| eachRepoUl.appendChild(eachName); | ||
| const eachDescription = document.createElement('li'); | ||
| eachDescription.innerText = `Contributions: ${description}`; | ||
| eachRepoUl.appendChild(eachDescription); | ||
| const eachForks = document.createElement('li'); | ||
| eachForks.innerText = `Forks: ${forks}`; | ||
| eachRepoUl.appendChild(eachForks); | ||
| const eachUpdated = document.createElement('li'); | ||
| eachUpdated.innerText = `Updated: ${updated}`; | ||
| eachRepoUl.appendChild(eachUpdated); | ||
| repoDiv.appendChild(eachRepoUl); | ||
| } | ||
|
|
||
| const selectElement = document.querySelector('.selector'); | ||
|
|
||
| selectElement.addEventListener('change', event => { | ||
|
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 looks like you're doing this outside of the main function. This can cause problems down the line (but looks like it's working during my tests), as some things might not be ready before |
||
| const result = document.querySelector('.result'); | ||
| 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`; | ||
|
|
||
| main(HYF_REPOS_URL); | ||
|
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'm not sure why you're calling |
||
|
|
||
| fetchJSON(repoURL, (err, data) => { | ||
| const root = document.getElementById('root'); | ||
| if (err) { | ||
| createAndAppend('div', root, { text: err.message, class: 'alert-error' }); | ||
| } else { | ||
| repoDiv.innerHTML = ''; | ||
| let repository = data.name; | ||
| let description = data.description; | ||
| let forks = data.forks; | ||
| let updated = data.updated_at; | ||
| displayRepo(repository, description, forks, updated); | ||
| } | ||
| console.log('Here is the repodata'); | ||
| }); | ||
|
|
||
| fetchJSON(contribURL, (err, data) => { | ||
| const root = document.getElementById('root'); | ||
| if (err) { | ||
| createAndAppend('div', root, { text: err.message, class: 'alert-error' }); | ||
| } else { | ||
| contribDiv.innerHTML = ''; | ||
| data.forEach(object => { | ||
| let contributor = object.login; | ||
| let contributions = object.contributions; | ||
| let avatar = object.avatar_url; | ||
| displayContrib(contributor, contributions, avatar); | ||
| }); | ||
| console.log('Here is the contribdata'); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| window.onload = () => main(HYF_REPOS_URL); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| .header { | ||
| background-color: #176ab0; | ||
| color: white; | ||
| text-align: center; | ||
| padding: 5px; | ||
| } | ||
|
|
||
| #main-content { | ||
|
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 isn't used, so it's best to remove it. |
||
| width: 350px; | ||
| float: left; | ||
| padding: 10px; | ||
| } | ||
|
|
||
| #footer { | ||
|
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 is never used, so it's best to remove it. |
||
| background-color: #176ab0; | ||
| color: white; | ||
| clear: both; | ||
| text-align: center; | ||
| padding: 5px; | ||
| } | ||
|
|
||
| .flex-wrapper { | ||
| font-family: Abel; | ||
| background-color: grey; | ||
| text-align: left; | ||
| padding-top: 25px; | ||
| padding-bottom: 25px; | ||
| font-size: 20px; | ||
| display: flex; | ||
| flex-direction: row; | ||
| justify-content: center; | ||
| background-color: #2E2C2F; | ||
| color: #444; | ||
| } | ||
|
|
||
| #repo { | ||
|
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. The contents of |
||
| background-color: whitesmoke; | ||
| border-color: #2E2C2F; | ||
| width: 40%; | ||
| border: 5px; | ||
| padding: 20px; | ||
| margin: 20px; | ||
| } | ||
|
|
||
| #contribDiv { | ||
| background-color: whitesmoke; | ||
| border-color: #2E2C2F; | ||
| width: 40%; | ||
| border: 5px; | ||
| padding: 20px; | ||
| margin: 20px; | ||
| } | ||
|
|
||
| @media screen and (max-width: 600px) { | ||
|
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, looks good on mobile! |
||
| .flex-wrapper { | ||
| -webkit-flex-direction: column; | ||
| flex-direction: column; | ||
| } | ||
| #repo { | ||
| width: 80%; | ||
| } | ||
| #contribDiv { | ||
| width: 80%; | ||
| } | ||
| } | ||
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.
From what I can see these variables aren't used, it's always a good idea to remove stuff that you don't need. Keeping them around will likely confuse you the next time you look at the code.