From 73f62ed2f681b8760feee50a1d7365364fa538ac Mon Sep 17 00:00:00 2001 From: ROG Date: Sat, 13 Apr 2024 16:33:31 -0300 Subject: [PATCH 1/5] feat: create post fetch from page and component --- src/components/Pagination.astro | 47 +++++++++++++++++ src/components/PostsListsPaginated.astro | 28 ++++++++++ src/components/ui/pagination.tsx | 8 ++- src/layouts/LayoutBase.astro | 39 +++++++------- src/scripts/consts.ts | 1 + src/scripts/content_fetcher.ts | 66 ++++++++++++++++++------ src/scripts/content_utils.ts | 9 ++++ 7 files changed, 158 insertions(+), 40 deletions(-) create mode 100644 src/components/Pagination.astro create mode 100644 src/components/PostsListsPaginated.astro create mode 100644 src/scripts/consts.ts diff --git a/src/components/Pagination.astro b/src/components/Pagination.astro new file mode 100644 index 0000000..7fa68db --- /dev/null +++ b/src/components/Pagination.astro @@ -0,0 +1,47 @@ +--- +import { + Pagination as PaginationShadcn, + PaginationContent, + PaginationItem, + PaginationLink, + PaginationNext, + PaginationPrevious, +} from "./ui/pagination"; + +interface Props { + currentPage?: number | undefined; + basePath: string; + pageCount: number; +} + +const { basePath, currentPage, pageCount } = Astro.props; +--- + + + + { + currentPage && currentPage > 1 && ( + + + + ) + } + { + Array.from({ length: pageCount }).map((_, index) => { + const page = index + 1; + return ( + + {page} + + ); + }) + } + { + currentPage && currentPage < pageCount && ( + + + + ) + } + + diff --git a/src/components/PostsListsPaginated.astro b/src/components/PostsListsPaginated.astro new file mode 100644 index 0000000..367aa44 --- /dev/null +++ b/src/components/PostsListsPaginated.astro @@ -0,0 +1,28 @@ +--- +import { getPageCount } from '@/scripts/content_utils'; +import Pagination from './Pagination.astro'; +import PostsList from './PostsList.astro'; +import { getPostsFromPage } from '@/scripts/content_fetcher'; + +interface Props { + currentPage: number; +} + +const { currentPage } = Astro.props; +const basePath = '/posts/'; +const pageCount = await getPageCount(); +const pagePosts = await getPostsFromPage(currentPage); +--- + +
+ + { + pageCount !== 1 && ( + + ) + } +
\ No newline at end of file diff --git a/src/components/ui/pagination.tsx b/src/components/ui/pagination.tsx index f3d9f32..dd16ef5 100644 --- a/src/components/ui/pagination.tsx +++ b/src/components/ui/pagination.tsx @@ -21,7 +21,7 @@ const PaginationContent = React.forwardRef< >(({ className, ...props }, ref) => (