summaryrefslogtreecommitdiff
path: root/lib/project-gtc/table/project-gtc-table.tsx
blob: 6e529ccfd49aa7489ee92a6d4010905b0f2c408a (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
"use client";

import * as React from "react";
import { useRouter } from "next/navigation";
import { DataTable } from "@/components/data-table/data-table";
import { useDataTable } from "@/hooks/use-data-table";
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar";
import type {
  DataTableAdvancedFilterField,
  DataTableRowAction,
} from "@/types/table"
import { getProjectGtcList } from "../service";
import { getColumns } from "./project-gtc-table-columns";
import { DeleteGtcFileDialog } from "./delete-gtc-file-dialog";
import { UpdateGtcFileSheet } from "./update-gtc-file-sheet";
import { ProjectGtcTableToolbarActions } from "./project-gtc-table-toolbar-actions";
import { ProjectGtcView } from "@/db/schema";

interface ProjectGtcTableProps {
  promises: Promise<
    [
      Awaited<ReturnType<typeof getProjectGtcList>>,
    ]
  >
}

export function ProjectGtcTable({ promises }: ProjectGtcTableProps) {
  const router = useRouter();
  const [rowAction, setRowAction] =
    React.useState<DataTableRowAction<ProjectGtcView> | null>(null)

  const [{ data, pageCount }] =
    React.use(promises)

  // 컬럼 설정 - 외부 파일에서 가져옴
  const columns = React.useMemo(
    () => getColumns({ setRowAction }),
    [setRowAction]
  )

  // config 기반으로 필터 필드 설정
  const advancedFilterFields: DataTableAdvancedFilterField<ProjectGtcView>[] = [
    { id: "code", label: "프로젝트 코드", type: "text" },
    { id: "name", label: "프로젝트명", type: "text" },
    {
      id: "type", label: "프로젝트 타입", type: "select", options: [
        { label: "Ship", value: "ship" },
        { label: "Offshore", value: "offshore" },
        { label: "Other", value: "other" },
      ]
    },
    { id: "originalFileName", label: "GTC 파일명", type: "text" },
    { id: "projectCreatedAt", label: "프로젝트 생성일", type: "date" },
    { id: "gtcCreatedAt", label: "GTC 등록일", type: "date" },
  ];

  const { table } = useDataTable({
      data,
      columns,
      pageCount,
      enablePinning: true,
      enableAdvancedFilter: true,
      initialState: {
        sorting: [{ id: "projectCreatedAt", desc: true }],
        columnPinning: { right: ["actions"] },
      },
      getRowId: (originalRow) => String(originalRow.id),
      shallow: false,
      clearOnDefault: true,
    })

  return (
    <>
      <DataTable table={table}>
        <DataTableAdvancedToolbar
          table={table}
          filterFields={advancedFilterFields}
        >
          <ProjectGtcTableToolbarActions table={table} />
        </DataTableAdvancedToolbar>
      </DataTable>

      <DeleteGtcFileDialog
        open={rowAction?.type === "delete"}
        onOpenChange={() => setRowAction(null)}
        projects={rowAction?.row.original ? [rowAction?.row.original] : []}
        showTrigger={false}
        onSuccess={() => {
          router.refresh();
        }}
      />

      <UpdateGtcFileSheet
        open={rowAction?.type === "upload"}
        onOpenChange={() => setRowAction(null)}
        project={rowAction && rowAction.type === "upload" ? rowAction.row.original : null}
      />
    </>
  );
}