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
|
"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 { 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
type ShipbuildingItem = {
id: number
itemCode: string
workType: "기장" | "전장" | "선실" | "배관" | "철의"
shipTypes: string
itemList: string | null
}
type OffshoreTopItem = {
id: number
itemCode: string
workType: "TM" | "TS" | "TE" | "TP"
itemList: string | null
subItemList: string | null
}
type OffshoreHullItem = {
id: number
itemCode: string
workType: "HA" | "HE" | "HH" | "HM" | "NC"
itemList: string | null
subItemList: string | null
}
type UpdateItemSchema = {
itemCode?: string
workType?: string
shipTypes?: string
itemList?: 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: {
workType: item.workType,
...getItemTypeSpecificDefaults(item, itemType),
},
})
function getItemTypeSpecificDefaults(
item: ShipbuildingItem | OffshoreTopItem | OffshoreHullItem,
itemType: ItemType
) {
switch (itemType) {
case 'shipbuilding':
return {
shipTypes: (item as ShipbuildingItem).shipTypes,
itemList: (item as ShipbuildingItem).itemList || "",
};
case 'offshoreTop':
case 'offshoreHull':
const offshoreItem = item as OffshoreTopItem | OffshoreHullItem;
return {
itemList: offshoreItem.itemList || "",
subItemList: offshoreItem.subItemList || ""
};
default:
return {};
}
}
async function onSubmit(data: UpdateItemSchema) {
try {
setIsSubmitting(true)
let result;
switch (itemType) {
case 'shipbuilding':
result = await modifyShipbuildingItem({
...data,
id: item.id,
itemCode: item.itemCode // itemCode는 변경 불가, 원래 값 사용
});
break;
case 'offshoreTop':
result = await modifyOffshoreTopItem({
...data,
id: item.id,
itemCode: item.itemCode // itemCode는 변경 불가, 원래 값 사용
});
break;
case 'offshoreHull':
result = await modifyOffshoreHullItem({
...data,
id: item.id,
itemCode: item.itemCode // itemCode는 변경 불가, 원래 값 사용
});
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">
{/* Material Group을 Form 안으로 이동 */}
<div className="mt-4">
<div className="grid gap-2">
<label className="text-sm font-medium leading-none">
Material Group (수정 불가)
</label>
<Input value={item.itemCode} disabled readOnly />
</div>
</div>
<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>
)}
/>
</>
)}
<FormField
control={form.control}
name="itemList"
render={({ field }) => (
<FormItem>
<FormLabel>아이템 리스트</FormLabel>
<FormControl>
<Input placeholder="아이템 리스트를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<SheetFooter>
<SheetClose asChild>
<Button variant="outline">취소</Button>
</SheetClose>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? "수정 중..." : "수정"}
</Button>
</SheetFooter>
</form>
</Form>
</SheetContent>
</Sheet>
)
}
|