"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 { deleteSubmission, deleteAllSubmissions } from "@/app/actions/submissions"; import { toast } from "sonner"; import { useRouter } from "next/navigation"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { Spinner } from "@/components/ui/spinner"; interface DeleteFormButtonProps { formId: string; } export 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 ( Are you absolutely sure? This action cannot be undone. This will permanently delete the form, all of its questions, and all submissions/answers. ); } export function DeleteSubmissionButton({ id, redirectUrl }: { id: string, redirectUrl?: string }) { const [loading, setLoading] = useState(false); const router = useRouter(); const handleDelete = async () => { setLoading(true); try { await deleteSubmission(id); toast.success("Submission deleted"); if (redirectUrl) { router.push(redirectUrl); } } catch (err: any) { toast.error(err.message || "Failed to delete submission"); } finally { setLoading(false); } }; return ( Are you sure? This action cannot be undone. This will permanently delete this submission and remove its data from our servers. Cancel Delete ); } export function DeleteAllSubmissionsButton() { const [loading, setLoading] = useState(false); const handleDeleteAll = async () => { setLoading(true); try { await deleteAllSubmissions(); toast.success("All submissions deleted"); } catch (err: any) { toast.error(err.message || "Failed to delete submissions"); } finally { setLoading(false); } }; return ( Are you absolutely sure? This action cannot be undone. This will permanently delete ALL submissions and remove their data from our servers. Cancel Delete All ); }