Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Conversation

@kawaraa
Copy link

@kawaraa kawaraa commented Oct 14, 2018

Here is my homework for week1

Finishes homework week1
Adds styles.
remarcmij
remarcmij previously approved these changes Oct 23, 2018
Copy link
Contributor

@remarcmij remarcmij left a 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) {
Copy link
Contributor

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.

Copy link
Contributor

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.

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" });
Copy link
Contributor

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?

};
xhr.onerror = () => cb(new Error('Network request failed'));
xhr.onerror = () => {
cb({ text: "Network request failed", id: "id", val: "error" });
Copy link
Contributor

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).

}

function createAndAppend(name, parent, options = {}) {
function createEl(name, parent, obj) {
Copy link
Contributor

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.

const header = createEl("header", root);
createEl("h3", header, { text: "HYF Repositories" });
const select = createEl("select", header);
let names = [];
Copy link
Contributor

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.

}
});
if (obj) {
if (obj.text) { elem.innerHTML = obj.text; }
Copy link
Contributor

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.

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>` });
Copy link
Contributor

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.

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");
Copy link
Contributor

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.

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 });
Copy link
Contributor

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)?

fetchJSON(url, (err, info) => {
if (err === null) {
let ul = createEl("ul", parent);
for (let i = 0; i < info.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a .forEach()?

@@ -1,47 +1,103 @@
'use strict';
"use strick";
Copy link
Contributor

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.

Copy link
Author

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

@kawaraa
Copy link
Author

kawaraa commented Nov 9, 2018

I'm done with all requirements, and I did my best to make it better and readable.
Again thanks for your time our teacher!

Copy link
Contributor

@remarcmij remarcmij left a 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) {
Copy link
Contributor

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") {
Copy link
Contributor

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:

https://medium.com/mindorks/how-to-write-clean-code-lessons-learnt-from-the-clean-code-robert-c-martin-9ffc7aef870c

'use strict';
"use strick";

function main(url) {
Copy link
Contributor

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.

@remarcmij remarcmij closed this Nov 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants