Skip to content

Commit 857b12e

Browse files
committed
Added load more button in /blog endpoint, as it was asked in Design Audit
1 parent 9687130 commit 857b12e

File tree

4 files changed

+51
-34
lines changed

4 files changed

+51
-34
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
406406
</tr>
407407
<tr>
408408
<td align="center" valign="top" width="14.28%"><a href="https://github.com/raj17ce"><img src="https://avatars.githubusercontent.com/u/116947399?v=4?s=100" width="100px;" alt="Raj Patel"/><br /><sub><b>Raj Patel</b></sub></a><br /><a href="https://github.com/asyncapi/website/commits?author=raj17ce" title="Code">💻</a></td>
409+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/nalindalal"><img src="https://avatars.githubusercontent.com/u/116961144?v=4&size=64" width="100px;" alt="Nalin Dalal"/><br /><sub><b>Nalin Dalal</b></sub></a><br /><a href="https://github.com/asyncapi/website/commits?author=nalindalal" title="Code">💻</a></td>
409410
</tr>
410411
</tbody>
411412
</table>

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
"node-fetch": "^3.3.2",
9393
"node-fetch-2": "npm:node-fetch@^2.7.0",
9494
"postcss": "^8.4.35",
95-
"prettier": "^3.3.3",
9695
"react": "^18",
9796
"react-dom": "^18",
9897
"react-ga": "^3.3.1",
@@ -156,6 +155,7 @@
156155
"inquirer": "^9.2.14",
157156
"jest": "^29.7.0",
158157
"postcss-import": "^16.0.1",
158+
"prettier": "^3.5.2",
159159
"remark-cli": "^12.0.1",
160160
"remark-lint": "^10.0.0",
161161
"remark-mdx": "^3.0.1",

pages/blog/index.tsx

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,52 @@ export default function BlogIndexPage() {
2121
const router = useRouter();
2222
const { navItems } = useContext(BlogContext);
2323

24-
const [posts, setPosts] = useState<IBlogPost[]>(
25-
navItems
26-
? navItems.sort((i1: IBlogPost, i2: IBlogPost) => {
27-
const i1Date = new Date(i1.date);
28-
const i2Date = new Date(i2.date);
24+
const [posts, setPosts] = useState<IBlogPost[]>([]);
25+
const [isClient, setIsClient] = useState(false);
26+
const [visiblePosts, setVisiblePosts] = useState(9); // Number of posts initially visible
2927

30-
if (i1.featured && !i2.featured) return -1;
31-
if (!i1.featured && i2.featured) return 1;
28+
useEffect(() => {
29+
if (navItems) {
30+
const sortedPosts = navItems.sort((i1: IBlogPost, i2: IBlogPost) => {
31+
const i1Date = new Date(i1.date);
32+
const i2Date = new Date(i2.date);
3233

33-
return i2Date.getTime() - i1Date.getTime();
34-
})
35-
: []
36-
);
37-
const [isClient, setIsClient] = useState(false);
34+
if (i1.featured && !i2.featured) return -1;
35+
if (!i1.featured && i2.featured) return 1;
36+
37+
return i2Date.getTime() - i1Date.getTime();
38+
});
3839

39-
const onFilter = (data: IBlogPost[]) => setPosts(data);
40-
const toFilter = [
41-
{
42-
name: 'type'
43-
},
44-
{
45-
name: 'authors',
46-
unique: 'name'
47-
},
48-
{
49-
name: 'tags'
40+
setPosts(sortedPosts);
5041
}
51-
];
42+
}, [navItems]);
43+
44+
useEffect(() => {
45+
setIsClient(true);
46+
}, []);
47+
48+
const loadMore = () => {
49+
setVisiblePosts((prev) => prev + 9); // Load additional 9 posts
50+
};
51+
52+
const onFilter = (data: IBlogPost[]) => {
53+
setPosts(data);
54+
setVisiblePosts(9); // Reset visible posts on filter change
55+
};
56+
57+
const toFilter = [{ name: 'type' }, { name: 'authors', unique: 'name' }, { name: 'tags' }];
58+
5259
const clearFilters = () => {
5360
router.push(`${router.pathname}`, undefined, {
5461
shallow: true
5562
});
5663
};
64+
5765
const showClearFilters = Object.keys(router.query).length > 0;
5866

5967
const description = 'Find the latest and greatest stories from our community';
6068
const image = '/img/social/blog.webp';
6169

62-
useEffect(() => {
63-
setIsClient(true);
64-
}, []);
65-
6670
return (
6771
<GenericLayout title='Blog' description={description} image={image} wide>
6872
<div className='relative px-4 pb-20 pt-8 sm:px-6 lg:px-8 lg:pb-28 lg:pt-12' id='main-content'>
@@ -122,7 +126,7 @@ export default function BlogIndexPage() {
122126
)}
123127
{Object.keys(posts).length > 0 && isClient && (
124128
<ul className='mx-auto mt-12 grid max-w-lg gap-5 lg:max-w-none lg:grid-cols-3'>
125-
{posts.map((post, index) => (
129+
{posts.slice(0, visiblePosts).map((post, index) => (
126130
<BlogPostItem key={index} post={post} />
127131
))}
128132
</ul>
@@ -132,6 +136,17 @@ export default function BlogIndexPage() {
132136
<Loader loaderText='Loading Blogs' className='mx-auto my-60' pulsating />
133137
</div>
134138
)}
139+
{visiblePosts < posts.length && isClient && (
140+
<div className='mt-8 flex justify-center'>
141+
<button
142+
type='button'
143+
className='rounded-md bg-primary-500 px-6 py-3 text-white shadow-md transition duration-300 hover:bg-primary-600'
144+
onClick={loadMore}
145+
>
146+
Load More
147+
</button>
148+
</div>
149+
)}
135150
</div>
136151
</div>
137152
</div>

0 commit comments

Comments
 (0)