Skip to content

Commit bf433bc

Browse files
move item meta data out into component
1 parent f19f0b3 commit bf433bc

File tree

4 files changed

+33
-54
lines changed

4 files changed

+33
-54
lines changed

components/item-meta.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Link } from "../routes";
2+
3+
export default ({ url, title, user, points, time_ago, id, comments_count }) => (
4+
<span>
5+
<a href={url}>{title}</a>
6+
{" "}
7+
{user &&
8+
<span>
9+
{points}
10+
{" "}
11+
points by
12+
{" "}
13+
<Link route="user" params={{ name: user }}><a>{user}</a></Link>
14+
</span>}
15+
{" "}
16+
{time_ago}
17+
{user &&
18+
<span>
19+
{" "}|{" "}
20+
<Link prefetch route="comments" params={{ id }}>
21+
<a>{comments_count} comments</a>
22+
</Link>
23+
</span>}
24+
</span>
25+
);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"author": "Chris Draycott-Wheatley",
66
"license": "MIT",
77
"scripts": {
8+
"lint": "prettier pages/*.js components/*.js *.js --write",
89
"dev": "node index.js",
910
"build": "NODE_ENV=production next build",
1011
"start": "NODE_ENV=production node index.js"

pages/comments.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fetch from "isomorphic-fetch";
22
import Link from "next/link";
33
import Navigation from "../components/navigation";
4+
import ItemMeta from "../components/item-meta";
45

56
function commentThread(comments) {
67
const thread = [];
@@ -24,33 +25,12 @@ function commentThread(comments) {
2425
}
2526

2627
const Comments = ({ data }) => {
27-
const {
28-
comments_count,
29-
id,
30-
points,
31-
time_ago,
32-
title,
33-
user
34-
} = data;
3528
return (
3629
<div>
3730
<Navigation />
3831
<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>
32+
<ItemMeta {...data} />
33+
<ul>{commentThread(data.comments)}</ul>
5434
</div>
5535
</div>
5636
);

pages/index.js

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,17 @@
11
import fetch from "isomorphic-fetch";
22
import { Link } from "../routes";
33
import Navigation from "../components/navigation";
4+
import ItemMeta from "../components/item-meta";
45

56
const Listing = ({ data }) => (
67
<div>
78
<Navigation />
89
<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}>
10+
{data.map((item, index) => (
11+
<li key={item.id}>
1912
{index + 1}
2013
{" "}
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>
14+
<ItemMeta {...item} />
4215
</li>
4316
))}
4417
</ul>

0 commit comments

Comments
 (0)