import { count, desc, like } from "drizzle-orm"; import Link from "next/link"; import { getTranslations } from "next-intl/server"; import { Button } from "@/components/ui/button"; import { fetchAdminList } from "@/lib/admin-list"; import { db, WebsitePoll } from "@/lib/db"; import { PERMS } from "@/lib/permissions"; import { PollsTable } from "./polls-table"; export default async function PollsPage({ params: paramsPromise, searchParams, }: { params: Promise<{ locale: string }>; searchParams: Promise>; }) { const t = await getTranslations("pages.admin.polls"); const result = await fetchAdminList( { permission: PERMS.POLLS_VIEW, fetch: async ({ search, page, perPage }) => { const where = search ? like(WebsitePoll.title, `%${search}%`) : undefined; const [rows, totals] = await Promise.all([ db .select({ id: WebsitePoll.id, title: WebsitePoll.title, status: WebsitePoll.status, showResults: WebsitePoll.showResults, multipleChoice: WebsitePoll.multipleChoice, startsAt: WebsitePoll.startsAt, endsAt: WebsitePoll.endsAt, createdAt: WebsitePoll.createdAt, }) .from(WebsitePoll) .where(where) .orderBy(desc(WebsitePoll.createdAt)) .limit(perPage) .offset((page - 1) * perPage), db.select({ total: count() }).from(WebsitePoll).where(where), ]); return { rows, total: totals[0]?.total ?? 0 }; }, }, paramsPromise, searchParams, ); return (
); }