"use client"; import { Pencil, Trash2 } from "lucide-react"; import Link from "next/link"; import { useTranslations } from "next-intl"; import { deletePoll } from "@/actions/polls"; import { useConfirmDialog } from "@/components/admin/confirm-dialog"; import { DataTable } from "@/components/admin/data-table"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { useServerAction } from "@/hooks/use-server-action"; import type { DataTableColumn, PaginatedResult } from "@/types"; interface PollRow { id: number; title: string; status: string; showResults: number; multipleChoice: number; startsAt: Date | null; endsAt: Date | null; createdAt: Date; } const statusVariants: Record = { active: "default", draft: "secondary", closed: "outline", }; interface PollsTableProps { data: PaginatedResult; } export function PollsTable({ data }: PollsTableProps) { const t = useTranslations("pages.admin.polls"); const { run, isPending } = useServerAction(); const { confirm, dialog: confirmDialog } = useConfirmDialog(); const statusLabel: Record = { active: t("statusActive"), draft: t("statusDraft"), closed: t("statusClosed"), }; async function handleDelete(row: PollRow) { const ok = await confirm({ title: "Delete poll", description: `Delete "${row.title}"? This cannot be undone.`, confirmLabel: "Delete", cancelLabel: "Cancel", }); if (!ok) return; run(() => deletePoll({ id: row.id }), { successMessage: "Poll deleted!", errorMessage: "Failed to delete poll.", }); } const columns: DataTableColumn[] = [ { key: "id", label: t("colId"), sortable: true }, { key: "title", label: t("colTitle"), sortable: true, render: (value) => {String(value)}, }, { key: "status", label: t("colStatus"), sortable: true, render: (value) => { const key = String(value); return ( {statusLabel[key] ?? key} ); }, }, { key: "multipleChoice", label: t("colType"), render: (value) => ( {Number(value) ? t("typeMultiple") : t("typeSingle")} ), }, { key: "createdAt", label: t("colCreated"), sortable: true, render: (value) => ( {new Date(value as unknown as string).toLocaleDateString()} ), }, { key: "actions", label: t("colActions"), render: (_, row) => (
), }, ]; return ( <> {confirmDialog} ); }