Skip to content

Commit

Permalink
fix category sticky posts not showing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
medmin committed Nov 23, 2019
1 parent 7824982 commit d691a37
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
18 changes: 9 additions & 9 deletions pages/category/[cid].js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ CategoryPage.getInitialProps = async ({ req }) => {
const url = req.originalUrl.split("/")[2];
const category_id = url == "all" ? "" : url.split("-")[0];

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

return { posts, tags };
return { posts: [...allPosts, ...pinnedPosts], tags };
};

export default CategoryPage;
2 changes: 1 addition & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const IndexPage = ({ all, pinned, tags }) => {
*/
IndexPage.getInitialProps = async () => {
let [respPosts, respStickyPosts, respTags] = await Promise.all([
getPosts(),
getPosts({ sticky: false }),
getPosts({ sticky: true }),
getMetas("tags"),
]);
Expand Down
10 changes: 5 additions & 5 deletions src/apis/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ async function getPosts(params = {}) {
url: postsUrl,
method: "get",
params: {
search: keyword ? keyword : "",
page: page ? page : 1,
per_page: per_page ? per_page : 10,
categories: category_id ? category_id : "",
sticky: sticky ? sticky : false,
search: keyword,
page: page || 1,
per_page: per_page || 10,
categories: category_id,
sticky: sticky || false,
},
});
return resp;
Expand Down
5 changes: 1 addition & 4 deletions src/components/posts/PostsContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ const PostsContainer = ({ isLoading }) => {
return <NoPostMessage msg="The author hasn't published any posts!" />;
}

const posts = [...all, ...pinned];
const postsWithoutDuplicates = Array.from(new Set(posts));

return <BaseContainer posts={postsWithoutDuplicates} tags={tags} />;
return <BaseContainer posts={[...all, ...pinned]} tags={tags} />;
};

PostsContainer.propTypes = {
Expand Down

0 comments on commit d691a37

Please sign in to comment.