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
207
208
209
210
211
212
213
214
215
216
|
"use client"
import * as React from "react"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
import { Upload } from "lucide-react"
import { toast } from "sonner"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Button } from "@/components/ui/button"
import { Textarea } from "@/components/ui/textarea"
import { Input } from "@/components/ui/input"
import { Progress } from "@/components/ui/progress"
const updateRevisionSchema = z.object({
revisionComment: z.string().min(1, "리비전 설명을 입력해주세요"),
file: z.instanceof(File, {
message: "새 버전 파일을 선택해주세요",
}).refine((file) => file.size <= 100 * 1024 * 1024, {
message: "파일 크기는 100MB를 초과할 수 없습니다.",
}),
})
type UpdateRevisionFormData = z.infer<typeof updateRevisionSchema>
interface UpdateRevisionDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
attachment: {
id: number;
originalFileName?: string | null;
currentRevision?: string | null;
};
onSuccess?: () => void;
}
export function UpdateRevisionDialog({
open,
onOpenChange,
attachment,
onSuccess,
}: UpdateRevisionDialogProps) {
const [isSubmitting, setIsSubmitting] = React.useState(false)
const [uploadProgress, setUploadProgress] = React.useState(0)
const form = useForm<UpdateRevisionFormData>({
resolver: zodResolver(updateRevisionSchema),
defaultValues: {
revisionComment: "",
},
})
const onSubmit = async (data: UpdateRevisionFormData) => {
setIsSubmitting(true);
setUploadProgress(0);
try {
const formData = new FormData();
formData.append("attachmentId", attachment.id.toString());
formData.append("revisionComment", data.revisionComment);
formData.append("file", data.file);
// 진행률 시뮬레이션
setUploadProgress(30);
const response = await fetch("/api/rfq-attachments/revision", {
method: "POST",
body: formData,
});
setUploadProgress(70);
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || "리비전 업데이트 실패");
}
const result = await response.json();
setUploadProgress(100);
if (result.success) {
toast.success(result.message);
form.reset();
onOpenChange(false);
onSuccess?.();
} else {
toast.error(result.message);
}
} catch (error) {
toast.error(error instanceof Error ? error.message : "파일 업로드 중 오류가 발생했습니다");
} finally {
setIsSubmitting(false);
setUploadProgress(0);
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[500px]">
<DialogHeader>
<DialogTitle>새 버전 업로드</DialogTitle>
<DialogDescription>
{attachment.originalFileName && (
<div className="mt-2">
<div className="text-sm font-medium">현재 파일:</div>
<div className="text-sm text-muted-foreground">
{attachment.originalFileName}
{attachment.currentRevision && ` (Rev. ${attachment.currentRevision})`}
</div>
</div>
)}
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="file"
render={({ field: { onChange, value, ...field } }) => (
<FormItem>
<FormLabel>새 버전 파일</FormLabel>
<FormControl>
<Input
type="file"
accept=".pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.zip,.rar,.dwg,.dxf"
onChange={(e) => {
const file = e.target.files?.[0];
if (file) {
onChange(file);
// 파일 크기 검증
if (file.size > 100 * 1024 * 1024) {
form.setError("file", {
message: "파일 크기는 100MB를 초과할 수 없습니다."
});
}
}
}}
disabled={isSubmitting}
{...field}
/>
</FormControl>
{value && (
<p className="text-sm text-muted-foreground mt-1">
선택된 파일: {value.name} ({(value.size / 1024 / 1024).toFixed(2)}MB)
</p>
)}
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="revisionComment"
render={({ field }) => (
<FormItem>
<FormLabel>리비전 설명</FormLabel>
<FormControl>
<Textarea
placeholder="변경사항을 설명해주세요"
className="resize-none"
rows={3}
disabled={isSubmitting}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{isSubmitting && (
<div className="space-y-2">
<div className="flex justify-between text-sm">
<span>업로드 진행중...</span>
<span>{uploadProgress}%</span>
</div>
<Progress value={uploadProgress} />
</div>
)}
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isSubmitting}
>
취소
</Button>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? "업로드 중..." : "업로드"}
</Button>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
)
}
|