Skip to content

Commit d691a37

Browse files
committed
fix category sticky posts not showing issue
1 parent 7824982 commit d691a37

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

pages/category/[cid].js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ CategoryPage.getInitialProps = async ({ req }) => {
2222
const url = req.originalUrl.split("/")[2];
2323
const category_id = url == "all" ? "" : url.split("-")[0];
2424

25-
let [respPosts, respTags] = await Promise.all([getPosts({ category_id }), getMetas("tags")]);
26-
let posts = respPosts.status == 200 ? [...respPosts.data] : [];
27-
let tags = {};
28-
if (respTags.status == 200) {
29-
for (let tag of respTags.data) {
30-
tags[tag.id] = tag.slug;
31-
}
32-
}
25+
const [respPosts, respStickyPosts, respTags] = await Promise.all([
26+
getPosts({ category_id, sticky: false }),
27+
getPosts({ category_id, sticky: true }),
28+
getMetas("tags"),
29+
]);
30+
const allPosts = respPosts.status === 200 ? [...respPosts.data] : [];
31+
const pinnedPosts = respStickyPosts.status === 200 ? [...respStickyPosts.data] : [];
32+
const tags = respTags.status === 200 ? Object.fromEntries(respTags.data.map((tag) => [tag.id, tag.slug])) : {};
3333

34-
return { posts, tags };
34+
return { posts: [...allPosts, ...pinnedPosts], tags };
3535
};
3636

3737
export default CategoryPage;

pages/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const IndexPage = ({ all, pinned, tags }) => {
3737
*/
3838
IndexPage.getInitialProps = async () => {
3939
let [respPosts, respStickyPosts, respTags] = await Promise.all([
40-
getPosts(),
40+
getPosts({ sticky: false }),
4141
getPosts({ sticky: true }),
4242
getMetas("tags"),
4343
]);

src/apis/posts.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ async function getPosts(params = {}) {
1818
url: postsUrl,
1919
method: "get",
2020
params: {
21-
search: keyword ? keyword : "",
22-
page: page ? page : 1,
23-
per_page: per_page ? per_page : 10,
24-
categories: category_id ? category_id : "",
25-
sticky: sticky ? sticky : false,
21+
search: keyword,
22+
page: page || 1,
23+
per_page: per_page || 10,
24+
categories: category_id,
25+
sticky: sticky || false,
2626
},
2727
});
2828
return resp;

src/components/posts/PostsContainer.jsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ const PostsContainer = ({ isLoading }) => {
1717
return <NoPostMessage msg="The author hasn't published any posts!" />;
1818
}
1919

20-
const posts = [...all, ...pinned];
21-
const postsWithoutDuplicates = Array.from(new Set(posts));
22-
23-
return <BaseContainer posts={postsWithoutDuplicates} tags={tags} />;
20+
return <BaseContainer posts={[...all, ...pinned]} tags={tags} />;
2421
};
2522

2623
PostsContainer.propTypes = {

0 commit comments

Comments
 (0)