From ef4c533ebacc2cdc97e518f30e9a9350004fcdfb Mon Sep 17 00:00:00 2001 From: dujinkim Date: Mon, 28 Apr 2025 02:13:30 +0000 Subject: ~20250428 작업사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../table/project-series-dialog.tsx | 133 ++++++++++++++++++ .../table/projects-table-columns.tsx | 102 ++++++++++++++ .../table/projects-table-toolbar-actions.tsx | 89 ++++++++++++ lib/bidding-projects/table/projects-table.tsx | 156 +++++++++++++++++++++ 4 files changed, 480 insertions(+) create mode 100644 lib/bidding-projects/table/project-series-dialog.tsx create mode 100644 lib/bidding-projects/table/projects-table-columns.tsx create mode 100644 lib/bidding-projects/table/projects-table-toolbar-actions.tsx create mode 100644 lib/bidding-projects/table/projects-table.tsx (limited to 'lib/bidding-projects/table') diff --git a/lib/bidding-projects/table/project-series-dialog.tsx b/lib/bidding-projects/table/project-series-dialog.tsx new file mode 100644 index 00000000..168ede7e --- /dev/null +++ b/lib/bidding-projects/table/project-series-dialog.tsx @@ -0,0 +1,133 @@ +"use client" + +import * as React from "react" +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog" +import { + Table, + TableBody, + TableCell, + TableHead, + TableHeader, + TableRow, +} from "@/components/ui/table" +import { BiddingProjects } from "@/db/schema" +import { useToast } from "@/hooks/use-toast" + +// Import the function +import { getProjectSeriesForProject } from "../service" + +// Define the ProjectSeries type based on the schema +interface ProjectSeries { + pspid: string; + sersNo: string; + scDt?: string | null; + klDt?: string | null; + lcDt?: string | null; + dlDt?: string | null; + dockNo?: string | null; + dockNm?: string | null; + projNo?: string | null; + post1?: string | null; +} + +interface ProjectSeriesDialogProps { + open: boolean + onOpenChange: (open: boolean) => void + project: BiddingProjects | null +} + +export function ProjectSeriesDialog({ + open, + onOpenChange, + project, +}: ProjectSeriesDialogProps) { + const { toast } = useToast() + + const [projectSeries, setProjectSeries] = React.useState([]) + const [isLoading, setIsLoading] = React.useState(false) + + React.useEffect(() => { + async function loadItems() { + if (!project?.pspid) return; + + setIsLoading(true) + try { + const result = await getProjectSeriesForProject(project.pspid) + setProjectSeries(result) + } catch (error) { + console.error("프로젝트 시리즈 로드 오류:", error) + toast({ + title: "오류", + description: "프로젝트 시리즈 로드 실패", + variant: "destructive", + }) + } finally { + setIsLoading(false) + } + } + + if (open && project) { + loadItems() + } + }, [toast, project, open]) + + return ( + + + + + {project ? `시리즈 목록 - ${project.projNm || project.pspid}` : "시리즈 목록"} + + + {isLoading ? ( +
+ 로딩 중... +
+ ) : ( +
+ + + + 시리즈번호 + K/L 연도분기 + 도크코드 + 도크명 + SN공사번호 + SN공사명 + + + + {projectSeries && projectSeries.length > 0 ? ( + projectSeries.map((series) => ( + + {series.sersNo} + {series.scDt} + {series.klDt} + {series.lcDt} + {series.dlDt} + {series.dockNo} + {series.dockNm} + {series.projNo} + {series.post1} + + )) + ) : ( + + + 시리즈 데이터가 없습니다. + + + )} + +
+
+ )} +
+
+ ) +} \ No newline at end of file diff --git a/lib/bidding-projects/table/projects-table-columns.tsx b/lib/bidding-projects/table/projects-table-columns.tsx new file mode 100644 index 00000000..08530ff0 --- /dev/null +++ b/lib/bidding-projects/table/projects-table-columns.tsx @@ -0,0 +1,102 @@ +"use client" +import * as React from "react" +import { type DataTableRowAction } from "@/types/table" +import { type ColumnDef } from "@tanstack/react-table" +import { formatDate } from "@/lib/utils" +import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header" +import { BiddingProjects } from "@/db/schema" +import { bidProjectsColumnsConfig } from "@/config/bidProjectsColumnsConfig" +import { Button } from "@/components/ui/button" +import { ListFilter } from "lucide-react" // Import an icon for the button + +interface GetColumnsProps { + setRowAction: React.Dispatch | null>> +} + +/** + * tanstack table 컬럼 정의 (중첩 헤더 버전) + */ +export function getColumns({ setRowAction }: GetColumnsProps): ColumnDef[] { + // ---------------------------------------------------------------- + // 3) 일반 컬럼들을 "그룹"별로 묶어 중첩 columns 생성 + // ---------------------------------------------------------------- + // 3-1) groupMap: { [groupName]: ColumnDef[] } + const groupMap: Record[]> = {} + bidProjectsColumnsConfig.forEach((cfg) => { + // 만약 group가 없으면 "_noGroup" 처리 + const groupName = cfg.group || "_noGroup" + if (!groupMap[groupName]) { + groupMap[groupName] = [] + } + // child column 정의 + const childCol: ColumnDef = { + accessorKey: cfg.id, + enableResizing: true, + header: ({ column }) => ( + + ), + meta: { + excelHeader: cfg.excelHeader, + group: cfg.group, + type: cfg.type, + }, + cell: ({ row, cell }) => { + if (cfg.id === "createdAt" || cfg.id === "updatedAt") { + const dateVal = cell.getValue() as Date + return formatDate(dateVal) + } + return row.getValue(cfg.id) ?? "" + }, + } + groupMap[groupName].push(childCol) + }) + + // ---------------------------------------------------------------- + // 3-2) groupMap에서 실제 상위 컬럼(그룹)을 만들기 + // ---------------------------------------------------------------- + const nestedColumns: ColumnDef[] = [] + // 순서를 고정하고 싶다면 group 순서를 미리 정의하거나 sort해야 함 + // 여기서는 그냥 Object.entries 순서 + Object.entries(groupMap).forEach(([groupName, colDefs]) => { + if (groupName === "_noGroup") { + // 그룹 없음 → 그냥 최상위 레벨 컬럼 + nestedColumns.push(...colDefs) + } else { + // 상위 컬럼 + nestedColumns.push({ + id: groupName, + header: groupName, // "Basic Info", "Metadata" 등 + columns: colDefs, + }) + } + }) + + // Add action column + const actionColumn: ColumnDef = { + id: "actions", + header: "Actions", + cell: ({ row }) => { + return ( + + ) + }, + } + + // ---------------------------------------------------------------- + // 4) 최종 컬럼 배열: nestedColumns + actions + // ---------------------------------------------------------------- + return [ + ...nestedColumns, + actionColumn, // Add the action column + ] +} \ No newline at end of file diff --git a/lib/bidding-projects/table/projects-table-toolbar-actions.tsx b/lib/bidding-projects/table/projects-table-toolbar-actions.tsx new file mode 100644 index 00000000..ee2f8c4e --- /dev/null +++ b/lib/bidding-projects/table/projects-table-toolbar-actions.tsx @@ -0,0 +1,89 @@ +"use client" + +import * as React from "react" +import { type Table } from "@tanstack/react-table" +import { Download, RefreshCcw } from "lucide-react" +import { toast } from "sonner" + +import { exportTableToExcel } from "@/lib/export" +import { Button } from "@/components/ui/button" +import { BiddingProjects } from "@/db/schema" + +interface ItemsTableToolbarActionsProps { + table: Table +} + +export function ProjectTableToolbarActions({ table }: ItemsTableToolbarActionsProps) { + const [isLoading, setIsLoading] = React.useState(false) + + // 프로젝트 동기화 API 호출 함수 + const syncProjects = async () => { + try { + setIsLoading(true) + + // API 엔드포인트 호출 + const response = await fetch('/api/cron/bid-projects') + + if (!response.ok) { + const errorData = await response.json() + throw new Error(errorData.error || 'Failed to sync projects') + } + + const data = await response.json() + + // 성공 메시지 표시 + toast.success( + `Projects synced successfully! ${data.result.items} items processed.` + ) + + // 페이지 새로고침으로 테이블 데이터 업데이트 + window.location.reload() + } catch (error) { + console.error('Error syncing projects:', error) + toast.error( + error instanceof Error + ? error.message + : 'An error occurred while syncing projects' + ) + } finally { + setIsLoading(false) + } + } + + return ( +
+ + + {/** 4) Export 버튼 */} + +
+ ) +} \ No newline at end of file diff --git a/lib/bidding-projects/table/projects-table.tsx b/lib/bidding-projects/table/projects-table.tsx new file mode 100644 index 00000000..0e0c48f9 --- /dev/null +++ b/lib/bidding-projects/table/projects-table.tsx @@ -0,0 +1,156 @@ +"use client" + +import * as React from "react" +import type { + DataTableAdvancedFilterField, + DataTableFilterField, + DataTableRowAction, +} from "@/types/table" + +import { useDataTable } from "@/hooks/use-data-table" +import { DataTable } from "@/components/data-table/data-table" +import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar" + +import { getColumns } from "./projects-table-columns" +import { getBidProjectLists } from "../service" +import { BiddingProjects } from "@/db/schema" +import { ProjectTableToolbarActions } from "./projects-table-toolbar-actions" +import { ProjectSeriesDialog } from "./project-series-dialog" + +interface ItemsTableProps { + promises: Promise< + [ + Awaited>, + ] + > +} + +export function BidProjectsTable({ promises }: ItemsTableProps) { + + const [{ data, pageCount }] = + React.use(promises) + + const [rowAction, setRowAction] = + React.useState | null>(null) + + const columns = React.useMemo( + () => getColumns({ setRowAction }), + [setRowAction] + ) + + /** + * This component can render either a faceted filter or a search filter based on the `options` prop. + * + * @prop options - An array of objects, each representing a filter option. If provided, a faceted filter is rendered. If not, a search filter is rendered. + * + * Each `option` object has the following properties: + * @prop {string} label - The label for the filter option. + * @prop {string} value - The value for the filter option. + * @prop {React.ReactNode} [icon] - An optional icon to display next to the label. + * @prop {boolean} [withCount] - An optional boolean to display the count of the filter option. + */ + const filterFields: DataTableFilterField[] = [ + + ] + + /** + * Advanced filter fields for the data table. + * These fields provide more complex filtering options compared to the regular filterFields. + * + * Key differences from regular filterFields: + * 1. More field types: Includes 'text', 'multi-select', 'date', and 'boolean'. + * 2. Enhanced flexibility: Allows for more precise and varied filtering options. + * 3. Used with DataTableAdvancedToolbar: Enables a more sophisticated filtering UI. + * 4. Date and boolean types: Adds support for filtering by date ranges and boolean values. + */ + const advancedFilterFields: DataTableAdvancedFilterField[] = [ + { + id: "pspid", + label: "견적프로젝트번호", + type: "text", + // group: "Basic Info", + }, + { + id: "projNm", + label: "견적프로젝트명", + type: "text", + // group: "Basic Info", + }, + { + id: "sector", + label: "부문(S / M)", + type: "text", + }, + { + id: "kunnrNm", + label: "선주명", + type: "text", + }, + { + id: "cls1Nm", + label: "선급명", + type: "text", + }, + { + id: "ptypeNm", + label: "선종명", + type: "text", + }, + { + id: "estmPm", + label: "견적대표PM 성명", + type: "text", + }, + { + id: "createdAt", + label: "Created At", + type: "date", + // group: "Metadata",a + }, + { + id: "updatedAt", + label: "Updated At", + type: "date", + // group: "Metadata", + }, + ] + + + const { table } = useDataTable({ + data, + columns, + pageCount, + filterFields, + enablePinning: true, + enableAdvancedFilter: true, + initialState: { + sorting: [{ id: "createdAt", desc: true }], + columnPinning: { right: ["actions"] }, + }, + getRowId: (originalRow) => String(originalRow.pspid), + shallow: false, + clearOnDefault: true, + }) + + return ( + <> + + + + + + + setRowAction(null)} + project={rowAction?.row.original ?? null} + /> + + ) +} -- cgit v1.2.3