Skip to content

Commit 676641d

Browse files
committed
final-state
1 parent 3d7fa3d commit 676641d

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

src/api.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export function getPostById({ id }) {
1111
return wrapPromise(get(url));
1212
}
1313

14+
export function getCommentsByPostId({ id }) {
15+
const url = new URL(`/api/comments/${id}`, urlBase);
16+
return wrapPromise(get(url));
17+
}
18+
1419
async function get(url) {
1520
const response = await fetch(url.toString(), {
1621
contentType: "application/json"

src/pages/Detail.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
import React, { Suspense } from "react";
1+
import React, { Suspense, SuspenseList } from "react";
22
import { Stack, Heading } from "@chakra-ui/core";
33

44
import { PageWrapper } from "../components/PageWrapper";
55
import { PostList, PostListPH } from "../components/PostList";
66
import { PostDetail } from "../components/PostDetail";
7+
import { CommentsList, CommentsListPH } from "../components/CommentsList";
78

8-
export default function Detail({ post, relatedPosts }) {
9+
export default function Detail({ post, relatedPosts, comments }) {
910
const postData = post.read();
1011

1112
return (
1213
<PageWrapper>
1314
<Stack spacing={4}>
1415
<PostDetail post={postData} />
15-
<Suspense fallback={<RelatedPostsPH />}>
16-
<RelatedPosts posts={relatedPosts} />
17-
</Suspense>
16+
<SuspenseList revealOrder="forwards">
17+
<Suspense fallback={<CommentsPH />}>
18+
<Comments comments={comments} />
19+
</Suspense>
20+
<Suspense fallback={<RelatedPostsPH />}>
21+
<RelatedPosts posts={relatedPosts} />
22+
</Suspense>
23+
</SuspenseList>
1824
</Stack>
1925
</PageWrapper>
2026
);
@@ -48,3 +54,33 @@ const RelatedPostsWrapper = ({ children, props }) => (
4854
{children}
4955
</Stack>
5056
);
57+
58+
// Comments
59+
const Comments = ({ comments }) => {
60+
const commentsData = comments.read();
61+
return (
62+
<CommentsWrapper>
63+
<CommentsList comments={commentsData} />
64+
</CommentsWrapper>
65+
);
66+
};
67+
68+
const CommentsPH = () => (
69+
<CommentsWrapper>
70+
<CommentsListPH placeholders={3} />
71+
</CommentsWrapper>
72+
);
73+
74+
const CommentsWrapper = ({ children, props }) => (
75+
<Stack spacing={8} mb={8}>
76+
<Heading
77+
size="md"
78+
pb={4}
79+
borderBottomWidth="1px"
80+
borderBottomColor="gray.200"
81+
>
82+
Comments
83+
</Heading>
84+
{children}
85+
</Stack>
86+
);

src/router.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createRouter } from "./lib/createRouter";
2-
import { getPosts, getPostById } from "./api";
2+
import { getPosts, getPostById, getCommentsByPostId } from "./api";
33

44
// Routes
55
const routes = [
@@ -8,7 +8,8 @@ const routes = [
88
loadCode: () => import("./pages/Detail"),
99
loadData: ({ postId }) => ({
1010
post: getPostById({ id: postId }),
11-
relatedPosts: getPosts({ limit: 3, relatedTo: postId })
11+
relatedPosts: getPosts({ limit: 3, relatedTo: postId }),
12+
comments: getCommentsByPostId({ id: postId })
1213
})
1314
},
1415
{

0 commit comments

Comments
 (0)