Skip to content

Commit 5a7a280

Browse files
committed
fix sidebar pinned posts
1 parent 75cd42b commit 5a7a280

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

pages/category/[cid].js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { getPosts } from "src/apis/posts";
55
import { getMetas } from "src/apis/metas";
66

77
/** hide_empty == true, so, a category mush have at least one post */
8-
const CategoryPage = ({ posts, tags }) => {
8+
const CategoryPage = ({ allPosts, pinnedPosts, tags }) => {
99
return (
1010
<div className="tile is-parent is-flex-widescreen">
1111
<div className="tile is-3">
12-
<SideBar />
12+
<SideBar posts={{ pinned: pinnedPosts }} page="category" />
1313
</div>
1414
<div className="tile is-9">
15-
<BaseContainer posts={posts} tags={tags} />
15+
<BaseContainer posts={[...allPosts, ...pinnedPosts]} tags={tags} />
1616
</div>
1717
</div>
1818
);
@@ -24,14 +24,14 @@ CategoryPage.getInitialProps = async ({ req }) => {
2424

2525
const [respPosts, respStickyPosts, respTags] = await Promise.all([
2626
getPosts({ category_id, sticky: false }),
27-
getPosts({ category_id, sticky: true }),
27+
getPosts({ category_id, sticky: true }), // sticky posts in this category
2828
getMetas("tags"),
2929
]);
3030
const allPosts = respPosts.status === 200 ? [...respPosts.data] : [];
3131
const pinnedPosts = respStickyPosts.status === 200 ? [...respStickyPosts.data] : [];
3232
const tags = respTags.status === 200 ? Object.fromEntries(respTags.data.map((tag) => [tag.id, tag.slug])) : {};
3333

34-
return { posts: [...allPosts, ...pinnedPosts], tags };
34+
return { 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
@@ -20,7 +20,7 @@ const IndexPage = ({ all, pinned, tags }) => {
2020
return (
2121
<div className="tile is-parent is-flex-widescreen is-x-paddingless-desktop">
2222
<div className="tile is-3">
23-
<SideBar />
23+
<SideBar page="index" posts={{ pinned }} />
2424
</div>
2525
<div className="tile is-9">
2626
<PostsContainer isLoading={isLoading} />

pages/search.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from "react";
22
import SideBar from "src/components/common/SideBar";
33
import SearchContainer from "src/components/posts/SearchContainer";
4+
import { getPosts } from "src/apis/posts";
45

5-
const SearchResultPage = () => {
6+
const SearchResultPage = ({ pinnedPosts }) => {
67
return (
78
<div className="tile is-parent is-flex-widescreen">
89
<div className="tile is-3">
9-
<SideBar />
10+
<SideBar posts={{ pinned: pinnedPosts }} page="search" />
1011
</div>
1112
<div className="tile is-9">
1213
<SearchContainer />
@@ -15,4 +16,10 @@ const SearchResultPage = () => {
1516
);
1617
};
1718

19+
SearchResultPage.getInitialProps = async () => {
20+
const respStickyPosts = await getPosts({ sticky: true });
21+
const pinnedPosts = respStickyPosts.status === 200 ? [...respStickyPosts.data] : [];
22+
return { pinnedPosts };
23+
};
24+
1825
export default SearchResultPage;

src/components/common/SideBar.jsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import React, { useEffect, useState, useContext } from "react";
2-
import PostContext from "src/contexts/PostContext";
1+
import React, { useEffect, useState } from "react";
2+
import PropTypes from "prop-types";
33
import { getMetas } from "src/apis/metas";
44
import { useRouter } from "next/router";
55

6-
const SideBar = () => {
6+
const SideBar = ({ page, posts: { pinned } }) => {
77
const router = useRouter();
8-
const { postState } = useContext(PostContext);
98

109
const [categories, setCategories] = useState([]);
1110
const initCategories = async () => {
@@ -48,8 +47,8 @@ const SideBar = () => {
4847
</ul>
4948
<p className="menu-label is-hidden-mobile">Pinned Posts</p>
5049
<ul className="menu-list is-hidden-mobile">
51-
{postState.pinned.length ? (
52-
postState.pinned.map((post) => {
50+
{pinned.length ? (
51+
pinned.map((post) => {
5352
return (
5453
<li key={post.id} onClick={() => handlePostOnClick(post)}>
5554
<a>{post.title.rendered}</a>
@@ -58,12 +57,17 @@ const SideBar = () => {
5857
})
5958
) : (
6059
<li>
61-
<a>No Pinned Posts Yet</a>
60+
<a>No pinned posts{page === "category" ? " in this category" : ""}</a>
6261
</li>
6362
)}
6463
</ul>
6564
</aside>
6665
);
6766
};
6867

68+
SideBar.propTypes = {
69+
page: PropTypes.string.isRequired,
70+
posts: PropTypes.object.isRequired,
71+
};
72+
6973
export default SideBar;

0 commit comments

Comments
 (0)