import { Badge } from "@workspace/ui/components/badge"; import { Button } from "@workspace/ui/components/button"; import { ConfirmButton } from "@workspace/ui/composed/confirm-button"; import { ProTable, type ProTableActions, } from "@workspace/ui/composed/pro-table/pro-table"; import { createLotteryActivity, deleteLotteryActivity, getLotteryActivityList, pauseLotteryActivity, publishLotteryActivity, updateLotteryActivity, } from "@workspace/ui/services/admin/lottery"; import { useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import { formatDate } from "@/utils/common"; import ActivityDetail from "./activity-detail"; import ActivityForm from "./activity-form"; import GrantDialog from "./grant-dialog"; export default function Lottery() { const { t } = useTranslation("lottery"); const [loading, setLoading] = useState(false); const [detailOpen, setDetailOpen] = useState(false); const [grantOpen, setGrantOpen] = useState(false); const [selectedActivity, setSelectedActivity] = useState(null); const ref = useRef(null); const statusConfig: Record< API.LotteryActivityStatus, { label: string; variant: "secondary" | "destructive" | "outline"; className?: string; } > = { draft: { label: t("status.draft", "Draft"), variant: "secondary" }, running: { label: t("status.running", "Running"), variant: "outline", className: "border-transparent bg-green-500 text-white", }, paused: { label: t("status.paused", "Paused"), variant: "outline", className: "border-transparent bg-yellow-500 text-white", }, ended: { label: t("status.ended", "Ended"), variant: "destructive" }, }; const statusOptions = [ { label: t("statusAll", "All"), value: "all" }, { label: t("status.draft", "Draft"), value: "draft" }, { label: t("status.running", "Running"), value: "running" }, { label: t("status.paused", "Paused"), value: "paused" }, { label: t("status.ended", "Ended"), value: "ended" }, ]; return ( <> action={ref} actions={{ render: (row) => { const rowActions: React.ReactNode[] = []; if (row.status === "draft") { rowActions.push( { try { await publishLotteryActivity({ id: row.id }); toast.success(t("publishSuccess", "Publish Success")); ref.current?.refresh(); } catch { // Request layer already surfaces the error toast } }} title={t( "confirmPublish", "Are you sure you want to publish this activity?" )} trigger={} /> ); } if (row.status === "running") { rowActions.push( { try { await pauseLotteryActivity({ id: row.id }); toast.success(t("pauseSuccess", "Pause Success")); ref.current?.refresh(); } catch { // Request layer already surfaces the error toast } }} title={t( "confirmPause", "Are you sure you want to pause this activity?" )} trigger={ } /> ); } rowActions.push( { setLoading(true); try { await updateLotteryActivity({ ...values, id: row.id }); toast.success(t("updateSuccess", "Update Success")); ref.current?.refresh(); setLoading(false); return true; } catch (_error) { setLoading(false); return false; } }} title={t("editActivity", "Edit Activity")} trigger={t("edit", "Edit")} />, , , { try { await deleteLotteryActivity({ id: row.id }); toast.success(t("deleteSuccess", "Deleted")); ref.current?.refresh(); } catch { // Request layer already surfaces the error toast } }} title={t("confirmDeleteActivity", "Delete this activity?")} trigger={ } /> ); return rowActions; }, }} columns={[ { accessorKey: "id", header: t("id", "ID"), }, { accessorKey: "title", header: t("title", "Title"), }, { accessorKey: "status", header: t("statusLabel", "Status"), cell: ({ row }) => { const status = statusConfig[row.original.status]; return ( {status.label} ); }, }, { accessorKey: "start_at", header: t("activityTime", "Activity Time"), cell: ({ row }) => `${formatDate(row.original.start_at)} - ${formatDate( row.original.end_at )}`, }, { accessorKey: "grid_size", header: t("gridSize", "Grid Size"), }, ]} header={{ toolbar: ( { setLoading(true); try { await createLotteryActivity(values); toast.success(t("createSuccess", "Create Success")); ref.current?.refresh(); setLoading(false); return true; } catch (_error) { setLoading(false); return false; } }} title={t("createActivity", "Create Activity")} trigger={t("create", "Create")} /> ), }} params={[ { key: "status", placeholder: t("statusLabel", "Status"), options: statusOptions, }, ]} request={async (pagination, filters) => { const { data } = await getLotteryActivityList({ ...pagination, ...filters, status: filters?.status && filters.status !== "all" ? filters.status : undefined, }); return { list: data.data?.list || [], total: data.data?.total || 0, }; }} /> ref.current?.refresh()} onOpenChange={setDetailOpen} open={detailOpen} /> ); }