import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import { TrashIcon } from "lucide-react"; import { useRouter } from "next/router"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const deleteComposeSchema = z.object({ projectName: z.string().min(1, { message: "Compose name is required", }), }); type DeleteCompose = z.infer; interface Props { composeId: string; } export const DeleteCompose = ({ composeId }: Props) => { const [isOpen, setIsOpen] = useState(false); const { mutateAsync, isLoading } = api.compose.delete.useMutation(); const { data } = api.compose.one.useQuery( { composeId }, { enabled: !!composeId }, ); const { push } = useRouter(); const form = useForm({ defaultValues: { projectName: "", }, resolver: zodResolver(deleteComposeSchema), }); const onSubmit = async (formData: DeleteCompose) => { const expectedName = `${data?.name}/${data?.appName}`; if (formData.projectName === expectedName) { await mutateAsync({ composeId }) .then((result) => { push(`/dashboard/project/${result?.projectId}`); toast.success("Compose deleted successfully"); setIsOpen(false); }) .catch(() => { toast.error("Error deleting the compose"); }); } else { form.setError("projectName", { message: `Project name must match "${expectedName}"`, }); } }; return ( Are you absolutely sure? This action cannot be undone. This will permanently delete the compose. If you are sure please enter the compose name to delete this compose.
( To confirm, type "{data?.name}/{data?.appName}" in the box below {" "} )} />
); };