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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
"use client"
import * as React from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { toast } from "sonner"
import { Button } from "@/components/ui/button"
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { ScrollArea } from "@/components/ui/scroll-area"
import {
Sheet,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
} from "@/components/ui/sheet"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { updateQnaSchema, type UpdateQnaSchema } from "@/lib/qna/validation"
import { QNA_CATEGORY_LABELS, QnaViewSelect } from "@/db/schema"
import { updateQna } from "../service"
import TiptapEditor from "@/components/qna/tiptap-editor"
interface UpdateQnaSheetProps {
open: boolean
onOpenChange: (open: boolean) => void
qna: QnaViewSelect | null
}
export function UpdateQnaSheet({ open, onOpenChange, qna }: UpdateQnaSheetProps) {
const [isUpdatePending, startUpdateTransition] = React.useTransition()
const form = useForm<UpdateQnaSchema>({
resolver: zodResolver(updateQnaSchema),
defaultValues: {
title: "",
content: "",
category: undefined,
},
})
// qna 데이터가 변경될 때 폼 초기화
React.useEffect(() => {
if (qna) {
form.reset({
title: qna.title,
content: qna.content,
category: qna.category,
})
}
}, [qna, form])
function onSubmit(input: UpdateQnaSchema) {
if (!qna) return
startUpdateTransition(async () => {
try {
const result = await updateQna(qna.id, input)
if (result.success) {
toast.success(result.message || "질문이 성공적으로 수정되었습니다.")
onOpenChange(false)
} else {
toast.error(result.error || "질문 수정에 실패했습니다.")
}
} catch (error) {
toast.error("예기치 못한 오류가 발생했습니다.")
console.error("질문 수정 오류:", error)
}
})
}
return (
<Sheet open={open} onOpenChange={onOpenChange}>
<SheetContent className="flex h-full w-full flex-col gap-6 sm:max-w-4xl">
<SheetHeader className="text-left">
<SheetTitle>질문 수정</SheetTitle>
<SheetDescription>
질문의 제목과 내용을 수정할 수 있습니다. 수정된 내용은 즉시 반영됩니다.
</SheetDescription>
</SheetHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col flex-1 min-h-0 space-y-6">
{/* 작성자 정보 표시 */}
{qna && (
<div className="rounded-md border border-muted bg-muted/50 p-3 text-sm">
<div className="font-medium text-muted-foreground mb-1">질문 정보</div>
<div className="space-y-1">
<div>작성자: {qna.authorName} ({qna.authorEmail})</div>
<div>회사: {qna.companyName || "미지정"}</div>
<div>작성일: {new Date(qna.createdAt).toLocaleDateString("ko-KR")}</div>
<div>답변 수: {qna.totalAnswers}개 / 댓글 수: {qna.totalComments}개</div>
</div>
</div>
)}
{/* 제목 입력 */}
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>제목 *</FormLabel>
<FormControl>
<Input
placeholder="질문의 제목을 입력해주세요"
disabled={isUpdatePending}
className="text-base"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="category"
render={({ field }) => (
<FormItem>
<FormLabel>카테고리 *</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
disabled={isUpdatePending}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="카테고리를 선택해주세요" />
</SelectTrigger>
</FormControl>
<SelectContent>
{QNA_CATEGORY_LABELS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
{/* 내용 입력 (리치텍스트 에디터) */}
<FormField
control={form.control}
name="content"
render={({ field }) => (
<FormItem className="flex flex-col flex-1 min-h-0">
<FormLabel>내용 *</FormLabel>
<FormControl className="flex flex-col flex-1 min-h-0">
<TiptapEditor
content={field.value}
setContent={field.onChange}
disabled={isUpdatePending}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
<SheetFooter className="gap-2 pt-4 border-t">
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isUpdatePending}
>
취소
</Button>
<Button
type="submit"
onClick={form.handleSubmit(onSubmit)}
disabled={isUpdatePending}
>
{isUpdatePending ? "수정 중..." : "수정 완료"}
</Button>
</SheetFooter>
</SheetContent>
</Sheet>
)
}
|