import { useMemo, useState } from "react"; import { toast } from "sonner"; import { AlertDialog, 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"; import * as Yup from 'yup' import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { useForm } from "react-hook-form"; import { Input } from "../ui/input"; import { yupResolver } from "@hookform/resolvers/yup"; interface Props { endpoint?: string onSuccess?: () => void; onCancel?: () => void; title: string titleButton?: string description?: string, method?: any } interface FormDataType { name: string; } export function ConfirmDeleteWithInput({ endpoint, title, titleButton, description, onSuccess, onCancel, method }: Props) { const [isLoading, setIsLoading] = useState(false); const validationSchema = useMemo(() => { return Yup.object().shape({ name: Yup.string() .required("Please fill this field to continue") .oneOf([title], `Input must exactly match "${title}"`), // <-- Exact match validation }); }, [title]); const form = useForm({ resolver: yupResolver(validationSchema), defaultValues: { "name": '', }, }); const onSubmit = async () => { setIsLoading(true) const toastId = toast.loading('Deleting data...'); try { const res = await serverApiCall(method || 'DELETE', endpoint!) 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 ( Are you absolutely sure? {description || "This action cannot be undone. This will permanently delete this data and remove our servers."}
( Please type {title} to confirm * )} />
) }