summaryrefslogtreecommitdiff
path: root/lib/b-rfq/vendor-response/comment-edit-dialog.tsx
blob: 0c2c0c62ff4eaf785a055a7724286364a8083090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// 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<typeof commentFormSchema>;

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<CommentFormData>({
    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 (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        {trigger || (
          <Button size="sm" variant="outline">
            <MessageSquare className="h-3 w-3 mr-1" />
            코멘트
          </Button>
        )}
      </DialogTrigger>
      <DialogContent className="max-w-lg">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            <MessageSquare className="h-5 w-5" />
            코멘트 수정
          </DialogTitle>
        </DialogHeader>

        <Form {...form}>
          <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
            {/* 응답 코멘트 */}
            <FormField
              control={form.control}
              name="responseComment"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>응답 코멘트</FormLabel>
                  <FormControl>
                    <Textarea
                      placeholder="응답에 대한 설명을 입력하세요..."
                      className="resize-none"
                      rows={3}
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />

            {/* 벤더 코멘트 */}
            <FormField
              control={form.control}
              name="vendorComment"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>벤더 코멘트 (내부용)</FormLabel>
                  <FormControl>
                    <Textarea
                      placeholder="내부 참고용 코멘트를 입력하세요..."
                      className="resize-none"
                      rows={3}
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />

            {/* 버튼 */}
            <div className="flex justify-end gap-2">
              <Button
                type="button"
                variant="outline"
                onClick={() => setOpen(false)}
                disabled={isSaving}
              >
                취소
              </Button>
              <Button type="submit" disabled={isSaving}>
                {isSaving && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
                {isSaving ? "저장 중..." : "저장"}
              </Button>
            </div>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  );
}