Skip to content

Commit

Permalink
#24 Added proper logic for showing all posts or filtered by category.
Browse files Browse the repository at this point in the history
  • Loading branch information
krckyboy committed Feb 18, 2024
1 parent 5f9a692 commit baac646
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion next/src/app/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Props {
}

const Page: NextPage<Props> = async ({ searchParams: { category } }) => {
const posts = await db.getPosts();
const posts = category ? await db.getPostsByCategory(category) : await db.getPosts();
const categories = await db.getCategories();

return (
Expand Down
19 changes: 19 additions & 0 deletions next/src/scripts/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ export const db = {
const queryString = qs.stringify(queryParams);
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryString}`);
},
getPostsByCategory: async (category: string) => {
const queryParams = {
sort: ['publishedAt:desc'],
filters: {
categories: {
name: {
$eq: category
}
}
},
pagination: {
pageSize: 10,
page: 1
}
};

const queryString = qs.stringify(queryParams);
return await fetchWrapper<PostsFetchResponse>(`/posts?${queryString}`);
},
getPostBySlug: async (slug: string) => {
const queryParams = {
filters: {
Expand Down

0 comments on commit baac646

Please sign in to comment.