mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
109 lines
4.2 KiB
TypeScript
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>
|
|
)
|
|
}
|