Skip to content

Commit

Permalink
cleaning up code
Browse files Browse the repository at this point in the history
  • Loading branch information
0b01 committed May 30, 2017
1 parent f64fae6 commit 8df0954
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 37 deletions.
3 changes: 0 additions & 3 deletions .eslintrc
Expand Up @@ -27,7 +27,6 @@
"sleep": 1
},
"rules": {
"react/jsx-no-bind": [2, { "ignoreRefs": true }],
"react/jsx-no-duplicate-props": 2,
"react/self-closing-comp": 2,
"react/prefer-es6-class": 2,
Expand All @@ -51,8 +50,6 @@
"camelcase": 0,
"comma-style": 2,
"comma-dangle": [2, "never"],
"indent": [2, "tab", {"SwitchCase": 1}],
"no-mixed-spaces-and-tabs": [2, "smart-tabs"],
"no-trailing-spaces": [2, { "skipBlankLines": true }],
"max-nested-callbacks": [2, 3],
"no-eval": 2,
Expand Down
33 changes: 12 additions & 21 deletions src/components/app.js
Expand Up @@ -3,40 +3,31 @@ import { h, Component } from 'preact';
import Header from './header';
import Home from './home';

const DAMPENED_COLOR = "#dbdbbd";
const DEFAULT_COLOR = "#fb651e";

function fetchStory(url) {
return fetch("http://hn.algolia.com/api/v1/search?query=" + url)
.then((res) => {
if (res.status >= 200 && res.status < 300) {
return Promise.resolve(res)
} else {
return Promise.reject(new Error(res.statusText))
}
if (res.status >= 200 && res.status < 300)
return Promise.resolve(res);
return Promise.reject(new Error(res.statusText));
})
.then((res) => res.json())
.then((data) => Promise.resolve({size: data.nbHits, results: data.hits}))
.then((data) => Promise.resolve({size: data.nbHits, results: data.hits}));
}

function getTopMatchID(data) {
if (data.size === 0) {
if (data.size === 0)
return Promise.reject(new Error("not posted on HN"));
} else {
return Promise.resolve(data.results[0].objectID);
}
return Promise.resolve(data.results[0].objectID);
}

function fetchCommentsByID(id) {
return fetch("http://hn.algolia.com/api/v1/items/" + id)
.then((res) => {
if (res.status >= 200 && res.status < 300) {
return Promise.resolve(res)
} else {
return Promise.reject(new Error(res.statusText))
}
if (res.status >= 200 && res.status < 300)
return Promise.resolve(res);
return Promise.reject(new Error(res.statusText));
})
.then((res) => res.json())
.then((res) => res.json());
}

export default class App extends Component {
Expand All @@ -45,14 +36,14 @@ export default class App extends Component {
this.state = {
loading: true,
comments: []
}
};
if (this.props.id) {
fetchCommentsByID(this.props.id)
.then((comments) => {
this.setState({loading: false, comments});
});
} else {
var url = document.URL;
const url = document.URL;
// url = "http://danwang.co/why-so-few-computer-science-majors?idk"
fetchStory(url)
.then(getTopMatchID)
Expand Down
1 change: 0 additions & 1 deletion src/components/header/index.js
@@ -1,5 +1,4 @@
import { h, Component } from 'preact';
import { Link } from 'preact-router';
import style from './style.less';

const HNLogo = "data:image/svg+xml;base64,PHN2ZyBpZD0naG4tbG9nbycgdmlld0JveD0iMCAwIDI1NiAyNTYiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgcHJlc2VydmVBc3BlY3RSYXRpbz0ieE1pbllNaW4gbWVldCI+PGc+PHJlY3QgZmlsbD0iI0ZCNjUxRSIgeD0iMCIgeT0iMCIgd2lkdGg9IjI1NiIgaGVpZ2h0PSIyNTYiPjwvcmVjdD48cGF0aCBkPSJNMTE5LjM3MzY1MywxNDQuNzQ1ODEzIEw3NS40MzI5Niw2Mi40MzE1NzMzIEw5NS41MTQ0NTMzLDYyLjQzMTU3MzMgTDEyMS4zNjE5MiwxMTQuNTI0MTYgQzEyMS43NTk1NzUsMTE1LjQ1MjAyMiAxMjIuMjIzNSwxMTYuNDEzMDA4IDEyMi43NTM3MDcsMTE3LjQwNzE0NyBDMTIzLjI4MzkxNCwxMTguNDAxMjg1IDEyMy43NDc4MzgsMTE5LjQyODU0NiAxMjQuMTQ1NDkzLDEyMC40ODg5NiBDMTI0LjQxMDU5NywxMjAuODg2NjE1IDEyNC42MDk0MjIsMTIxLjI1MTEyNyAxMjQuNzQxOTczLDEyMS41ODI1MDcgQzEyNC44NzQ1MjUsMTIxLjkxMzg4NiAxMjUuMDA3MDc1LDEyMi4yMTIxMjMgMTI1LjEzOTYyNywxMjIuNDc3MjI3IEMxMjUuODAyMzg2LDEyMy44MDI3NDQgMTI2LjM5ODg2LDEyNS4wOTUxMDUgMTI2LjkyOTA2NywxMjYuMzU0MzQ3IEMxMjcuNDU5Mjc0LDEyNy42MTM1ODkgMTI3LjkyMzE5OCwxMjguNzczMzk5IDEyOC4zMjA4NTMsMTI5LjgzMzgxMyBDMTI5LjM4MTI2OCwxMjcuNTgwNDMzIDEzMC41NDEwNzgsMTI1LjE2MTQgMTMxLjgwMDMyLDEyMi41NzY2NCBDMTMzLjA1OTU2MiwxMTkuOTkxODggMTM0LjM1MTkyMiwxMTcuMzA3NzQ3IDEzNS42Nzc0NCwxMTQuNTI0MTYgTDE2MS45MjI1Niw2Mi40MzE1NzMzIEwxODAuNjEyMjY3LDYyLjQzMTU3MzMgTDEzNi4yNzM5MiwxNDUuNzM5OTQ3IEwxMzYuMjczOTIsMTk4LjgyNjY2NyBMMTE5LjM3MzY1MywxOTguODI2NjY3IEwxMTkuMzczNjUzLDE0NC43NDU4MTMgWiIgZmlsbD0iI0ZGRkZGRiI+PC9wYXRoPjwvZz48L3N2Zz4=";
Expand Down
21 changes: 10 additions & 11 deletions src/components/home/comments/index.js
Expand Up @@ -7,10 +7,10 @@ function timeSince(date) {
date = new Date(date);
}

var seconds = Math.floor(((new Date().getTime()/1000) - date))
var intervalType;
const seconds = Math.floor(((new Date().getTime()/1000) - date));
let intervalType;

var interval = Math.floor(seconds / 31536000);
let interval = Math.floor(seconds / 31536000);
if (interval >= 1) {
intervalType = 'year';
} else {
Expand Down Expand Up @@ -43,7 +43,7 @@ function timeSince(date) {
}

return interval + ' ' + intervalType + ' ago';
};
}



Expand All @@ -56,7 +56,7 @@ class Child extends Component {
super();
this.state = {
hidden: false
}
};
}

toggleHide() {
Expand All @@ -65,9 +65,9 @@ class Child extends Component {

renderBody(comment) {
if (this.state.hidden) return null;
else return (
return (
<div>
<div className={style.body} dangerouslySetInnerHTML={{__html: comment.text}}></div>
<div className={style.body} dangerouslySetInnerHTML={{__html: comment.text}} />
{renderChildren(comment.children)}
</div>
);
Expand Down Expand Up @@ -101,9 +101,8 @@ class Child extends Component {
{this.renderBody(comment)}
</div>
);
} else {
return null;
}
return null;
}
}

Expand All @@ -119,7 +118,7 @@ const renderChildren = (children, topLevel=false) => {
{sortedChildren.map((c) => <Child comment={c} topLevel={topLevel} />)}
</div>
);
}
};



Expand All @@ -139,7 +138,7 @@ export default class Comments extends Component {
<div>
{renderChildren(children, true)}
</div>
)
);
}
}
}
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -8,7 +8,7 @@ function init() {
let App = require('./components/app').default;

const element = document.querySelector('meta[property="hacker-news"]');
var hnID = undefined;
let hnID;
if (element) {
hnID = element.getAttribute("content");
}
Expand Down

0 comments on commit 8df0954

Please sign in to comment.