-
Notifications
You must be signed in to change notification settings - Fork 278
adding homework js3-w1 #21
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 |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| 'user strict'; | ||
|
|
||
|
|
||
| const url = "https://api.github.com/orgs/HackYourFuture/repos?per_page=100"; | ||
|
|
||
| function fetchJSON(url, cb) { | ||
|
|
||
| const request = new XMLHttpRequest(); | ||
|
|
||
| request.open("GET", url); | ||
| request.responseType = "json"; | ||
|
|
||
| request.onreadystatechange = () => { | ||
| if (request.readyState === 4) { | ||
| if (request.status < 400) { | ||
| cb(null, request.response); | ||
| } else { | ||
| cb(new Error(request.statusText)); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| request.send(); | ||
| } | ||
|
|
||
|
|
||
| function callback(error, data) { | ||
| if (error !== null) { | ||
| } else { | ||
| renderRepo(data); | ||
| } | ||
| } | ||
| fetchJSON(url, callback); | ||
|
|
||
| function renderRepo(repo) { | ||
| const root = document.getElementById('root'); | ||
| const listItem = createAndAppend('div', root); | ||
| listItem.id = 'listItem'; | ||
| const listItemName = createAndAppend('p', listItem); | ||
| listItemName.innerHTML = 'HackYourFuture Repositories'; | ||
| const select = createAndAppend('select', listItem); | ||
| const repoBox = createAndAppend('div', root); | ||
| repoBox.id = 'repo-box'; | ||
| const contributorBox = createAndAppend('div', root); | ||
| contributorBox.id = 'contributors-box'; | ||
| select.addEventListener("change", () => repoInfo(select.value)); | ||
| repo.forEach(repo => { | ||
| const option = createAndAppend('option', select); | ||
| option.innerHTML = repo.name; | ||
| option.setAttribute('value', repo.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. You forgot to sort the elements of the list in an alphabetical order! |
||
| }); | ||
| repoInfo(select.value); | ||
| } | ||
|
|
||
| function createAndAppend(tagName, parent) { | ||
| const element = document.createElement(tagName); | ||
| parent.appendChild(element); | ||
| return element; | ||
| } | ||
|
|
||
| function repoInfo(repoName) { | ||
| const urlContributor = 'https://api.github.com/repos/HackYourFuture/' + repoName + '/contributors'; | ||
| const repoUrl = 'https://api.github.com/repos/HackYourFuture/' + repoName; | ||
|
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 should not put static links inside functions, you should save these strings in a variable and then get the value from the variable. The way you did it gets the work done, but it will cause problems when you try to change the link (and forget to change it on some places). |
||
| fetchJSON(urlContributor, listRenderContributor); | ||
| fetchJSON(repoUrl, repoInfoData); | ||
| } | ||
|
|
||
| function listRenderContributor(err, dataContributor) { | ||
| if (err !== null) { | ||
| } else { | ||
| renderContributors(dataContributor); | ||
| } | ||
| } | ||
| function repoInfoData(err, dataContributor) { | ||
| if (err !== null) { | ||
| } else { | ||
| renderRepoToHTML(dataContributor); | ||
| } | ||
| } | ||
|
|
||
| function renderRepoToHTML(repoInfo) { | ||
| const repoBox = document.getElementById('repo-box'); | ||
| repoBox.innerHTML = ''; | ||
| const p = createAndAppend('p', repoBox); | ||
| const repoName = createAndAppend('p', repoBox); | ||
| const forks = createAndAppend('p', repoBox); | ||
| const updated = createAndAppend('p', repoBox); | ||
| repoName.innerHTML = 'repo: ' + repoInfo.name; | ||
| forks.innerHTML = 'Forks: ' + repoInfo.forks_count; | ||
| updated.innerHTML = 'Updated: ' + repoInfo.updated_at; | ||
| p.innerHTML = 'Description: ' + repoInfo.description; | ||
|
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 use spaces, tabs, etc. to format the text on the webpage, always use CSS for this. (Can you please correct this in the next assignment when you get time?) |
||
| } | ||
|
|
||
|
|
||
|
|
||
| function createAndAppend2(name, parent) { | ||
| const elem = document.createElement(name); | ||
| parent.appendChild(elem); | ||
| return elem; | ||
| } | ||
|
|
||
| function renderContributors(contributors) { | ||
| const container = document.getElementById('contributors-box'); | ||
| container.innerHTML = ''; | ||
| const Contributions = createAndAppend('p', container); | ||
| Contributions.innerHTML = 'Contributions'; | ||
| const ul = createAndAppend2('ul', container); | ||
| contributors.forEach(contributor => { | ||
| const li = createAndAppend2('li', ul); | ||
| li.innerHTML = ""; | ||
| const img = createAndAppend('img', li); | ||
| li.innerHTML = contributor.login + " " + contributor.contributions + "<img src=" + contributor.avatar_url + ">"; | ||
| img.setAttribute('src', contributor.avatar_url); | ||
| li.setAttribute('value', contributor.login); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <link rel="stylesheet" href="Style.css" /> | ||
|
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. Somehow your CSS file was not linked, turns out that stylesheet is 'style.css' and not 'Style.css' (yep, the value for the source is case-sensitive) |
||
| <title>HYF Repos</title> | ||
| </head> | ||
|
|
||
| <body> | ||
| <div id="root"></div> | ||
|
|
||
| <script src="app.js"></script> | ||
| </body> | ||
|
|
||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
|
|
||
|
|
||
| html { | ||
| margin: 50px; | ||
| font-family: sans-serif; | ||
| font-size: 1.2em; | ||
|
|
||
| } | ||
|
|
||
| div #listItem { | ||
| background-color: #1b0ed4; | ||
| width: 100%; | ||
| height: 100px; | ||
| } | ||
|
|
||
| div #listItem p { | ||
| font-size: 2.2em; | ||
| text-align: center; | ||
| color: white; | ||
| float: left; | ||
| margin: 25px; | ||
| } | ||
|
|
||
| select { | ||
| font-size: 1.2em; | ||
| margin-top: 25px; | ||
| margin-left: 10px; | ||
| border: 1px solid; | ||
| padding: 10px 10px; | ||
| } | ||
|
|
||
| div #repo-box { | ||
| border: 1px solid; | ||
| padding: 20px 20px; | ||
| width: 40%; | ||
| margin-top: 5%; | ||
| width: 45%; | ||
| display: inline-block; | ||
| } | ||
|
|
||
| div #contributors-box { | ||
| border: 1px solid; | ||
| padding: 20px 20px; | ||
| width: 40%; | ||
| margin-top: 5%; | ||
| display: block; | ||
| float: right; | ||
| } | ||
|
|
||
| li { | ||
| list-style: none; | ||
| text-align: center; | ||
| height: 100px; | ||
| } | ||
|
|
||
| img { | ||
| width: 75px; | ||
| height: 75px; | ||
| float: left; | ||
| } |
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.
You didn't handle the error! If the server is unreachable, the page doesn't load at all. This isn't the best thing to do as the user would be wondering what happened. You should put text on the page saying that there was an error retrieving the data from the server.