"use client"; import { useState, type FormEvent, type MouseEvent, type ReactNode } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogTitle } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; export function CommissionTicketRename({ children }: { children: ReactNode }) { const router = useRouter(); const [open, setOpen] = useState(false); const [ticketId, setTicketId] = useState(null); const [value, setValue] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); function changeOpen(next: boolean) { if (busy) return; setOpen(next); } function openFromClick(event: MouseEvent) { if (!(event.target instanceof Element)) return; const trigger = event.target.closest("[data-ticket-rename-id]"); if (!trigger) return; setTicketId(trigger.dataset.ticketRenameId ?? null); setValue(trigger.dataset.ticketRenameTitle ?? ""); setError(""); setOpen(true); } async function save(event: FormEvent) { event.preventDefault(); if (busy || !ticketId) return; setBusy(true); setError(""); try { const response = await fetch(`/api/commission/tickets/${ticketId}/title`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title: value }), }); if (!response.ok) throw new Error(); setOpen(false); router.refresh(); } catch { setError("เปลี่ยนชื่อ Ticket ไม่สำเร็จ กรุณาลองอีกครั้ง"); } finally { setBusy(false); } } return
{children}
เปลี่ยนชื่อ Ticket ชื่อนี้จะแสดงให้ลูกค้าเห็น เว้นว่างเพื่อใช้หมายเลข Ticket เดิม
setValue(event.target.value)} maxLength={80} placeholder="เช่น ทีมสำหรับ Spiral Abyss" autoFocus disabled={busy} />
{error &&

{error}

}
; }