// components/rfq/comment-edit-dialog.tsx "use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Textarea } from "@/components/ui/textarea"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import { MessageSquare, Loader2 } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { useRouter } from "next/navigation"; const commentFormSchema = z.object({ responseComment: z.string().optional(), vendorComment: z.string().optional(), }); type CommentFormData = z.infer; interface CommentEditDialogProps { responseId: number; currentResponseComment?: string; currentVendorComment?: string; trigger?: React.ReactNode; onSuccess?: () => void; } export function CommentEditDialog({ responseId, currentResponseComment, currentVendorComment, trigger, onSuccess, }: CommentEditDialogProps) { const [open, setOpen] = useState(false); const [isSaving, setIsSaving] = useState(false); const { toast } = useToast(); const router = useRouter(); const form = useForm({ resolver: zodResolver(commentFormSchema), defaultValues: { responseComment: currentResponseComment || "", vendorComment: currentVendorComment || "", }, }); const onSubmit = async (data: CommentFormData) => { setIsSaving(true); try { const response = await fetch("/api/vendor-responses/update-comment", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ responseId, responseComment: data.responseComment, vendorComment: data.vendorComment, }), }); if (!response.ok) { const error = await response.json(); throw new Error(error.message || "코멘트 업데이트 실패"); } toast({ title: "코멘트 업데이트 완료", description: "코멘트가 성공적으로 업데이트되었습니다.", }); setOpen(false); router.refresh(); onSuccess?.(); } catch (error) { console.error("Comment update error:", error); toast({ title: "업데이트 실패", description: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.", variant: "destructive", }); } finally { setIsSaving(false); } }; return ( {trigger || ( )} 코멘트 수정
{/* 응답 코멘트 */} ( 응답 코멘트