refactor: useDataTable 적용
This commit is contained in:
@@ -7,7 +7,6 @@ import { useConfirm } from "@frontend-opensource/use-react-hooks";
|
||||
import { keepPreviousData, useQuery } from "@tanstack/react-query";
|
||||
import {
|
||||
getCoreRowModel,
|
||||
useReactTable,
|
||||
type PaginationState,
|
||||
type RowSelectionState,
|
||||
} from "@tanstack/react-table";
|
||||
@@ -21,7 +20,6 @@ import {
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
DataTable,
|
||||
} from "@common/components";
|
||||
import {
|
||||
PostApi,
|
||||
@@ -32,7 +30,12 @@ import {
|
||||
} from "@/entities/post";
|
||||
import { BoardApi } from "@/entities/board";
|
||||
import { AuthorityButton } from "@/entities/menu-authority";
|
||||
import { useAlertStore, useMutationWrapper, usePrompt } from "@/shared/lib";
|
||||
import {
|
||||
useAlertStore,
|
||||
useMutationWrapper,
|
||||
usePrompt,
|
||||
useDataTable,
|
||||
} from "@/shared/lib";
|
||||
import { ENDPOINT } from "@/shared/config";
|
||||
import { columns } from "../model/table-columns";
|
||||
import { MovePostDialog } from "./move-post-dialog";
|
||||
@@ -63,7 +66,7 @@ function RecentPage() {
|
||||
});
|
||||
const [isDeletePostData, setIsDeletePostData] = useState(false);
|
||||
|
||||
const { data: recentPostList, refetch } = useQuery({
|
||||
const query = useQuery({
|
||||
queryKey: [ENDPOINT.BOARD_SERVICE.POSTS, pagination],
|
||||
queryFn: () =>
|
||||
PostApi.getRecentPostList({
|
||||
@@ -82,23 +85,34 @@ function RecentPage() {
|
||||
const { mutate: movePost } = useMutationWrapper({
|
||||
mutationFn: (data: MovePost[]) => PostApi.movePost(data),
|
||||
onSuccess: () => {
|
||||
alert("이동이 완료되었습니다.", refetch);
|
||||
alert("이동이 완료되었습니다.", query.refetch);
|
||||
},
|
||||
});
|
||||
|
||||
const table = useReactTable({
|
||||
data: recentPostList?.content ?? [],
|
||||
columns,
|
||||
state: {
|
||||
pagination,
|
||||
rowSelection,
|
||||
const handleRowClick = (row: RecentPost) => {
|
||||
router.push(
|
||||
`/bbs/${row.bbsTypeCd.toLowerCase()}/${row.bbsId}/${row.pstNo}`,
|
||||
);
|
||||
};
|
||||
|
||||
const { tableComponent, tableInstance: table } = useDataTable({
|
||||
query,
|
||||
tableOptions: {
|
||||
columns,
|
||||
state: {
|
||||
pagination,
|
||||
rowSelection,
|
||||
},
|
||||
manualPagination: true,
|
||||
enableMultiRowSelection: true,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
onPaginationChange: setPagination,
|
||||
onRowSelectionChange: setRowSelection,
|
||||
},
|
||||
dataTableOptions: {
|
||||
onRowClick: handleRowClick,
|
||||
viewPagination: true,
|
||||
},
|
||||
rowCount: recentPostList?.totalElements ?? 0,
|
||||
manualPagination: true,
|
||||
enableMultiRowSelection: true,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
onPaginationChange: setPagination,
|
||||
onRowSelectionChange: setRowSelection,
|
||||
});
|
||||
|
||||
const isSelectedRow = table.getFilteredSelectedRowModel().rows.length > 0;
|
||||
@@ -106,13 +120,7 @@ function RecentPage() {
|
||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
setIsDeletePostData(recentPostParams.delYn === "Y");
|
||||
refetch();
|
||||
};
|
||||
|
||||
const handleRowClick = (row: RecentPost) => {
|
||||
router.push(
|
||||
`/bbs/${row.bbsTypeCd.toLowerCase()}/${row.bbsId}/${row.pstNo}`,
|
||||
);
|
||||
query.refetch();
|
||||
};
|
||||
|
||||
const handleRecentPostParamsChange =
|
||||
@@ -160,7 +168,7 @@ function RecentPage() {
|
||||
delRsn,
|
||||
}));
|
||||
PostApi.softDeletePost(selectedPosts);
|
||||
alert("삭제가 완료되었습니다.", refetch);
|
||||
alert("삭제가 완료되었습니다.", query.refetch);
|
||||
setRowSelection({});
|
||||
};
|
||||
|
||||
@@ -177,7 +185,7 @@ function RecentPage() {
|
||||
pstNo: row.original.pstNo,
|
||||
}));
|
||||
PostApi.hardDeletePost(selectedPosts);
|
||||
alert("삭제가 완료되었습니다.", refetch);
|
||||
alert("삭제가 완료되었습니다.", query.refetch);
|
||||
setRowSelection({});
|
||||
}
|
||||
};
|
||||
@@ -191,7 +199,7 @@ function RecentPage() {
|
||||
pstNo: row.original.pstNo,
|
||||
}));
|
||||
PostApi.restorePost(selectedPosts);
|
||||
alert("복원이 완료되었습니다.", refetch);
|
||||
alert("복원이 완료되었습니다.", query.refetch);
|
||||
setRowSelection({});
|
||||
}
|
||||
};
|
||||
@@ -332,9 +340,7 @@ function RecentPage() {
|
||||
</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>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user