Skip to content

Commit

Permalink
Merge pull request #29 from nowrobin/feat/quoteList_pagination
Browse files Browse the repository at this point in the history
Feat/quote list pagination
  • Loading branch information
nowrobin authored Jul 21, 2024
2 parents a338f05 + 3af8732 commit 1dc226c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import prisma from "@/app/lib/prisma";
import { createClient } from "@/app/utils/supabase/server";
import { NextRequest } from "next/server";

export async function GET(req: NextRequest) {
export async function GET(
req: NextRequest,
{ params }: { params: { quote_id: string } }
) {
const supabase = createClient();
const user = await supabase.auth.getUser();

const quoteNumber = parseInt(params.quote_id);
const quotes = user.data.user
? await prisma.user.findUnique({
where: {
Expand All @@ -20,6 +23,9 @@ export async function GET(req: NextRequest) {
},
})
: await prisma.quote.findMany({
take: 10,
skip: quoteNumber ? 10 : 0,
...(quoteNumber && { cursor: { id: quoteNumber } }),
orderBy: [
{
id: "asc",
Expand Down
23 changes: 17 additions & 6 deletions src/app/quote-list/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import { useEffect, useState } from "react";

export default function QuoteList() {
const [list, setList] = useState<any>([]);
const [page, setPage] = useState(0);
const router = useRouter();
useEffect(() => {
fetch("/api/quoteList")
fetch(`/api/quoteList/${page}`)
.then((data) => data.json())
.then((res) => {
const { data } = res;
setList(data);
});
}, []);

}, [page]);
const handleQuoteClick = (index: number) => {
router.push(`/${index}`);
};
//TODO: fix the type of the value in map
return (
<div className="flex flex-col gap-2 w-screen items-center justify-center">
<div className="mt-10 text-slate-700 text-3xl">Quote List</div>
<div className="flex flex-col gap-2 w-screen text-slate-700 items-center justify-center">
<div className="mt-10 text-3xl">Quote List</div>
{list.map((value: any, index: number) => {
return (
<div
key={index}
className="bg-slate-600 w-[32rem]"
className="bg-slate-600 text-white w-[32rem]"
onClick={() => handleQuoteClick(index + 1)}
>
<div className="flex flex-row gap-10">
Expand All @@ -38,6 +38,17 @@ export default function QuoteList() {
</div>
);
})}
<div className="flex flex-row gap-10">
<button
onClick={() =>
setPage((prev) => (prev !== 0 ? (prev -= 1) : (prev = 0)))
}
>
Prev
</button>
<div>{page}</div>
<button onClick={() => setPage((prev) => (prev += 1))}>Prev</button>
</div>
</div>
);
}

1 comment on commit 1dc226c

@vercel
Copy link

@vercel vercel bot commented on 1dc226c Jul 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ecrire – ./

ecrire-git-develop-nowrobins-projects.vercel.app
ecrire-nowrobins-projects.vercel.app

Please sign in to comment.