164 lines
4.6 KiB
TypeScript
164 lines
4.6 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import {
|
|
keepPreviousData,
|
|
useQuery,
|
|
useQueryClient,
|
|
} from "@tanstack/react-query";
|
|
import {
|
|
getCoreRowModel,
|
|
type PaginationState,
|
|
type RowSelectionState,
|
|
} from "@tanstack/react-table";
|
|
import { useConfirm } from "@frontend-opensource/use-react-hooks";
|
|
import { Button, Input, Label } from "@common/components";
|
|
import { GroupUsersApi } from "@/entities/group-user";
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import { useAlertStore, useDataTable } from "@/shared/lib";
|
|
import { columns } from "../model/group-users-table-columns";
|
|
import { AddUserDialog } from "./add-user-dialog";
|
|
|
|
type GroupUsersSectionProps = {
|
|
groupId?: string;
|
|
};
|
|
|
|
function GroupUsersSection({ groupId }: GroupUsersSectionProps) {
|
|
const queryClient = useQueryClient();
|
|
const { confirm } = useConfirm();
|
|
const alert = useAlertStore((state) => state.setMessage);
|
|
|
|
const [pagination, setPagination] = useState<PaginationState>({
|
|
pageIndex: 0,
|
|
pageSize: 10,
|
|
});
|
|
|
|
const [userNm, setUserNm] = useState("");
|
|
|
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
|
|
|
|
const query = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.GROUP_USERS, groupId, pagination, userNm],
|
|
queryFn: () =>
|
|
GroupUsersApi.findPageGroupUser({
|
|
keyword: groupId,
|
|
userNm: userNm,
|
|
page: pagination.pageIndex,
|
|
size: pagination.pageSize,
|
|
}),
|
|
enabled: !!groupId,
|
|
placeholderData: keepPreviousData,
|
|
});
|
|
|
|
const handleSubmit = (e: React.SubmitEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
setPagination({
|
|
...pagination,
|
|
pageIndex: 0,
|
|
});
|
|
// userNm이 이미 state에 반영되어 있으므로 별도 처리 불필요
|
|
// 쿼리키에 userNm이 포함되어 있어 자동 refetch됨
|
|
};
|
|
|
|
const handleReset = () => {
|
|
setUserNm("");
|
|
};
|
|
|
|
const { tableComponent, tableInstance: table } = useDataTable({
|
|
query,
|
|
tableOptions: {
|
|
columns,
|
|
state: { pagination, rowSelection },
|
|
manualPagination: true,
|
|
enableRowSelection: true,
|
|
enableMultiRowSelection: true,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
onPaginationChange: setPagination,
|
|
onRowSelectionChange: setRowSelection,
|
|
},
|
|
dataTableOptions: {
|
|
viewPagination: true,
|
|
},
|
|
});
|
|
|
|
const handleDeleteSelected = async () => {
|
|
const selectedRows = table.getSelectedRowModel().rows.map((row) => ({
|
|
groupId: row.original.groupId,
|
|
userUnqId: row.original.userUnqId,
|
|
}));
|
|
if (selectedRows.length === 0 || !groupId) return;
|
|
if (!(await confirm("사용자를 삭제하시겠습니까?"))) return;
|
|
|
|
try {
|
|
const usersToDelete = selectedRows.map((row) => ({
|
|
mode: "D",
|
|
groupId: row.groupId,
|
|
userUnqId: row.userUnqId,
|
|
}));
|
|
|
|
await GroupUsersApi.saveGroupUser(usersToDelete);
|
|
queryClient.invalidateQueries({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.GROUP_USERS, groupId],
|
|
});
|
|
alert("사용자 삭제를 완료했습니다.", () => {
|
|
setRowSelection({});
|
|
});
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
setPagination({ ...pagination, pageIndex: 0 });
|
|
setRowSelection({});
|
|
}, [groupId]);
|
|
|
|
return (
|
|
<div className="flex-1">
|
|
<div className="card card-border">
|
|
<form onSubmit={handleSubmit} className="flex items-center gap-2 p-2">
|
|
<Label htmlFor="" className="mr-2 text-label">
|
|
이름
|
|
</Label>
|
|
<Input
|
|
className="h-[3.2rem] w-[20rem] rounded-sm !text-[1.3rem]"
|
|
value={userNm}
|
|
onChange={(e) => setUserNm(e.target.value)}
|
|
/>
|
|
<Button className="h-[3.2rem]" type="submit">
|
|
조회
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
color="white"
|
|
className="h-[3.2rem]"
|
|
onClick={handleReset}
|
|
>
|
|
초기화
|
|
</Button>
|
|
</form>
|
|
</div>
|
|
<div className="card card-border">
|
|
<div className="card-header">
|
|
<h3 className="text-2xl font-medium text-label">그룹 사용자 목록</h3>
|
|
<div className="flex gap-2">
|
|
<AddUserDialog groupId={groupId} />
|
|
<Button
|
|
color="red"
|
|
disabled={table.getSelectedRowModel().rows.length === 0}
|
|
onClick={handleDeleteSelected}
|
|
>
|
|
선택삭제
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
<div className="card !m-4 h-[calc(100vh-25.5rem)] overflow-auto !p-0">
|
|
{tableComponent}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export { GroupUsersSection };
|