import { useState } from "react"; import { toast } from "sonner"; import { AlertDialog, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog" import { Button } from "@/components/ui/button"; import { Loader2 } from "lucide-react"; import { serverApiCall } from "@/lib/server-api"; interface Props { endpoint?: string onSuccess?: () => void; onCancel?: () => void; title?: string description?: string, method?: any payload?: any } export function ConfirmDelete({ endpoint, title, description, onSuccess, onCancel, method, payload }: Props) { const [isLoading, setIsLoading] = useState(false); const handleDelete = async () => { setIsLoading(true) const toastId = toast.loading('Deleting data...'); try { const res = await serverApiCall(method || 'DELETE', endpoint!, payload) if (res.success) { toast.success("Data deleted successfully ☺️", { id: toastId }); onSuccess!() } else { toast.error("Oops sorry, error has occurred 😢", { description: `(${res.code}) ${res.message || "An unexpected error occurred while saving data. Please try again later."}`, id: toastId }) } } catch (error: any) { toast.error(error.message, { id: toastId }) } finally { setIsLoading(false) } } return ( {title || 'Are you absolutely sure?'} {description || "This action cannot be undone. This will permanently delete this data and remove our servers."} Cancel ) }