"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { PencilIcon } from "lucide-react"; import { toast } from "sonner"; import type { Ticket } from "@/payload-types"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { editTicket } from "@/app/(frontend)/helpdesk/actions"; import { TICKET_CATEGORIES, TICKET_PRIORITIES, type TicketCategory, type TicketPriority, } from "@/lib/tickets/ticketMeta"; interface EditTicketDialogProps { readonly ticket: Ticket; readonly description: string; } function isTicketCategory(value: string): value is TicketCategory { return TICKET_CATEGORIES.some((option) => option.value === value); } function isTicketPriority(value: string): value is TicketPriority { return TICKET_PRIORITIES.some((option) => option.value === value); } export function EditTicketDialog({ ticket, description }: EditTicketDialogProps) { const router = useRouter(); const [open, setOpen] = useState(false); const [title, setTitle] = useState(ticket.title); const [details, setDetails] = useState(description); const [category, setCategory] = useState(ticket.category); const [priority, setPriority] = useState(ticket.priority); const [pending, setPending] = useState(false); const [error, setError] = useState(null); async function handleSubmit() { setPending(true); setError(null); const result = await editTicket({ ticketId: ticket.id, title, description: details, category, priority, }); setPending(false); if (!result.success) { setError(result.error ?? "Could not edit the ticket."); return; } setOpen(false); toast.success("Ticket updated"); router.refresh(); } return ( Edit ticket Update the request details. The previous and new values are retained in the audit trail.
{ event.preventDefault(); void handleSubmit(); }} >
setTitle(event.target.value)} />