"use client" import * as React from "react" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { toast } from "sonner" import { Loader, Upload, X } from "lucide-react" import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Switch } from "@/components/ui/switch" import { updateInformationData } from "@/lib/information/service" import { updateInformationSchema, type UpdateInformationSchema } from "@/lib/information/validations" import type { PageInformation } from "@/db/schema/information" interface UpdateInformationDialogProps { open: boolean onOpenChange: (open: boolean) => void information?: PageInformation onClose: () => void } export function UpdateInformationDialog({ open, onOpenChange, information, onClose, }: UpdateInformationDialogProps) { const router = useRouter() const [isLoading, setIsLoading] = React.useState(false) const [uploadedFile, setUploadedFile] = React.useState(null) const form = useForm({ resolver: zodResolver(updateInformationSchema), defaultValues: { id: 0, pageCode: "", pageName: "", title: "", description: "", noticeTitle: "", noticeContent: "", isActive: true, }, }) // 인포메이션 데이터가 변경되면 폼 업데이트 React.useEffect(() => { if (information && open) { form.reset({ id: information.id, pageCode: information.pageCode, pageName: information.pageName, title: information.title, description: information.description, noticeTitle: information.noticeTitle || "", noticeContent: information.noticeContent || "", attachmentFileName: information.attachmentFileName || "", attachmentFilePath: information.attachmentFilePath || "", attachmentFileSize: information.attachmentFileSize || "", isActive: information.isActive, }) } }, [information, open, form]) const handleFileSelect = (event: React.ChangeEvent) => { const file = event.target.files?.[0] if (file) { setUploadedFile(file) // 파일 크기를 MB 단위로 변환 const sizeInMB = (file.size / (1024 * 1024)).toFixed(2) form.setValue("attachmentFileName", file.name) form.setValue("attachmentFileSize", `${sizeInMB} MB`) } } const removeFile = () => { setUploadedFile(null) form.setValue("attachmentFileName", "") form.setValue("attachmentFilePath", "") form.setValue("attachmentFileSize", "") } const uploadFile = async (file: File): Promise => { const formData = new FormData() formData.append("file", file) const response = await fetch("/api/upload", { method: "POST", body: formData, }) if (!response.ok) { throw new Error("파일 업로드에 실패했습니다.") } const result = await response.json() return result.url } const onSubmit = async (values: UpdateInformationSchema) => { setIsLoading(true) try { const finalValues = { ...values } // 새 파일이 있으면 업로드 if (uploadedFile) { const filePath = await uploadFile(uploadedFile) finalValues.attachmentFilePath = filePath } const result = await updateInformationData(finalValues) if (result.success) { toast.success(result.message) onClose() router.refresh() } else { toast.error(result.message) } } catch (error) { toast.error("인포메이션 수정에 실패했습니다.") console.error(error) } finally { setIsLoading(false) } } const handleClose = () => { setUploadedFile(null) onClose() } const currentFileName = form.watch("attachmentFileName") return ( 인포메이션 수정 페이지 인포메이션 정보를 수정합니다.
( 페이지 코드 )} /> ( 페이지명 )} />
( 제목 )} /> ( 설명