summaryrefslogtreecommitdiff
path: root/lib/docu-list-rule/code-groups/table/code-groups-table.tsx
blob: c10d3445794354f41131c537f760130ecbdba5c7 (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
101
102
103
104
105
106
107
108
109
110
111
"use client";
import * as React from "react";
import { useRouter } from "next/navigation";
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 type {
  DataTableAdvancedFilterField,
  DataTableFilterField,
  DataTableRowAction,
} from "@/types/table"
import { getColumns } from "./code-groups-table-columns";
import { DeleteCodeGroupsDialog } from "./delete-code-groups-dialog";
import { CodeGroupsEditSheet } from "./code-groups-edit-sheet";
import { CodeGroupsTableToolbarActions } from "./code-groups-table-toolbar";
import { codeGroups } from "@/db/schema/docu-list-rule";

interface CodeGroupsTableProps {
  promises?: Promise<[{ data: typeof codeGroups.$inferSelect[]; pageCount: number }] >;
}

export function CodeGroupsTable({ promises }: CodeGroupsTableProps) {
  const router = useRouter();
  const [rowAction, setRowAction] = React.useState<DataTableRowAction<typeof codeGroups.$inferSelect> | null>(null);

  const [{ data, pageCount }] = promises ? React.use(promises) : [{ data: [], pageCount: 0 }];

  const refreshData = React.useCallback(async () => {
    // 전체 페이지 새로고침 대신 router.refresh() 사용 (성능 개선)
    router.refresh();
  }, [router]);

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

  // 필터 필드 설정
  const filterFields: DataTableFilterField<typeof codeGroups.$inferSelect>[] = [];

  // 고급 필터 필드 설정
  const advancedFilterFields: DataTableAdvancedFilterField<typeof codeGroups.$inferSelect>[] = [
    { id: "groupId", label: "Group ID", type: "text" },
    { id: "description", label: "Description", type: "text" },
    { id: "codeFormat", label: "Code Format", type: "text" },
    {
      id: "controlType", label: "Control Type", type: "select", options: [
        { label: "Textbox", value: "textbox" },
        { label: "Combobox", value: "combobox" },
        { label: "Date", value: "date" },
        { label: "Number", value: "number" },
      ]
    },
    {
      id: "isActive", label: "Status", type: "select", options: [
        { label: "Active", value: "true" },
        { label: "Inactive", value: "false" },
      ]
    },
    { id: "createdAt", label: "Created At", type: "date" },
  ];

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

  return (
    <>
      <DataTable table={table}>
        <DataTableAdvancedToolbar
          table={table}
          filterFields={advancedFilterFields}
          shallow={false}
        >
          <CodeGroupsTableToolbarActions table={table} onSuccess={refreshData} />
        </DataTableAdvancedToolbar>
      </DataTable>

      <DeleteCodeGroupsDialog
        open={rowAction?.type === "delete"}
        onOpenChange={() => setRowAction(null)}
        codeGroups={rowAction?.row.original ? [rowAction?.row.original] : []}
        showTrigger={false}
        onSuccess={() => {
          rowAction?.row.toggleSelected(false)
          refreshData()
        }}
      />

      <CodeGroupsEditSheet
        open={rowAction?.type === "update"}
        onOpenChange={() => setRowAction(null)}
        data={rowAction?.row.original ?? null}
        onSuccess={refreshData}
      />
    </>
  );
}