summaryrefslogtreecommitdiff
path: root/lib/tech-vendor-rfq-response/vendor-cbe-table/rfq-items-table/rfq-items-table.tsx
blob: c5c67e54a63a695e48fcd0fe20277226395420f3 (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
'use client'

import * as React from "react"
import { ClientDataTable } from "@/components/client-data-table/data-table"
import { getColumns } from "./rfq-items-table-column"
import { DataTableAdvancedFilterField } from "@/types/table"
import { Loader2 } from "lucide-react"
import { useToast } from "@/hooks/use-toast"
import { getItemsByRfqId } from "../../service"

export interface ItemData {
  id: number
  itemCode: string
  description: string | null
  quantity: number
  uom: string | null
  createdAt: Date
  updatedAt: Date
}

interface RFQItemsTableProps {
  rfqId: number
}

export function RfqItemsTable({ rfqId }: RFQItemsTableProps) {
  const { toast } = useToast()
  
  const columns = React.useMemo(
    () => getColumns(),
    []
  )
  
  const [rfqItems, setRfqItems] = React.useState<ItemData[]>([])
  const [isLoading, setIsLoading] = React.useState(false)
  
  React.useEffect(() => {
    async function loadItems() {
      setIsLoading(true)
      try {
        // Use the correct function name (camelCase)
        const result = await getItemsByRfqId(rfqId)
        if (result.success && result.data) {
          setRfqItems(result.data as ItemData[])
        } else {
          throw new Error(result.error || "Unknown error occurred")
        }
      } catch (error) {
        console.error("RFQ 아이템 로드 오류:", error)
        toast({
          title: "Error",
          description: "Failed to load RFQ items",
          variant: "destructive",
        })
      } finally {
        setIsLoading(false)
      }
    }
    loadItems()
  }, [toast, rfqId])
  
  const advancedFilterFields: DataTableAdvancedFilterField<ItemData>[] = [
    { id: "itemCode", label: "Item Code", type: "text" },
    { id: "description", label: "Description", type: "text" },
    { id: "quantity", label: "Quantity", type: "number" },
    { id: "uom", label: "UoM", type: "text" },
  ]
    
  // If loading, show a flex container that fills the parent and centers the spinner
  if (isLoading) {
    return (
      <div className="flex h-full w-full items-center justify-center">
        <Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
      </div>
    )
  }
  
  // Otherwise, show the table
  return (
    <ClientDataTable
      data={rfqItems}
      columns={columns}
      advancedFilterFields={advancedFilterFields}
    >
    </ClientDataTable>
  )
}