"use client"; import { Button } from "@unsend/ui/src/button"; import { Input } from "@unsend/ui/src/input"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@unsend/ui/src/dialog"; import { api } from "~/trpc/react"; import React, { useState } from "react"; import { toast } from "@unsend/ui/src/toaster"; import { Trash2 } from "lucide-react"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@unsend/ui/src/form"; import { ContactBook } from "@prisma/client"; const contactBookSchema = z.object({ name: z.string(), }); export const DeleteContactBook: React.FC<{ contactBook: Partial & { id: string }; }> = ({ contactBook }) => { const [open, setOpen] = useState(false); const deleteContactBookMutation = api.contacts.deleteContactBook.useMutation(); const utils = api.useUtils(); const contactBookForm = useForm>({ resolver: zodResolver(contactBookSchema), }); async function onContactBookDelete( values: z.infer ) { if (values.name !== contactBook.name) { contactBookForm.setError("name", { message: "Name does not match", }); return; } deleteContactBookMutation.mutate( { contactBookId: contactBook.id, }, { onSuccess: () => { utils.contacts.getContactBooks.invalidate(); setOpen(false); toast.success(`Contact book deleted`); }, } ); } const name = contactBookForm.watch("name"); return ( (_open !== open ? setOpen(_open) : null)} > Delete Contact Book Are you sure you want to delete{" "} {contactBook.name} ? You can't reverse this.
( Contact book name {formState.errors.name ? ( ) : ( . )} )} />
); }; export default DeleteContactBook;