Files
erika/components/delete-form-button.tsx
T

72 lines
1.9 KiB
TypeScript

"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Trash2, Loader2 } from "lucide-react";
import { deleteForm } from "@/app/actions/form";
import { toast } from "sonner";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
interface DeleteFormButtonProps {
formId: string;
}
export default function DeleteFormButton({ formId }: DeleteFormButtonProps) {
const [open, setOpen] = useState(false);
const [deleting, setDeleting] = useState(false);
const handleDelete = async () => {
setDeleting(true);
try {
await deleteForm(formId);
toast.success("Form deleted successfully!");
setOpen(false);
} catch (err: any) {
toast.error(err.message || "Failed to delete form.");
} finally {
setDeleting(false);
}
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="ghost" size="icon">
<Trash2 className="w-4 h-4" />
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Are you absolutely sure?</DialogTitle>
<DialogDescription>
This action cannot be undone. This will permanently delete the form, all of its questions, and all submissions/answers.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="ghost" disabled={deleting} onClick={() => setOpen(false)}>
Cancel
</Button>
<Button variant="destructive" disabled={deleting} onClick={handleDelete}>
{deleting ? (
<>
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
Deleting...
</>
) : (
"Delete"
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}