Skip to content

Commit 9a264a9

Browse files
committed
adding single blog post view
1 parent 191e053 commit 9a264a9

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://blog.reactjsfoundations.com/wp-json/wp/v2/posts/50

reactjsfoundations.com/src/WpBlog.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { QueryClient, QueryClientProvider, useQuery } from 'react-query';
22
import { useState } from 'react';
33
import * as styles from './WpBlog.module.css';
4+
import { useParams } from 'react-router-dom';
45

56
const queryClient = new QueryClient();
67

@@ -19,13 +20,19 @@ export default function WpBlog(props) {
1920
function ReactBlog(props) {
2021
const [totalPages, setTotalPages] = useState(0);
2122
const [page, setPage] = useState(1);
23+
let { id } = useParams();
2224

25+
let url;
26+
27+
if (id) {
28+
url = `https://blog.reactjsfoundations.com/wp-json/wp/v2/posts/${id}/?per_page=${props.posts}&page=${page}`;
29+
} else {
30+
url = `https://blog.reactjsfoundations.com/wp-json/wp/v2/posts?per_page=${props.posts}&page=${page}`;
31+
}
2332
const { isLoading, error, data } = useQuery(
2433
['posts', page],
2534
() => {
26-
return fetch(
27-
`https://blog.reactjsfoundations.com/wp-json/wp/v2/posts?per_page=${props.posts}&page=${page}`
28-
).then((res) => {
35+
return fetch(url).then((res) => {
2936
setTotalPages(parseInt(res.headers.get('x-wp-totalpages')));
3037
return res.json();
3138
});
@@ -66,6 +73,8 @@ function ReactBlog(props) {
6673
className={styles.post__content}
6774
dangerouslySetInnerHTML={{ __html: post.content.rendered }}
6875
/>
76+
77+
<hr />
6978
</div>
7079
))}
7180
</div>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { QueryClient, QueryClientProvider, useQuery } from 'react-query';
2+
import { useState, useEffect } from 'react';
3+
import * as styles from './WpBlog.module.css';
4+
import { useParams } from 'react-router-dom';
5+
6+
export default function ReactBlog(props) {
7+
let { id } = useParams();
8+
const [post, setPost] = useState();
9+
useEffect(() => {
10+
if (id) {
11+
const getPost = async () => {
12+
const response = await fetch(
13+
`https://blog.reactjsfoundations.com/wp-json/wp/v2/posts/${id}/`
14+
);
15+
const data = await response.json();
16+
console.log(data);
17+
setPost(data);
18+
};
19+
getPost();
20+
}
21+
}, [id]);
22+
23+
if (post) {
24+
return (
25+
<div className={styles.blog__posts}>
26+
<h1 className={styles.blog__title}>{post.title.rendered}</h1>
27+
<div key={post.id}>
28+
<section
29+
className={styles.post__content}
30+
dangerouslySetInnerHTML={{ __html: post.content.rendered }}
31+
/>
32+
</div>
33+
</div>
34+
);
35+
}
36+
return 'Loading...';
37+
}

reactjsfoundations.com/src/WpBlogWidget.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { QueryClient, QueryClientProvider, useQuery } from 'react-query';
22
import { useState } from 'react';
33
import * as styles from './WpBlog.module.css';
4+
import { Link } from 'react-router-dom';
45

56
const queryClient = new QueryClient();
67

@@ -56,16 +57,21 @@ function ReactBlog(props) {
5657
{data.map((post) => (
5758
<div key={post.id}>
5859
<h2>
59-
<section
60-
className={styles.post__title}
61-
dangerouslySetInnerHTML={{ __html: post.title.rendered }}
62-
/>
60+
<Link to={`/blog/${post.id}`}>
61+
<section
62+
className={styles.post__title}
63+
dangerouslySetInnerHTML={{ __html: post.title.rendered }}
64+
/>
65+
</Link>
6366
</h2>
6467

6568
<section
6669
className={styles.post__content}
67-
dangerouslySetInnerHTML={{ __html: post.content.rendered }}
70+
dangerouslySetInnerHTML={{ __html: post.excerpt.rendered }}
6871
/>
72+
<p>
73+
<Link to={`/blog/${post.id}`}>Read More &gt;</Link>
74+
</p>
6975
</div>
7076
))}
7177
</div>

reactjsfoundations.com/src/routes.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ErrorBoundary from './chapter13/ErrorBoundary';
88
import SyntaxHighlighter from 'react-syntax-highlighter';
99
import { github } from 'react-syntax-highlighter/dist/esm/styles/hljs';
1010
import WpBlog from './WpBlog';
11+
import WpBlogSP from './WpBlogSP';
1112
const Introduction = lazy(() => import('./Introduction'));
1213
const WhereToBuy = lazy(() => import('./WhereToBuy'));
1314
const About = lazy(() => import('./About'));
@@ -34,9 +35,12 @@ export const routes = (
3435
<Route path="/intro" component={Introduction} />
3536
<Route path="/WhereToBuy" component={WhereToBuy} />
3637
<Route path="/AboutChrisMinnick" component={About} />
37-
<Route path="/blog">
38+
<Route exact path="/blog/">
3839
<WpBlog posts={10} title="ReactJS Foundations Blog and Errata" />
3940
</Route>
41+
<Route exact path="/blog/:id">
42+
<WpBlogSP title="ReactJS Foundations Blog and Errata" />
43+
</Route>
4044
<Route path="/listing102">
4145
<Helmet>
4246
<title>

0 commit comments

Comments
 (0)