refactor: useDataTable 적용

This commit is contained in:
2025-06-18 17:00:18 +09:00
parent 6d6892973f
commit 3b7f8bcca7
34 changed files with 718 additions and 709 deletions
@@ -1,8 +1,4 @@
import {
getCoreRowModel,
useReactTable,
type ColumnDef,
} from "@tanstack/react-table";
import { getCoreRowModel, type ColumnDef } from "@tanstack/react-table";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import {
Dialog,
@@ -11,7 +7,6 @@ import {
DialogHeader,
DialogTitle,
Button,
DataTable,
Switch,
} from "@common/components";
import {
@@ -20,7 +15,7 @@ import {
type RoleAuthorizationsByRoleId,
} from "@/entities/role-authorization";
import { ENDPOINT } from "@/shared/config";
import { useAlertStore, useMutationWrapper } from "@/shared/lib";
import { useAlertStore, useMutationWrapper, useDataTable } from "@/shared/lib";
type MappingRoleDialogProps = {
roleId: string;
@@ -37,7 +32,7 @@ function MappingRoleDialog({
const queryClient = useQueryClient();
const alert = useAlertStore((state) => state.setMessage);
const { data: authorizationList } = useQuery({
const query = useQuery({
queryKey: [ENDPOINT.USER_SERVICE.AUTHORIZATIONS],
queryFn: () =>
RoleAuthorizationsApi.getRoleAuthorizationsListByRoleId({
@@ -63,7 +58,7 @@ function MappingRoleDialog({
const handleApplyAll = (authYn: "Y" | "N") => {
mutate(
authorizationList?.content.map((authorization) => ({
query.data?.content.map((authorization) => ({
roleId,
authorizationNo: authorization.authorizationNo,
authYn,
@@ -71,10 +66,12 @@ function MappingRoleDialog({
);
};
const table = useReactTable({
data: authorizationList?.content ?? [],
columns: createColumns(isPending, applyRoleAuthorization),
getCoreRowModel: getCoreRowModel(),
const { tableComponent } = useDataTable({
query,
tableOptions: {
columns: createColumns(isPending, applyRoleAuthorization),
getCoreRowModel: getCoreRowModel(),
},
});
return (
@@ -95,7 +92,7 @@ function MappingRoleDialog({
일괄해제
</Button>
</div>
<DataTable table={table} />
{tableComponent}
</div>
</DialogContent>
</Dialog>
+24 -23
View File
@@ -3,14 +3,11 @@
import { useState } from "react";
import { useRouter } from "next/navigation";
import { useQuery } from "@tanstack/react-query";
import {
getCoreRowModel,
useReactTable,
type PaginationState,
} from "@tanstack/react-table";
import { Button, DataTable, Input, Label } from "@common/components";
import { getCoreRowModel, type PaginationState } from "@tanstack/react-table";
import { Button, Input, Label } from "@common/components";
import { RoleApi, type Role, type RoleParams } from "@/entities/role";
import { ENDPOINT } from "@/shared/config";
import { useDataTable } from "@/shared/lib";
import { columns } from "../model/table-columns";
function RolePage() {
@@ -24,7 +21,7 @@ function RolePage() {
pageSize: 10,
});
const { data: roles } = useQuery({
const query = useQuery({
queryKey: [ENDPOINT.USER_SERVICE.ROLES, pagination],
queryFn: () =>
RoleApi.getRoles({
@@ -34,24 +31,30 @@ function RolePage() {
}),
});
const table = useReactTable({
data: roles?.content ?? [],
columns,
state: {
pagination,
const handleRowClick = (row: Role) => {
router.push(`/role/register/${row.roleId}`);
};
const { tableComponent } = useDataTable({
query,
tableOptions: {
columns,
state: {
pagination,
},
manualPagination: true,
getCoreRowModel: getCoreRowModel(),
onPaginationChange: setPagination,
},
dataTableOptions: {
onRowClick: handleRowClick,
viewPagination: true,
},
rowCount: roles?.totalElements ?? 0,
manualPagination: true,
getCoreRowModel: getCoreRowModel(),
onPaginationChange: setPagination,
});
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
};
const handleRowClick = (row: Role) => {
router.push(`/role/register/${row.roleId}`);
query.refetch();
};
return (
@@ -84,9 +87,7 @@ function RolePage() {
</div>
</div>
</div>
<div className="card !m-[1.2rem] !p-0">
<DataTable table={table} onRowClick={handleRowClick} viewPagination />
</div>
<div className="card !m-[1.2rem] !p-0">{tableComponent}</div>
</div>
</div>
);