Skip to content

Commit

Permalink
feat: add pagination for blog list
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-guoba committed Mar 18, 2024
1 parent 9938912 commit ec154de
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/(lobby)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";

export default async function LobyLayout({ children }: { children: React.ReactNode }) {
return <div className="relative flex min-h-screen flex-col">{children}</div>;
return <div className="relative flex min-h-[90vh] flex-col">{children}</div>;
}
17 changes: 13 additions & 4 deletions app/(lobby)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ function dbParams() {
return { ...defaultParam, ...filters, ...sorter };
}

export default async function Home() {

type Props = {
params: { slug: string[] };
searchParams: { [key: string]: string | string[] | undefined };
};

export default async function Home({ searchParams }: Props) {
const queryParams = dbParams();
const posts = await QueryDatabase(env.NOTION_DATABASE_ID, queryParams);
const total = posts.length;

const page = Number(searchParams["page"]) || 1;
const subpost = posts.slice((page-1)*env.POST_PAGE_SIZES, page*env.POST_PAGE_SIZES);

return (
<Shell className="md:pb-10">
<PageHeader>
Expand All @@ -41,7 +50,7 @@ export default async function Home() {
<PostCardSkeleton key={i} />
))}
>
{posts.map((post: any, i) => {
{subpost.map((post: any, i) => {
const title = rawText(post.properties?.Title?.title);
const slug = post.id; // + "/" + post.properties?.Summary?.rich_text[0]?.plain_text;
const edit_time = post?.properties?.PublishDate?.date?.start || post?.last_edited_time;
Expand All @@ -62,8 +71,8 @@ export default async function Home() {
})}
</React.Suspense>
</section>
<Separator className="mt-10" />
<PostPagination total={total} pageSize={8} ></PostPagination>
{/* <Separator className="mt-10" /> */}
<PostPagination total={total} pageSize={env.POST_PAGE_SIZES} ></PostPagination>
</Shell>
);
}
14 changes: 5 additions & 9 deletions components/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
} from "@/components/ui/pagination";

import Head from "next/head";
import Link from "next/link";
// import Link from "next/link";

import { usePathname, useSearchParams } from "next/navigation";
import { Icons } from "./icons";
// import { Icons } from "./icons";
import { getPageNumbers } from "@/lib/page-numbers";
import { cn } from "@/lib/utils";

Expand All @@ -28,13 +28,9 @@ interface PaginationProps {
* The max number of per pages
*/
pageSize: number;
/**
* Extra props to pass to the next.js links
*/
linkProps?: { [key: string]: any };
}

export function PostPagination({ total, pageSize, linkProps }: PaginationProps) {
export function PostPagination({ total, pageSize}: PaginationProps) {
const pathname = usePathname();
const searchParams = useSearchParams();
const currentPage = Number(searchParams.get("page")) || 1;
Expand Down Expand Up @@ -76,8 +72,8 @@ export function PostPagination({ total, pageSize, linkProps }: PaginationProps)
<PaginationLink
href={createPageURL(pageNumber)}
prefetch={false}
isActive={currentPage == i+1}
> {i+1} </PaginationLink>
isActive={currentPage == pageNumber}
> {pageNumber} </PaginationLink>
</PaginationItem>
)
)}
Expand Down
2 changes: 2 additions & 0 deletions env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const env = createEnv({
DATABASE_URL: z.string().optional(),
NOTION_CACHE_EXPIRER: z.coerce.number().default(3600),
NOTION_API_LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]).default("warn"),
POST_PAGE_SIZES: z.coerce.number().default(8),
},

/**
Expand All @@ -43,6 +44,7 @@ export const env = createEnv({
DATABASE_URL: process.env.DATABASE_URL,
NOTION_CACHE_EXPIRER: process.env.NOTION_CACHE_EXPIRER,
NOTION_API_LOG_LEVEL: process.env.NOTION_API_LOG_LEVEL,
POST_PAGE_SIZES: process.env.POST_PAGE_SIZES,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down

0 comments on commit ec154de

Please sign in to comment.