Files
hats-app/components/modal/ConfirmDeleteWithInput.tsx
Ichsan Kurniawan b5735a90c4 update logic, ui
2026-04-21 16:00:21 +07:00

109 lines
4.2 KiB
TypeScript

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<FormDataType>({
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 (
<AlertDialog open={true}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
{description || "This action cannot be undone. This will permanently delete this data and remove our servers."}
</AlertDialogDescription>
</AlertDialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<div className="mt-4 mb-8">
<FormField control={form.control} name='name'
render={({ field }) => (
<FormItem>
<FormLabel>Please type <strong>{title}</strong> to confirm <span className='text-red-600 font-bold'>*</span></FormLabel>
<FormControl>
<Input {...field} placeholder="Type the name..." />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<AlertDialogFooter className="grid grid-cols-1 gap-3">
<Button onClick={onCancel} disabled={isLoading} type='button' variant='secondary'>Cancel</Button>
<Button type="submit" disabled={isLoading}>
{isLoading && <Loader2 className='animate-spin' />}
{titleButton || "I understand the consequences, delete this data"}
</Button>
</AlertDialogFooter>
</form>
</Form>
</AlertDialogContent>
</AlertDialog>
)
}