summaryrefslogtreecommitdiff
path: root/hooks/use-form-schema.ts
blob: a4d8f3c644a8302e4c6a7d082626f2f36ab1834c (plain)
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
"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,
  }
}