-
Notifications
You must be signed in to change notification settings - Fork 280
Conversation
Finishes homework week1
Adds styles.
remarcmij
left a comment
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.
Hi @kawaraa, your application works so I will approve your PR. But I think you made some unfortunate design decisions which makes your code rather cumbersome to maintain. Please take good note of the comments below.
| xhr.responseType = 'json'; | ||
| xhr.onload = () => { | ||
| if (xhr.status < 400) { | ||
| if (xhr.status === 200) { |
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.
By changing the condition of the if statement you are now considering any status code not equal to 200 as an error. This is an error by itself. Any code equal or greater than 400 is an error. All codes less than 400 are not errors. See: https://www.restapitutorial.com/httpstatuscodes.html
If you expect a 200 code but get some other code but still less than 400 you need to find some other way of dealing with. But you can't report it to the user as an HTTP error.
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.
This still needs to be fixed.
homework/src/index.js
Outdated
| cb(null, xhr.response); | ||
| } else { | ||
| cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`)); | ||
| cb({ text: `Error: ${xhr.status} - ${xhr.statusText}`, id: "id", val: "error" }); |
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.
By convention, error information is reported through a standard JavaScript Error object (as the original code did). Why did you choose to change this?
homework/src/index.js
Outdated
| }; | ||
| xhr.onerror = () => cb(new Error('Network request failed')); | ||
| xhr.onerror = () => { | ||
| cb({ text: "Network request failed", id: "id", val: "error" }); |
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.
Use an Error object (as per the original code).
homework/src/index.js
Outdated
| } | ||
|
|
||
| function createAndAppend(name, parent, options = {}) { | ||
| function createEl(name, parent, obj) { |
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.
Your createEl() function is a less capable version than the original createAndAppend() function. The original createAndAppend() could set any attribute. Your version can only deal with the id attribute. I have a hard time understanding why you chose to dumbed down this function.
homework/src/index.js
Outdated
| const header = createEl("header", root); | ||
| createEl("h3", header, { text: "HYF Repositories" }); | ||
| const select = createEl("select", header); | ||
| let names = []; |
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.
ESLint warns that you could have used const instead of let. You should follow up on such warnings.
homework/src/index.js
Outdated
| } | ||
| }); | ||
| if (obj) { | ||
| if (obj.text) { elem.innerHTML = obj.text; } |
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.
The original function used innerText instead of innerHTML. We did that on purpose, because we want to discourage this use of innerHTML for creating HTML elements.
homework/src/index.js
Outdated
| let a = createEl("a", td, { text: details.name, id: "href", val: details.html_url }); | ||
| a.setAttribute("target", "_blank"); | ||
| if (details.description) { | ||
| createEl("tr", table, { text: `<th>Description:</th><td>${details.description}</td>` }); |
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.
As mentioned, we discourage the use of innerHTML for creating HTML elements.
homework/src/index.js
Outdated
| createEl("th", tr, { text: "Repository:" }); | ||
| let td = createEl("td", tr); | ||
| let a = createEl("a", td, { text: details.name, id: "href", val: details.html_url }); | ||
| a.setAttribute("target", "_blank"); |
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.
The original createAndAppend() function could have set this target attribute.
homework/src/index.js
Outdated
| let tr = createEl("tr", table); | ||
| createEl("th", tr, { text: "Repository:" }); | ||
| let td = createEl("td", tr); | ||
| let a = createEl("a", td, { text: details.name, id: "href", val: details.html_url }); |
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.
Your createEl() function doesn't know how to deal with a val property (what would be its purpose anyway)?
homework/src/index.js
Outdated
| fetchJSON(url, (err, info) => { | ||
| if (err === null) { | ||
| let ul = createEl("ul", parent); | ||
| for (let i = 0; i < info.length; i++) { |
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 not a .forEach()?
homework/src/index.js
Outdated
| @@ -1,47 +1,103 @@ | |||
| 'use strict'; | |||
| "use strick"; | |||
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.
This is a silly typing error to make (and flagged by the spelling checker). Your code is now not using strict mode.
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.
I can see all you have mentioned and I will fix them and come back again! thanks for your comments
|
I'm done with all requirements, and I did my best to make it better and readable. |
remarcmij
left a comment
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.
Hi @kawaraa, you still haven't addressed the important comment to not include all code in one gigantic main() function.
| xhr.responseType = 'json'; | ||
| xhr.onload = () => { | ||
| if (xhr.status < 400) { | ||
| if (xhr.status === 200) { |
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.
This still needs to be fixed.
| if (key === 'text') { | ||
| elem.innerText = value; | ||
| for (let key in options) { | ||
| if (key === "txt") { |
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.
There is really no reason to use cryptic abbreviations. Why not just used text instead of txt or name instead of nm. Professionals write clean code, see for instance this link:
| 'use strict'; | ||
| "use strick"; | ||
|
|
||
| function main(url) { |
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.
This is still not fixed.
Here is my homework for week1