Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

chore: remove manual pagination and api route

bautistaaa4ad3fe706d91
diff --git a/apps/web/src/app/api/challenge-reviews/route.ts b/apps/web/src/app/api/challenge-reviews/route.ts
deleted file mode 100644
index bb150434..00000000
--- a/apps/web/src/app/api/challenge-reviews/route.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import { NextResponse } from 'next/server';
-import { prisma } from '@repo/db';
-
-export async function GET(request: Request) {
- const { searchParams } = new URL(request.url);
- const page = Number(searchParams.get('page')) ?? 1;
- const size = Number(searchParams.get('size')) ?? 10;
-
- const [count, data] = await getChallenges(page, size);
- const pageCount = Math.ceil(count / size);
-
- return NextResponse.json([pageCount, data]);
-}
-
-export type ChallengeReviewData = Awaited<ReturnType<typeof getChallenges>>;
-async function getChallenges(page: number, size: number) {
- return prisma.$transaction([
- prisma.challenge.count({
- where: {
- status: 'PENDING',
- },
- }),
- prisma.challenge.findMany({
- skip: page * size,
- take: size,
- where: {
- status: 'PENDING',
- },
- include: {
- user: {
- select: {
- name: true,
- },
- },
- },
- }),
- ]);
-}
diff --git a/apps/web/src/components/admin/challenge-reviews/challenge-reviews.action.ts b/apps/web/src/components/admin/challenge-reviews/challenge-reviews.action.ts
new file mode 100644
index 00000000..c5f66f5a
--- /dev/null
+++ b/apps/web/src/components/admin/challenge-reviews/challenge-reviews.action.ts
@@ -0,0 +1,19 @@
+'use server';
+
+import { prisma } from '@repo/db';
+
+export type ChallengeReviewData = Awaited<ReturnType<typeof getPendingChallenges>>;
+export async function getPendingChallenges() {
+ return prisma.challenge.findMany({
+ where: {
+ status: 'PENDING',
+ },
+ include: {
+ user: {
+ select: {
+ name: true,
+ },
+ },
+ },
+ });
+}
diff --git a/apps/web/src/components/admin/challenge-reviews/data-table-pagination.tsx b/apps/web/src/components/admin/challenge-reviews/data-table-pagination.tsx
new file mode 100644
index 00000000..6290756f
--- /dev/null
+++ b/apps/web/src/components/admin/challenge-reviews/data-table-pagination.tsx
@@ -0,0 +1,77 @@
+import { Button, Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@repo/ui';
+import type { Table } from '@tanstack/react-table';
+import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight } from 'lucide-react';
+
+interface DataTablePaginationProps<TData> {
+ table: Table<TData>;
+}
+
+export function DataTablePagination<TData>({ table }: DataTablePaginationProps<TData>) {
+ return (
+ <div className="flex items-center justify-end px-2">
+ <div className="flex items-center space-x-6 lg:space-x-8">
+ <div className="flex items-center space-x-2">
+ <p className="text-sm font-medium">Rows per page</p>
+ <Select
+ value={`${table.getState().pagination.pageSize}`}
+ onValueChange={(value) => {
+ table.setPageSize(Number(value));
+ }}
+ >
+ <SelectTrigger className="h-8 w-[70px]">
+ <SelectValue placeholder={table.getState().pagination.pageSize} />
+ </SelectTrigger>
+ <SelectContent side="top">
+ {[10, 20, 30, 40, 50].map((pageSize) => (
+ <SelectItem key={pageSize} value={`${pageSize}`}>
+ {pageSize}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ <div className="flex w-[100px] items-center justify-center text-sm font-medium">
+ Page {table.getState().pagination.pageIndex + 1} of {table.getPageCount()}
+ </div>
+ <div className="flex items-center space-x-2">
+ <Button
+ variant="outline"
+ className="hidden h-8 w-8 p-0 lg:flex"
+ onClick={() => table.setPageIndex(0)}
+ disabled={!table.getCanPreviousPage()}
+ >
+ <span className="sr-only">Go to first page</span>
+ <ChevronsLeft className="h-4 w-4" />
+ </Button>
+ <Button
+ variant="outline"
+ className="h-8 w-8 p-0"
+ onClick={() => table.previousPage()}
+ disabled={!table.getCanPreviousPage()}
+ >
+ <span className="sr-only">Go to previous page</span>
+ <ChevronLeft className="h-4 w-4" />
+ </Button>
+ <Button
+ variant="outline"
+ className="h-8 w-8 p-0"
+ onClick={() => table.nextPage()}
+ disabled={!table.getCanNextPage()}
+ >
+ <span className="sr-only">Go to next page</span>
+ <ChevronRight className="h-4 w-4" />
+ </Button>
+ <Button
+ variant="outline"
+ className="hidden h-8 w-8 p-0 lg:flex"
+ onClick={() => table.setPageIndex(table.getPageCount() - 1)}
+ disabled={!table.getCanNextPage()}
+ >
+ <span className="sr-only">Go to last page</span>
+ <ChevronsRight className="h-4 w-4" />
+ </Button>
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/apps/web/src/components/admin/challenge-reviews/data-table.tsx b/apps/web/src/components/admin/challenge-reviews/data-table.tsx
index d2c17b92..4f206dcb 100644
--- a/apps/web/src/components/admin/challenge-reviews/data-table.tsx
+++ b/apps/web/src/components/admin/challenge-reviews/data-table.tsx
@@ -1,19 +1,18 @@
'use client';
import type { Challenge } from '@repo/db/types';
+import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@repo/ui';
import { useQuery } from '@tanstack/react-query';
import {
- type PaginationState,
flexRender,
getCoreRowModel,
+ getPaginationRowModel,
useReactTable,
type ColumnDef,
} from '@tanstack/react-table';
import { useRouter } from 'next/navigation';
-import { useMemo, useState } from 'react';
-import { Button, Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@repo/ui';
-import type { ChallengeReviewData } from '~/app/api/challenge-reviews/route';
-import { getBaseUrl } from '~/utils/getBaseUrl';
+import { getPendingChallenges } from './challenge-reviews.action';
+import { DataTablePagination } from './data-table-pagination';
interface Props {
columns: ColumnDef<Challenge>[];
@@ -21,41 +20,17 @@ interface Props {
export function DataTable({ columns }: Props) {
const router = useRouter();
- const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
- pageIndex: 0,
- pageSize: 20,
- });
-
- const fetchDataOptions = {
- page: pageIndex,
- size: pageSize,
- };
- const data = useQuery(['data', fetchDataOptions], () => getChallengeReviews(fetchDataOptions), {
- keepPreviousData: true,
- });
-
- const pagination = useMemo(
- () => ({
- pageIndex,
- pageSize,
- }),
- [pageIndex, pageSize],
- );
+ const { data, isLoading } = useQuery(['challenge-reviews'], () => getPendingChallenges());
const table = useReactTable({
- data: data.data?.[1] ?? [],
+ data: data ?? [],
columns,
- pageCount: data.data?.[0] ?? -1,
getCoreRowModel: getCoreRowModel(),
- state: {
- pagination,
- },
- onPaginationChange: setPagination,
- manualPagination: true,
+ getPaginationRowModel: getPaginationRowModel(),
});
- if (data.isLoading) return null;
+ if (isLoading) return null;
return (
<div className="w-full rounded-md border">
@@ -99,41 +74,7 @@ export function DataTable({ columns }: Props) {
)}
</TableBody>
</Table>
- <div className="flex items-center justify-between p-4">
- <div className="flex items-center gap-1">
- <div>Page</div>
- <strong>
- {table.getState().pagination.pageIndex + 1} of {table.getPageCount()}
- </strong>
- </div>
- <div className="space-x-2">
- <Button
- disabled={!table.getCanPreviousPage()}
- onClick={() => table.previousPage()}
- size="sm"
- variant="outline"
- >
- Previous
- </Button>
- <Button
- disabled={!table.getCanNextPage()}
- onClick={() => table.nextPage()}
- size="sm"
- variant="outline"
- >
- Next
- </Button>
- </div>
- </div>
+ <DataTablePagination table={table} />
</div>
);
}
-
-async function getChallengeReviews(options: {
- page: number;
- size: number;
-}): Promise<ChallengeReviewData> {
- return fetch(
- `${getBaseUrl()}/api/challenge-reviews?page=${options.page}&size=${options.size}`,
- ).then((res) => res.json());
-}