Skip to content

Commit f19f0b3

Browse files
initial application logic
0 parents  commit f19f0b3

File tree

9 files changed

+3497
-0
lines changed

9 files changed

+3497
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.next

components/navigation.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Link } from "../routes";
2+
3+
export default () => (
4+
<div>
5+
<Link prefetch route="home"><a>HN</a></Link> |{" "}
6+
<Link prefetch route="index" params={{ type: "new" }}><a>New</a></Link>
7+
{" "}
8+
|
9+
{" "}
10+
<Link prefetch route="index" params={{ type: "show" }}><a>Show</a></Link>
11+
{" "}
12+
|
13+
{" "}
14+
<Link prefetch route="index" params={{ type: "ask" }}><a>Ask</a></Link>
15+
{" "}
16+
|
17+
{" "}
18+
<Link prefetch route="index" params={{ type: "jobs" }}><a>Jobs</a></Link>
19+
</div>
20+
);

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { createServer } = require("http");
2+
const next = require("next");
3+
const routes = require("./routes");
4+
5+
const app = next({ dev: process.env.NODE_ENV !== "production" });
6+
const handler = routes.getRequestHandler(app);
7+
8+
app.prepare().then(() => {
9+
createServer(handler).listen(3000);
10+
});

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "nextjs-hn",
3+
"version": "0.1.0",
4+
"description": "Hacker News Progressive Web App using Next.js",
5+
"author": "Chris Draycott-Wheatley",
6+
"license": "MIT",
7+
"scripts": {
8+
"dev": "node index.js",
9+
"build": "NODE_ENV=production next build",
10+
"start": "NODE_ENV=production node index.js"
11+
},
12+
"dependencies": {
13+
"isomorphic-fetch": "^2.2.1",
14+
"next": "^2.0.1",
15+
"next-routes": "^1.0.24",
16+
"react": "^15.4.2",
17+
"react-dom": "^15.4.2"
18+
},
19+
"devDependencies": {
20+
"prettier": "^0.22.0"
21+
}
22+
}

pages/comments.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import fetch from "isomorphic-fetch";
2+
import Link from "next/link";
3+
import Navigation from "../components/navigation";
4+
5+
function commentThread(comments) {
6+
const thread = [];
7+
8+
comments.map(item => {
9+
thread.push(
10+
<div key={item.id} style={{ marginLeft: `${item.level}0px` }}>
11+
<li>
12+
{item.user} {item.time_ago}
13+
<span dangerouslySetInnerHTML={{ __html: item.content }} />
14+
</li>
15+
</div>
16+
);
17+
18+
if (item.comments && item.comments.length) {
19+
thread.push(commentThread(item.comments));
20+
}
21+
});
22+
23+
return thread;
24+
}
25+
26+
const Comments = ({ data }) => {
27+
const {
28+
comments_count,
29+
id,
30+
points,
31+
time_ago,
32+
title,
33+
user
34+
} = data;
35+
return (
36+
<div>
37+
<Navigation />
38+
<div>
39+
{data.title}
40+
{" "}
41+
<span>
42+
{points}
43+
{" "}
44+
points by
45+
{" "}
46+
<Link route="user" params={{ name: user }}><a>{user}</a></Link>
47+
{" "}
48+
{time_ago}
49+
{" "}
50+
|
51+
{" "}
52+
{comments_count} comments
53+
</span> <ul>{commentThread(data.comments)}</ul>
54+
</div>
55+
</div>
56+
);
57+
};
58+
59+
Comments.getInitialProps = async ({ query: { id } }) => {
60+
const res = await fetch(`https://node-hnapi.herokuapp.com/item/${id}`);
61+
const json = await res.json();
62+
return { data: json };
63+
};
64+
65+
export default Comments;

pages/index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import fetch from "isomorphic-fetch";
2+
import { Link } from "../routes";
3+
import Navigation from "../components/navigation";
4+
5+
const Listing = ({ data }) => (
6+
<div>
7+
<Navigation />
8+
<ul>
9+
{data.map(({
10+
comments_count,
11+
id,
12+
points,
13+
time_ago,
14+
title,
15+
url,
16+
user
17+
}, index) => (
18+
<li key={id}>
19+
{index + 1}
20+
{" "}
21+
<a href={url}>{title}</a>
22+
{" "}
23+
<span>
24+
{user &&
25+
<span>
26+
{points}
27+
{" "}
28+
points by
29+
{" "}
30+
<Link route="user" params={{ name: user }}><a>{user}</a></Link>
31+
</span>}
32+
{" "}
33+
{time_ago}
34+
{user &&
35+
<span>
36+
{" "}|{" "}
37+
<Link prefetch route="comments" params={{ id }}>
38+
<a>{comments_count} comments</a>
39+
</Link>
40+
</span>}
41+
</span>
42+
</li>
43+
))}
44+
</ul>
45+
</div>
46+
);
47+
48+
Listing.getInitialProps = async ({ query: { type } }) => {
49+
const allowed = ["news", "new", "show", "ask", "jobs"];
50+
if (!type || !allowed.includes(type)) {
51+
type = "news";
52+
}
53+
if (type === "new") {
54+
type = "newest";
55+
}
56+
57+
const res = await fetch(`https://node-hnapi.herokuapp.com/${type}`);
58+
const json = await res.json();
59+
return { data: json };
60+
};
61+
62+
export default Listing;

pages/user.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import fetch from "isomorphic-fetch";
2+
import Link from "next/link";
3+
import Navigation from "../components/navigation";
4+
5+
const User = ({ data: { about, created, id, karma } }) => (
6+
<div>
7+
<Navigation />
8+
<div>user: {id}</div>
9+
<div>created: {created}</div>
10+
<div>karma: {karma}</div>
11+
<div>about: <span dangerouslySetInnerHTML={{ __html: about }} /></div>
12+
</div>
13+
);
14+
15+
User.getInitialProps = async ({ query: { name } }) => {
16+
const res = await fetch(`https://node-hnapi.herokuapp.com/user/${name}`);
17+
const json = await res.json();
18+
if (json.error) {
19+
throw new Error(json.error);
20+
}
21+
return { data: json };
22+
};
23+
24+
export default User;

routes.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const nextRoutes = require("next-routes");
2+
3+
const routes = (module.exports = nextRoutes());
4+
5+
routes.add("home", "/", "index");
6+
routes.add("index", "/:type");
7+
routes.add("comments", "/item/:id");
8+
routes.add("user", "/user/:name");

0 commit comments

Comments
 (0)