import { zodResolver } from '@hookform/resolvers/zod'; import { useMutation } from '@tanstack/react-query'; import { overlay } from 'overlay-kit'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; import { postMutations } from '@/entities/post'; import { Button } from '@/shared/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/shared/ui/dialog'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/shared/ui/form'; import { Input } from '@/shared/ui/input'; const createPostSchema = z.object({ title: z .string() .min(1, '제목을 입력하세요') .max(100, '제목은 100자 이하로 입력하세요'), body: z.string().min(1, '내용을 입력하세요'), }); type CreatePostForm = z.infer; interface CreatePostDialogProps { isOpen: boolean; close: () => void; } export const openCreatePostDialog = () => { overlay.open(({ isOpen, close, unmount }) => ( { close(); unmount(); }} /> )); }; const CreatePostDialog = ({ isOpen, close }: CreatePostDialogProps) => { const form = useForm({ resolver: zodResolver(createPostSchema), defaultValues: { title: '', body: '' }, }); const { mutate, isPending } = useMutation(postMutations.create()); const onSubmit = form.handleSubmit((values) => { mutate(values, { onSuccess: close }); }); return ( { if (!open) { close(); } }} > 새 글 제목과 내용을 입력하고 저장하세요.
{ onSubmit(event); }} > ( 제목 )} /> ( 내용 )} />
); };