"use client"; import { Loader2, Plus } from "lucide-react"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import { createCatalogPage } from "@/actions/catalog"; import { createBcPage } from "@/actions/catalog-bc"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { useServerAction } from "@/hooks/use-server-action"; import { CATALOG_LAYOUTS } from "@/lib/catalog-layouts"; interface CatalogActionsProps { catalogType?: "normal" | "bc"; /** Prefill parent and auto-open create dialog (from ?newPageParent=). */ initialParentId?: string | null; } export function CatalogActions({ catalogType = "normal", initialParentId = null, }: CatalogActionsProps) { const { run, isPending } = useServerAction(); const router = useRouter(); const [open, setOpen] = useState(false); const [caption, setCaption] = useState(""); const [parentId, setParentId] = useState("-1"); const [pageLayout, setPageLayout] = useState("default_3x3"); const isBc = catalogType === "bc"; useEffect(() => { if (initialParentId == null || initialParentId === "") return; const n = Number.parseInt(initialParentId, 10); if (!Number.isFinite(n)) return; setParentId(String(n)); setOpen(true); if (typeof window === "undefined") return; const url = new URL(window.location.href); if (url.searchParams.has("newPageParent")) { url.searchParams.delete("newPageParent"); const qs = url.searchParams.toString(); router.replace(`${url.pathname}${qs ? `?${qs}` : ""}`, { scroll: false, }); } }, [initialParentId, router]); function handleCreate() { if (!caption.trim()) { toast.error("Caption is required"); return; } const input = { caption: caption.trim(), parentId: parseInt(parentId, 10), pageLayout, }; const fn = isBc ? () => createBcPage(input) : () => createCatalogPage(input); run(fn, { successMessage: `Catalog page "${caption.trim()}" created.`, onSuccess: (data) => { setOpen(false); setCaption(""); setParentId("-1"); if (data.id) { window.location.href = isBc ? `/admin/catalog/builder-club/${data.id}` : `/admin/catalog/${data.id}`; } }, }); } return ( {isBc ? "Create Builder Club Page" : "Create Catalog Page"}
setCaption(e.target.value)} placeholder="Page name" />
setParentId(e.target.value)} />
); }