|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as React from "react"; |
| 4 | +import { Pencil, Trash2 } from "lucide-react"; |
| 5 | +import { toast } from "sonner"; |
| 6 | + |
| 7 | +import { |
| 8 | + AlertDialog, |
| 9 | + AlertDialogAction, |
| 10 | + AlertDialogCancel, |
| 11 | + AlertDialogContent, |
| 12 | + AlertDialogDescription, |
| 13 | + AlertDialogFooter, |
| 14 | + AlertDialogHeader, |
| 15 | + AlertDialogTitle, |
| 16 | +} from "@/components/ui/alert-dialog"; |
| 17 | +import { Button } from "@/components/ui/button"; |
| 18 | +import { |
| 19 | + Tooltip, |
| 20 | + TooltipContent, |
| 21 | + TooltipTrigger, |
| 22 | +} from "@/components/ui/tooltip"; |
| 23 | +import { deleteForm } from "../actions"; |
| 24 | +import type { FormListItem } from "../queries"; |
| 25 | + |
| 26 | +export function FormActionsCell({ |
| 27 | + form, |
| 28 | + onEdit, |
| 29 | +}: { |
| 30 | + form: FormListItem; |
| 31 | + onEdit: (form: FormListItem) => void; |
| 32 | +}) { |
| 33 | + const [deleteOpen, setDeleteOpen] = React.useState(false); |
| 34 | + const [pending, startTransition] = React.useTransition(); |
| 35 | + |
| 36 | + const handleDelete = () => { |
| 37 | + const fd = new FormData(); |
| 38 | + fd.set("form_id", form.id); |
| 39 | + startTransition(async () => { |
| 40 | + const result = await deleteForm(null, fd); |
| 41 | + if (result?.errors?._form) { |
| 42 | + toast.error("Could not delete form", { |
| 43 | + description: result.errors._form.join(" "), |
| 44 | + }); |
| 45 | + return; |
| 46 | + } |
| 47 | + if (result?.message) { |
| 48 | + toast.success(result.message); |
| 49 | + setDeleteOpen(false); |
| 50 | + } |
| 51 | + }); |
| 52 | + }; |
| 53 | + |
| 54 | + return ( |
| 55 | + <> |
| 56 | + <div className="flex items-center justify-end gap-0.5"> |
| 57 | + <Tooltip> |
| 58 | + <TooltipTrigger asChild> |
| 59 | + <Button |
| 60 | + variant="ghost" |
| 61 | + size="icon-sm" |
| 62 | + aria-label="Edit form" |
| 63 | + onClick={() => onEdit(form)} |
| 64 | + > |
| 65 | + <Pencil /> |
| 66 | + </Button> |
| 67 | + </TooltipTrigger> |
| 68 | + <TooltipContent>Edit</TooltipContent> |
| 69 | + </Tooltip> |
| 70 | + <Tooltip> |
| 71 | + <TooltipTrigger asChild> |
| 72 | + <Button |
| 73 | + variant="ghost" |
| 74 | + size="icon-sm" |
| 75 | + aria-label="Delete form" |
| 76 | + onClick={() => setDeleteOpen(true)} |
| 77 | + > |
| 78 | + <Trash2 /> |
| 79 | + </Button> |
| 80 | + </TooltipTrigger> |
| 81 | + <TooltipContent>Delete</TooltipContent> |
| 82 | + </Tooltip> |
| 83 | + </div> |
| 84 | + |
| 85 | + <AlertDialog open={deleteOpen} onOpenChange={setDeleteOpen}> |
| 86 | + <AlertDialogContent className="sm:max-w-md"> |
| 87 | + <AlertDialogHeader className="place-items-start text-left sm:place-items-start sm:text-left"> |
| 88 | + <AlertDialogTitle>Delete form?</AlertDialogTitle> |
| 89 | + <AlertDialogDescription> |
| 90 | + {form.attachedServiceCount > 0 |
| 91 | + ? `This form is attached to ${form.attachedServiceCount} service(s). Detach it from those services before deleting.` |
| 92 | + : `Permanently delete "${form.name}" and all of its questions. This cannot be undone.`} |
| 93 | + </AlertDialogDescription> |
| 94 | + </AlertDialogHeader> |
| 95 | + <AlertDialogFooter className="sm:flex-row sm:justify-end"> |
| 96 | + <AlertDialogCancel disabled={pending}>Cancel</AlertDialogCancel> |
| 97 | + <AlertDialogAction |
| 98 | + disabled={pending || form.attachedServiceCount > 0} |
| 99 | + onClick={(e) => { |
| 100 | + e.preventDefault(); |
| 101 | + handleDelete(); |
| 102 | + }} |
| 103 | + > |
| 104 | + {pending ? "Deleting..." : "Delete"} |
| 105 | + </AlertDialogAction> |
| 106 | + </AlertDialogFooter> |
| 107 | + </AlertDialogContent> |
| 108 | + </AlertDialog> |
| 109 | + </> |
| 110 | + ); |
| 111 | +} |
0 commit comments