summaryrefslogtreecommitdiff
path: root/lib/vendor-document/table
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vendor-document/table')
-rw-r--r--lib/vendor-document/table/doc-table-column.tsx150
-rw-r--r--lib/vendor-document/table/doc-table-toolbar-actions.tsx57
-rw-r--r--lib/vendor-document/table/doc-table.tsx124
3 files changed, 331 insertions, 0 deletions
diff --git a/lib/vendor-document/table/doc-table-column.tsx b/lib/vendor-document/table/doc-table-column.tsx
new file mode 100644
index 00000000..e53b03b9
--- /dev/null
+++ b/lib/vendor-document/table/doc-table-column.tsx
@@ -0,0 +1,150 @@
+"use client"
+
+import * as React from "react"
+import { ColumnDef } from "@tanstack/react-table"
+import { formatDate, formatDateTime } from "@/lib/utils"
+import { Checkbox } from "@/components/ui/checkbox"
+import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
+import { DataTableRowAction } from "@/types/table"
+import { VendorDocumentsView } from "@/db/schema/vendorDocu"
+
+interface GetColumnsProps {
+ setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<VendorDocumentsView> | null>>
+}
+
+export function getColumns({
+ setRowAction,
+}: GetColumnsProps): ColumnDef<VendorDocumentsView>[] {
+ return [
+ {
+ id: "select",
+ // Remove the "Select all" checkbox in header since we're doing single-select
+ header: () => <span className="sr-only">Select</span>,
+ cell: ({ row, table }) => (
+ <Checkbox
+ checked={row.getIsSelected()}
+ onCheckedChange={(value) => {
+ // If selecting this row
+ if (value) {
+ // First deselect all rows (to ensure single selection)
+ table.toggleAllRowsSelected(false)
+ // Then select just this row
+ row.toggleSelected(true)
+ // Trigger the same action that was in the "Select" button
+ setRowAction({ row, type: "select" })
+ } else {
+ // Just deselect this row
+ row.toggleSelected(false)
+ }
+ }}
+ aria-label="Select row"
+ className="translate-y-0.5"
+ />
+ ),
+ enableSorting: false,
+ enableHiding: false,
+ enableResizing: false,
+ size: 40,
+ minSize: 40,
+ maxSize: 40,
+ },
+
+ {
+ accessorKey: "docNumber",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Doc Number" />
+ ),
+ cell: ({ row }) => <div>{row.getValue("docNumber")}</div>,
+ meta: {
+ excelHeader: "Doc Number"
+ },
+ enableResizing: true,
+ minSize: 100,
+ size: 160,
+ },
+ {
+ accessorKey: "title",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Doc title" />
+ ),
+ cell: ({ row }) => <div>{row.getValue("title")}</div>,
+ meta: {
+ excelHeader: "Doc title"
+ },
+ enableResizing: true,
+ minSize: 100,
+ size: 160,
+ },
+ {
+ accessorKey: "latestStageName",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Latest Stage Name" />
+ ),
+ cell: ({ row }) => <div>{row.getValue("latestStageName")}</div>,
+ meta: {
+ excelHeader: "Latest Stage Name"
+ },
+ enableResizing: true,
+ minSize: 100,
+ size: 160,
+ },
+ {
+ accessorKey: "latestStagePlanDate",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Latest Stage Plan Date" />
+ ),
+ cell: ({ cell }) => {
+ const value = cell.getValue();
+ return value ? formatDate(value as Date) : "";
+ }, meta: {
+ excelHeader: "Latest Stage Plan Date"
+ },
+ enableResizing: true,
+ minSize: 100,
+ size: 160,
+ },
+ {
+ accessorKey: "latestStageActualDate",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Latest Stage Actual Date" />
+ ),
+ cell: ({ cell }) => {
+ const value = cell.getValue();
+ return value ? formatDate(value as Date) : "";
+ }, meta: {
+ excelHeader: "Latest Stage Actual Date"
+ },
+ enableResizing: true,
+ minSize: 100,
+ size: 160,
+ },
+ {
+ accessorKey: "latestRevision",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Latest Revision" />
+ ),
+ cell: ({ row }) => <div>{row.getValue("latestRevision")}</div>,
+ meta: {
+ excelHeader: "Latest Revision"
+ },
+ enableResizing: true,
+ minSize: 100,
+ size: 160,
+ },
+
+ {
+ accessorKey: "updatedAt",
+ header: ({ column }) => (
+ <DataTableColumnHeaderSimple column={column} title="Updated At" />
+ ),
+ cell: ({ cell }) => formatDateTime(cell.getValue() as Date),
+ meta: {
+ excelHeader: "updated At"
+ },
+ enableResizing: true,
+ minSize: 120,
+ size: 180,
+ },
+ // The "actions" column has been removed
+ ]
+} \ No newline at end of file
diff --git a/lib/vendor-document/table/doc-table-toolbar-actions.tsx b/lib/vendor-document/table/doc-table-toolbar-actions.tsx
new file mode 100644
index 00000000..cf4aa7c1
--- /dev/null
+++ b/lib/vendor-document/table/doc-table-toolbar-actions.tsx
@@ -0,0 +1,57 @@
+"use client"
+
+import * as React from "react"
+import { type Table } from "@tanstack/react-table"
+import { Download, Send, Upload } from "lucide-react"
+import { toast } from "sonner"
+
+import { exportTableToExcel } from "@/lib/export"
+import { Button } from "@/components/ui/button"
+import { VendorDocumentsView } from "@/db/schema/vendorDocu"
+
+
+interface DocTableToolbarActionsProps {
+ table: Table<VendorDocumentsView>
+}
+
+export function DocTableToolbarActions({ table }: DocTableToolbarActionsProps) {
+
+
+ return (
+ <div className="flex items-center gap-2">
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() =>
+ exportTableToExcel(table, {
+ filename: "tasks",
+ excludeColumns: ["select", "actions"],
+ })
+ }
+ className="gap-2"
+ >
+ <Download className="size-4" aria-hidden="true" />
+ <span className="hidden sm:inline">Export</span>
+ </Button>
+
+
+ <Button
+ size="sm"
+ className="gap-2"
+ >
+ <Upload className="size-4" aria-hidden="true" />
+ <span className="hidden sm:inline">Bulk File Upload</span>
+ </Button>
+
+ <Button
+ variant="samsung"
+ size="sm"
+ className="gap-2"
+ >
+ <Send className="size-4" aria-hidden="true" />
+ <span className="hidden sm:inline">Send to SHI</span>
+ </Button>
+
+ </div>
+ )
+} \ No newline at end of file
diff --git a/lib/vendor-document/table/doc-table.tsx b/lib/vendor-document/table/doc-table.tsx
new file mode 100644
index 00000000..dfd906fa
--- /dev/null
+++ b/lib/vendor-document/table/doc-table.tsx
@@ -0,0 +1,124 @@
+"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 { getColumns } from "./doc-table-column"
+import { getVendorDocumentLists } from "../service"
+import { VendorDocumentsView } from "@/db/schema/vendorDocu"
+import { useEffect } from "react"
+import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar"
+import { DocTableToolbarActions } from "./doc-table-toolbar-actions"
+
+interface DocumentListTableProps {
+ promises: Promise<[Awaited<ReturnType<typeof getVendorDocumentLists>>]>
+ selectedPackageId: number
+ onSelectDocument?: (document: VendorDocumentsView | null) => void
+}
+
+export function DocumentListTable({
+ promises,
+ selectedPackageId,
+ onSelectDocument
+}: DocumentListTableProps) {
+ // 1) 데이터를 가져옴 (server component -> use(...) pattern)
+ const [{ data, pageCount }] = React.use(promises)
+
+ console.log(data)
+ const [rowAction, setRowAction] = React.useState<DataTableRowAction<VendorDocumentsView> | null>(null)
+
+ // 3) 행 액션 처리
+ useEffect(() => {
+ if (rowAction) {
+ // 액션 유형에 따라 처리
+ switch (rowAction.type) {
+ case "select":
+ // 선택된 문서 처리
+ if (onSelectDocument) {
+ onSelectDocument(rowAction.row.original)
+ }
+ break;
+ case "update":
+ // 업데이트 처리 로직
+ console.log("Update document:", rowAction.row.original)
+ break;
+ case "delete":
+ // 삭제 처리 로직
+ console.log("Delete document:", rowAction.row.original)
+ break;
+ }
+
+ // 액션 처리 후 rowAction 초기화
+ setRowAction(null)
+ }
+ }, [rowAction, onSelectDocument])
+
+ const columns = React.useMemo(
+ () => getColumns({ setRowAction }),
+ [setRowAction]
+ )
+
+ // Filter fields
+ const filterFields: DataTableFilterField<VendorDocumentsView>[] = []
+
+ const advancedFilterFields: DataTableAdvancedFilterField<VendorDocumentsView>[] = [
+ {
+ id: "docNumber",
+ label: "Doc Number",
+ type: "text",
+ },
+ {
+ id: "title",
+ label: "Doc Title",
+ type: "text",
+ },
+ {
+ id: "createdAt",
+ label: "Created at",
+ type: "date",
+ },
+ {
+ id: "updatedAt",
+ label: "Updated at",
+ type: "date",
+ },
+ ]
+
+ // useDataTable 훅으로 react-table 구성
+ const { table } = useDataTable({
+ data: data, // <-- 여기서 tableData 사용
+ columns,
+ pageCount,
+ filterFields,
+ enablePinning: true,
+ enableAdvancedFilter: true,
+ initialState: {
+ sorting: [{ id: "createdAt", desc: true }],
+ columnPinning: { right: ["actions"] },
+ },
+ getRowId: (originalRow) => String(originalRow.id),
+ shallow: false,
+ clearOnDefault: true,
+ columnResizeMode: "onEnd",
+
+ })
+ return (
+ <>
+ <DataTable table={table} >
+ <DataTableAdvancedToolbar
+ table={table}
+ filterFields={advancedFilterFields}
+ shallow={false}
+ >
+ <DocTableToolbarActions table={table}/>
+ </DataTableAdvancedToolbar>
+ </DataTable>
+ </>
+ )
+} \ No newline at end of file