Skip to content

Commit 65a9006

Browse files
committed
fix pinned posts not showing problem
1 parent fea0fc9 commit 65a9006

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

pages/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@ const IndexPage = ({ all, pinned, tags }) => {
3636
* https://github.com/zeit/next.js/issues/6115
3737
*/
3838
IndexPage.getInitialProps = async () => {
39-
let [respPosts, respTags] = await Promise.all([getPosts(), getMetas("tags")]);
40-
let ok = respPosts.status == 200;
41-
let all = ok ? [...respPosts.data] : [];
42-
let pinned = ok ? respPosts.data.filter((post) => post.sticky) : [];
39+
let [respPosts, respStickyPosts, respTags] = await Promise.all([
40+
getPosts(),
41+
getPosts({ sticky: true }),
42+
getMetas("tags"),
43+
]);
44+
let all = respPosts.status == 200 ? [...respPosts.data] : [];
45+
let pinned = respStickyPosts.status == 200 ? [...respStickyPosts.data] : [];
4346

4447
let tags = {};
45-
if (respTags.status == 200) {
48+
const tagsOK = respTags.status == 200;
49+
// console.log(Object.values(respTags.data));
50+
if (tagsOK) {
4651
for (let tag of respTags.data) {
4752
tags[tag.id] = tag.slug;
4853
}

src/apis/posts.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const postsUrl = `${BASE_URL}/wp-json/wp/v2/posts`;
1111
*/
1212

1313
async function getPosts(params = {}) {
14-
const { keyword, page, per_page, category_id } = params;
14+
const { keyword, page, per_page, category_id, sticky } = params;
1515

1616
try {
1717
let resp = await axios.request({
@@ -22,6 +22,7 @@ async function getPosts(params = {}) {
2222
page: page ? page : 1,
2323
per_page: per_page ? per_page : 10,
2424
categories: category_id ? category_id : "",
25+
sticky: sticky ? sticky : false,
2526
},
2627
});
2728
return resp;

src/components/posts/BaseContainer.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ const BaseContainer = (props) => {
1515
});
1616
};
1717

18-
const pinnedPosts = posts.filter((post) => post.sticky);
19-
const regularPosts = posts.filter((post) => !post.sticky);
18+
// pinned and all, they may contain duplicated posts
19+
// i.e., in the most recent 10 posts, one of them may be sticky/pinned posts
20+
const pinnedPosts = posts.pinned;
21+
const regularPosts = posts.all.filter((post) => !post.sticky);
2022
const sortedPosts = [...pinnedPosts, ...regularPosts];
2123

2224
return (
@@ -64,7 +66,7 @@ const BaseContainer = (props) => {
6466
};
6567

6668
BaseContainer.propTypes = {
67-
posts: PropTypes.array.isRequired,
69+
posts: PropTypes.object.isRequired,
6870
tags: PropTypes.object.isRequired,
6971
};
7072

src/components/posts/PostsContainer.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ const PostsContainer = ({ isLoading }) => {
1010
}
1111

1212
const {
13-
postState: { all, tags },
13+
postState: { all, pinned, tags },
1414
} = useContext(PostContext);
1515
// console.log(tags);
1616
if (all.length === 0) {
1717
return <NoPostMessage msg="The author hasn't published any posts!" />;
1818
}
1919

20-
return <BaseContainer posts={all} tags={tags} />;
20+
return <BaseContainer posts={{ all, pinned }} tags={tags} />;
2121
};
2222

2323
PostsContainer.propTypes = {

0 commit comments

Comments
 (0)