From d0d2eaa2de58a0c33e9a21604b126961403cd69e Mon Sep 17 00:00:00 2001 From: dujinkim Date: Wed, 14 May 2025 06:12:13 +0000 Subject: (최겸) 기술영업 조선, 해양Top, 해양 Hull 아이템 리스트 개발(CRUD, excel import/export/template) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/items-tech/table/update-items-sheet.tsx | 390 ++++++++++++++++++++++++++++ 1 file changed, 390 insertions(+) create mode 100644 lib/items-tech/table/update-items-sheet.tsx (limited to 'lib/items-tech/table/update-items-sheet.tsx') diff --git a/lib/items-tech/table/update-items-sheet.tsx b/lib/items-tech/table/update-items-sheet.tsx new file mode 100644 index 00000000..2db3b193 --- /dev/null +++ b/lib/items-tech/table/update-items-sheet.tsx @@ -0,0 +1,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({ + 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 ( + + + + {getItemTypeLabel()} 수정 + + {getItemTypeLabel()} 정보를 수정합니다. 수정할 필드를 입력해주세요. + + +
+ + ( + + Material Group + + + + + + )} + /> + ( + + Description + + + + + + )} + /> + ( + + 기능(공종) + + + + )} + /> + + {/* 조선 아이템 전용 필드 */} + {itemType === 'shipbuilding' && ( + ( + + 선종 + + + + + + )} + /> + )} + + {/* 해양 TOP 또는 HULL 아이템 전용 필드 */} + {(itemType === 'offshoreTop' || itemType === 'offshoreHull') && ( + <> + ( + + Item List 1 + + + + + + )} + /> + ( + + Item List 2 + + + + + + )} + /> + ( + + Item List 3 + + + + + + )} + /> + ( + + Item List 4 + + + + + + )} + /> + + )} + + ( + + Size/Dimension + +