diff options
| author | joonhoekim <26rote@gmail.com> | 2025-03-25 15:55:45 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-03-25 15:55:45 +0900 |
| commit | 1a2241c40e10193c5ff7008a7b7b36cc1d855d96 (patch) | |
| tree | 8a5587f10ca55b162d7e3254cb088b323a34c41b /hooks/use-form-schema.ts | |
initial commit
Diffstat (limited to 'hooks/use-form-schema.ts')
| -rw-r--r-- | hooks/use-form-schema.ts | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/hooks/use-form-schema.ts b/hooks/use-form-schema.ts new file mode 100644 index 00000000..a4d8f3c6 --- /dev/null +++ b/hooks/use-form-schema.ts @@ -0,0 +1,113 @@ +"use client" + +import useSWR from "swr" + +// ------------------ 폼 스키마 구조 ------------------ +export interface FormField { + label: string + name: string + type: string // "text" | "number" | "select" | "date" | ... + options?: string[] // select 등에 필요한 옵션 +} + +export interface FormSchema { + formType: "table" | "form" + fields: FormField[] +} + +// ------------------ 실제 요청 함수 (mock) ------------------ +async function fetchFormSchema(itemName: string) { + // 실제 API 요청 예시: + // const res = await fetch(`/api/form-schema?item=${encodeURIComponent(itemName)}`) + // return await res.json() + + // 여기서는 예시 데이터(하드코딩) + if (itemName === "Mechanical Data Sheet") { + // 예: 테이블 형태 (설비 리스트) + return { + formType: "table", + fields: [ + { label: "Equipment Name", name: "equipmentName", type: "text" }, + { label: "Tag No.", name: "tagNo", type: "text" }, + { label: "Capacity", name: "capacity", type: "number" }, + { label: "Manufacturer", name: "manufacturer", type: "text" }, + { label: "Model", name: "model", type: "text" }, + { label: "Weight (kg)", name: "weight", type: "number" }, + { label: "Power (kW)", name: "power", type: "number" }, + // 필요하다면 옵션이 있는 select 필드 등도 추가 + // { label: "Power Source", name: "powerSource", type: "select", options: ["AC380V", "AC220V", "DC", "Other"] }, + ], + } + } else if (itemName === "Room List") { + // 예: 또 다른 테이블 형태 (건물 방 리스트) + return { + formType: "table", + fields: [ + { label: "Room Name", name: "roomName", type: "text" }, + { label: "Area (m²)", name: "area", type: "number" }, + { label: "Floor No.", name: "floorNo", type: "number" }, + { label: "Ceiling Type", name: "ceiling", type: "text" }, + { label: "Wall Finish", name: "wallFinish",type: "text" }, + { label: "Floor Finish", name: "floorFinish", type: "text" }, + // select 예시 + { label: "Usage", name: "usage", type: "select", options: ["Office", "Laboratory", "Storage", "Other"] }, + ], + } + } else if (itemName === "Piping Line List") { + // 예: 테이블 형태 (배관 정보) + return { + formType: "table", + fields: [ + { label: "Line Number", name: "lineNumber", type: "text" }, + { label: "Size (inches)", name: "size", type: "number" }, + { label: "Service", name: "service", type: "text" }, + { label: "Material Spec", name: "materialSpec",type: "text" }, + { label: "Insulation", name: "insulation", type: "text" }, + { label: "Design Temp (°C)", name: "designTemp", type: "number" }, + { label: "Design Press (bar)", name: "designPress", type: "number" }, + ], + } + } else if (itemName === "Electrical Load List") { + // 추가 예시: 테이블 or form (여기선 테이블로 예시) + return { + formType: "table", + fields: [ + { label: "Load Name", name: "loadName", type: "text" }, + { label: "Voltage (V)", name: "voltage", type: "number" }, + { label: "Phase", name: "phase", type: "select", options: ["Single-phase", "Three-phase"] }, + { label: "Load (kW)", name: "loadKw", type: "number" }, + { label: "Power Factor (%)", name: "powerFactor", type: "number" }, + { label: "Location", name: "location", type: "text" }, + ], + } + } else { + // 디폴트: formType: "form" (단일 폼) + // 예: 임시로 "Vendor Contact Info" 라고 가정 + return { + formType: "form", + fields: [ + { label: "Vendor Name", name: "vendorName", type: "text" }, + { label: "Contact Name", name: "contactName", type: "text" }, + { label: "Phone Number", name: "phoneNumber", type: "text" }, + { label: "Email", name: "email", type: "text" }, + // date 예시 + { label: "Contract Date", name: "contractDate", type: "date" }, + ], + } + } +} + +// ------------------ SWR 훅 ------------------ +export function useFormSchema(itemName: string | null) { + // itemName이 없으면(선택 안 됨) 요청하지 않음 + const { data, error, isLoading } = useSWR( + itemName ? `/api/form-schema?item=${encodeURIComponent(itemName)}` : null, + () => (itemName ? fetchFormSchema(itemName) : null) + ) + + return { + schema: data as FormSchema | undefined, + isLoading, + isError: !!error, + } +}
\ No newline at end of file |
