115 lines
3.1 KiB
TypeScript
115 lines
3.1 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import {
|
|
getCoreRowModel,
|
|
useReactTable,
|
|
type RowSelectionState,
|
|
} from "@tanstack/react-table";
|
|
import {
|
|
Button,
|
|
DataTable,
|
|
Dialog,
|
|
DialogTrigger,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
Input,
|
|
DialogFooter,
|
|
DialogClose,
|
|
} from "@common/components";
|
|
import {
|
|
UserOrganizationApi,
|
|
type SearchUser,
|
|
} from "@/entities/user-organization";
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import { columns } from "../model/additional-user-table-columns";
|
|
|
|
type AdditionalUserDialogProps = {
|
|
onAddUsers: (user: SearchUser[]) => void;
|
|
} & React.ComponentProps<typeof Dialog>;
|
|
|
|
function AdditionalUserDialog({
|
|
onAddUsers,
|
|
...props
|
|
}: AdditionalUserDialogProps) {
|
|
const [open, setOpen] = useState(false);
|
|
const [userNm, setUserNm] = useState("");
|
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
|
|
|
|
const { data: userList, refetch } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.USER_ORGANIZATION],
|
|
queryFn: () => UserOrganizationApi.searchUser({ userNm }),
|
|
enabled: false,
|
|
});
|
|
|
|
const table = useReactTable({
|
|
data: userList?.content ?? [],
|
|
columns,
|
|
state: { rowSelection },
|
|
getCoreRowModel: getCoreRowModel(),
|
|
onRowSelectionChange: setRowSelection,
|
|
});
|
|
|
|
const handleAddUsers = () => {
|
|
const selectedRows = table.getSelectedRowModel().rows;
|
|
const userData = selectedRows.map((row) => row.original);
|
|
if (!userData) return;
|
|
|
|
onAddUsers(userData);
|
|
table.setRowSelection({});
|
|
};
|
|
|
|
const handleCancel = () => {
|
|
setRowSelection({});
|
|
setUserNm("");
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (open) refetch();
|
|
}, [open]);
|
|
|
|
return (
|
|
<Dialog {...props} open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger asChild>
|
|
<Button size="sm" color="green">
|
|
사용자 추가
|
|
</Button>
|
|
</DialogTrigger>
|
|
<DialogContent className="max-w-[60rem]" aria-describedby={undefined}>
|
|
<DialogHeader>
|
|
<DialogTitle>콘텐츠 조회</DialogTitle>
|
|
</DialogHeader>
|
|
<div className="flex flex-col gap-4">
|
|
<div className="card card-border !m-0 flex w-full items-center gap-2">
|
|
<Input
|
|
placeholder="검색어를 입력하세요."
|
|
className="!text-[1.3rem]"
|
|
value={userNm}
|
|
onChange={(e) => setUserNm(e.target.value)}
|
|
/>
|
|
<Button onClick={() => refetch()}>
|
|
<i className="diveicon di-search" />
|
|
조회
|
|
</Button>
|
|
</div>
|
|
<div className="h-[40rem] w-full overflow-y-auto">
|
|
<DataTable table={table} />
|
|
</div>
|
|
</div>
|
|
<DialogFooter className="!justify-center">
|
|
<DialogClose asChild>
|
|
<Button color="white" onClick={handleCancel}>
|
|
취소
|
|
</Button>
|
|
</DialogClose>
|
|
<DialogClose asChild>
|
|
<Button onClick={handleAddUsers}>추가</Button>
|
|
</DialogClose>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
export { AdditionalUserDialog };
|