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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
"use client"
import * as React from "react"
import { useForm } from "react-hook-form"
import { Button } from "@/components/ui/button"
import {
Sheet,
SheetClose,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
} from "@/components/ui/sheet"
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { Textarea } from "@/components/ui/textarea"
import { toast } from "sonner"
import {
modifyShipbuildingItem,
modifyOffshoreTopItem,
modifyOffshoreHullItem
} from "../service"
import { ItemType } from "./delete-items-dialog"
const shipbuildingWorkTypes = [
{ value: "기장", label: "기장" },
{ value: "전장", label: "전장" },
{ value: "선실", label: "선실" },
{ value: "배관", label: "배관" },
{ value: "철의", label: "철의" },
] as const
const offshoreTopWorkTypes = [
{ value: "TM", label: "TM" },
{ value: "TS", label: "TS" },
{ value: "TE", label: "TE" },
{ value: "TP", label: "TP" },
] as const
const offshoreHullWorkTypes = [
{ value: "HA", label: "HA" },
{ value: "HE", label: "HE" },
{ value: "HH", label: "HH" },
{ value: "HM", label: "HM" },
{ value: "NC", label: "NC" },
] as const
interface CommonItemFields {
id: number
itemId: number
itemCode: string
itemName: string
description: string | null
createdAt: Date
updatedAt: Date
}
type ShipbuildingItem = CommonItemFields & {
workType: "기장" | "전장" | "선실" | "배관" | "철의"
shipTypes: string
}
type OffshoreTopItem = CommonItemFields & {
workType: "TM" | "TS" | "TE" | "TP"
itemList1: string | null
itemList2: string | null
itemList3: string | null
itemList4: string | null
}
type OffshoreHullItem = CommonItemFields & {
workType: "HA" | "HE" | "HH" | "HM" | "NC"
itemList1: string | null
itemList2: string | null
itemList3: string | null
itemList4: string | null
}
type UpdateItemSchema = {
itemCode?: string
itemName?: string
description?: string
workType?: string
shipTypes?: string
itemList1?: string
itemList2?: string
itemList3?: string
itemList4?: string
}
interface UpdateItemSheetProps {
item: ShipbuildingItem | OffshoreTopItem | OffshoreHullItem
itemType: ItemType
open: boolean
onOpenChange: (open: boolean) => void
}
export function UpdateItemSheet({ item, itemType, open, onOpenChange }: UpdateItemSheetProps) {
const [isSubmitting, setIsSubmitting] = React.useState(false)
// 초기값 설정
const form = useForm<UpdateItemSchema>({
defaultValues: {
itemCode: item.itemCode,
itemName: item.itemName,
description: item.description || "",
workType: item.workType,
...getItemTypeSpecificDefaults(item, itemType),
},
})
function getItemTypeSpecificDefaults(
item: ShipbuildingItem | OffshoreTopItem | OffshoreHullItem,
itemType: ItemType
) {
switch (itemType) {
case 'shipbuilding':
return {
shipTypes: (item as ShipbuildingItem).shipTypes
};
case 'offshoreTop':
case 'offshoreHull':
const offshoreItem = item as OffshoreTopItem | OffshoreHullItem;
return {
itemList1: offshoreItem.itemList1 || "",
itemList2: offshoreItem.itemList2 || "",
itemList3: offshoreItem.itemList3 || "",
itemList4: offshoreItem.itemList4 || "",
};
default:
return {};
}
}
async function onSubmit(data: UpdateItemSchema) {
try {
setIsSubmitting(true)
let result;
switch (itemType) {
case 'shipbuilding':
result = await modifyShipbuildingItem({
...data,
id: item.id
});
break;
case 'offshoreTop':
result = await modifyOffshoreTopItem({
...data,
id: item.id
});
break;
case 'offshoreHull':
result = await modifyOffshoreHullItem({
...data,
id: item.id
});
break;
default:
toast.error("지원하지 않는 아이템 타입입니다");
return;
}
if (result.success) {
toast.success(result.message || "아이템이 수정되었습니다.")
onOpenChange(false)
} else {
toast.error(result.error || "아이템 수정 중 오류가 발생했습니다. 다시 시도해주세요.")
}
} catch (error) {
toast.error("오류가 발생했습니다.")
console.error(error)
} finally {
setIsSubmitting(false)
}
}
const getItemTypeLabel = () => {
switch (itemType) {
case 'shipbuilding':
return '조선 아이템';
case 'offshoreTop':
return '해양 TOP 아이템';
case 'offshoreHull':
return '해양 HULL 아이템';
default:
return '아이템';
}
}
const getWorkTypeOptions = () => {
switch (itemType) {
case 'shipbuilding':
return shipbuildingWorkTypes;
case 'offshoreTop':
return offshoreTopWorkTypes;
case 'offshoreHull':
return offshoreHullWorkTypes;
default:
return [];
}
}
return (
<Sheet open={open} onOpenChange={onOpenChange}>
<SheetContent>
<SheetHeader>
<SheetTitle>{getItemTypeLabel()} 수정</SheetTitle>
<SheetDescription>
{getItemTypeLabel()} 정보를 수정합니다. 수정할 필드를 입력해주세요.
</SheetDescription>
</SheetHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="itemCode"
render={({ field }) => (
<FormItem>
<FormLabel>Material Group</FormLabel>
<FormControl>
<Input placeholder="Material Group을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="itemName"
render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Input placeholder="Description을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="workType"
render={({ field }) => (
<FormItem>
<FormLabel>기능(공종)</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="기능(공종)을 선택하세요" />
</SelectTrigger>
</FormControl>
<SelectContent>
{getWorkTypeOptions().map((type) => (
<SelectItem key={type.value} value={type.value}>
{type.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
{/* 조선 아이템 전용 필드 */}
{itemType === 'shipbuilding' && (
<FormField
control={form.control}
name="shipTypes"
render={({ field }) => (
<FormItem>
<FormLabel>선종</FormLabel>
<FormControl>
<Input placeholder="선종을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
)}
{/* 해양 TOP 또는 HULL 아이템 전용 필드 */}
{(itemType === 'offshoreTop' || itemType === 'offshoreHull') && (
<>
<FormField
control={form.control}
name="itemList1"
render={({ field }) => (
<FormItem>
<FormLabel>Item List 1</FormLabel>
<FormControl>
<Input placeholder="Item List 1을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="itemList2"
render={({ field }) => (
<FormItem>
<FormLabel>Item List 2</FormLabel>
<FormControl>
<Input placeholder="Item List 2를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="itemList3"
render={({ field }) => (
<FormItem>
<FormLabel>Item List 3</FormLabel>
<FormControl>
<Input placeholder="Item List 3을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="itemList4"
render={({ field }) => (
<FormItem>
<FormLabel>Item List 4</FormLabel>
<FormControl>
<Input placeholder="Item List 4를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</>
)}
<FormField
control={form.control}
name="description"
render={({ field }) => (
<FormItem>
<FormLabel>Size/Dimension</FormLabel>
<FormControl>
<Textarea
placeholder="Size/Dimension을 입력하세요"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<SheetFooter>
<SheetClose asChild>
<Button variant="outline">취소</Button>
</SheetClose>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? "수정 중..." : "수정"}
</Button>
</SheetFooter>
</form>
</Form>
</SheetContent>
</Sheet>
)
}
|