From 1a2241c40e10193c5ff7008a7b7b36cc1d855d96 Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Tue, 25 Mar 2025 15:55:45 +0900 Subject: initial commit --- lib/rfqs/tbe-table/tbe-table-columns.tsx | 307 +++++++++++++++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 lib/rfqs/tbe-table/tbe-table-columns.tsx (limited to 'lib/rfqs/tbe-table/tbe-table-columns.tsx') diff --git a/lib/rfqs/tbe-table/tbe-table-columns.tsx b/lib/rfqs/tbe-table/tbe-table-columns.tsx new file mode 100644 index 00000000..29fbd5cd --- /dev/null +++ b/lib/rfqs/tbe-table/tbe-table-columns.tsx @@ -0,0 +1,307 @@ +"use client" + +import * as React from "react" +import { type DataTableRowAction } from "@/types/table" +import { type ColumnDef } from "@tanstack/react-table" +import { Download, Ellipsis, MessageSquare } from "lucide-react" +import { toast } from "sonner" + +import { getErrorMessage } from "@/lib/handle-error" +import { formatDate } from "@/lib/utils" +import { Badge } from "@/components/ui/badge" +import { Button } from "@/components/ui/button" +import { Checkbox } from "@/components/ui/checkbox" +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuRadioGroup, + DropdownMenuRadioItem, + DropdownMenuSub, + DropdownMenuSubContent, + DropdownMenuSubTrigger, + DropdownMenuTrigger, +} from "@/components/ui/dropdown-menu" +import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header" +import { useRouter } from "next/navigation" + +import { + VendorTbeColumnConfig, + vendorTbeColumnsConfig, + VendorWithTbeFields, +} from "@/config/vendorTbeColumnsConfig" + +type NextRouter = ReturnType + +interface GetColumnsProps { + setRowAction: React.Dispatch< + React.SetStateAction | null> + > + router: NextRouter + openCommentSheet: (vendorId: number) => void + openFilesDialog: (tbeId:number , vendorId: number) => void +} + +/** + * tanstack table 컬럼 정의 (중첩 헤더 버전) + */ +export function getColumns({ + setRowAction, + router, + openCommentSheet, + openFilesDialog +}: GetColumnsProps): ColumnDef[] { + // ---------------------------------------------------------------- + // 1) Select 컬럼 (체크박스) + // ---------------------------------------------------------------- + const selectColumn: ColumnDef = { + id: "select", + header: ({ table }) => ( + table.toggleAllPageRowsSelected(!!value)} + aria-label="Select all" + className="translate-y-0.5" + /> + ), + cell: ({ row }) => ( + row.toggleSelected(!!value)} + aria-label="Select row" + className="translate-y-0.5" + /> + ), + size: 40, + enableSorting: false, + enableHiding: false, + } + + // ---------------------------------------------------------------- + // 2) 그룹화(Nested) 컬럼 구성 + // ---------------------------------------------------------------- + const groupMap: Record[]> = {} + + vendorTbeColumnsConfig.forEach((cfg) => { + const groupName = cfg.group || "_noGroup" + if (!groupMap[groupName]) { + groupMap[groupName] = [] + } + + // childCol: ColumnDef + const childCol: ColumnDef = { + accessorKey: cfg.id, + enableResizing: true, + header: ({ column }) => ( + + ), + meta: { + excelHeader: cfg.excelHeader, + group: cfg.group, + type: cfg.type, + }, + // 셀 렌더링 + cell: ({ row, getValue }) => { + // 1) 필드값 가져오기 + const val = getValue() + + if (cfg.id === "vendorStatus") { + const statusVal = row.original.vendorStatus + if (!statusVal) return null + // const Icon = getStatusIcon(statusVal) + return ( + + {statusVal} + + ) + } + + + if (cfg.id === "rfqVendorStatus") { + const statusVal = row.original.rfqVendorStatus + if (!statusVal) return null + // const Icon = getStatusIcon(statusVal) + const variant = statusVal ==="INVITED"?"default" :statusVal ==="DECLINED"?"destructive":statusVal ==="ACCEPTED"?"secondary":"outline" + return ( + + {statusVal} + + ) + } + + // 예) TBE Updated (날짜) + if (cfg.id === "tbeUpdated") { + const dateVal = val as Date | undefined + if (!dateVal) return null + return formatDate(dateVal) + } + + // 그 외 필드는 기본 값 표시 + return val ?? "" + }, + } + + groupMap[groupName].push(childCol) + }) + + // groupMap → nestedColumns + const nestedColumns: ColumnDef[] = [] + Object.entries(groupMap).forEach(([groupName, colDefs]) => { + if (groupName === "_noGroup") { + nestedColumns.push(...colDefs) + } else { + nestedColumns.push({ + id: groupName, + header: groupName, + columns: colDefs, + }) + } + }) + +// ---------------------------------------------------------------- +// 3) Comments 컬럼 +// ---------------------------------------------------------------- +const commentsColumn: ColumnDef = { + id: "comments", + header: ({ column }) => ( + + ), + cell: ({ row }) => { + const vendor = row.original + const commCount = vendor.comments?.length ?? 0 + + function handleClick() { + // rowAction + openCommentSheet + setRowAction({ row, type: "comments" }) + openCommentSheet(vendor.tbeId ?? 0) + } + + return ( +
+ + {/* + {commCount > 0 ? `${commCount} Comments` : "Add Comment"} + */} +
+ ) + }, + enableSorting: false, + maxSize:80 +} + + // ---------------------------------------------------------------- + // 4) Actions 컬럼 (예: 초대하기 버튼) + // ---------------------------------------------------------------- + // const actionsColumn: ColumnDef = { + // id: "actions", + // cell: ({ row }) => { + // const status = row.original.tbeResult + // // 예: 만약 tbeResult가 없을 때만 초대하기 버튼 표시 + // if (status) { + // return null + // } + + // return ( + // + // ) + // }, + // size: 80, + // enableSorting: false, + // enableHiding: false, + // } +// ---------------------------------------------------------------- +// 3) Files Column - Add before Comments column +// ---------------------------------------------------------------- +const filesColumn: ColumnDef = { + id: "files", + header: ({ column }) => ( + + ), + cell: ({ row }) => { + const vendor = row.original + // We'll assume that files count will be populated from the backend + // You'll need to modify your getTBE function to include files + const filesCount = vendor.files?.length ?? 0 + + function handleClick() { + // Open files dialog + setRowAction({ row, type: "files" }) + openFilesDialog(vendor.tbeId ?? 0, vendor.vendorId ?? 0) + } + + return ( +
+ +
+ ) + }, + enableSorting: false, + maxSize: 80 +} + +// ---------------------------------------------------------------- +// 5) 최종 컬럼 배열 - Update to include the files column +// ---------------------------------------------------------------- +return [ + selectColumn, + ...nestedColumns, + filesColumn, // Add the files column before comments + commentsColumn, + // actionsColumn, +] + +} \ No newline at end of file -- cgit v1.2.3