110 lines
3.1 KiB
TypeScript
110 lines
3.1 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { getCoreRowModel, type RowSelectionState } from "@tanstack/react-table";
|
|
import {
|
|
Button,
|
|
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 { useDataTable } from "@/shared/lib";
|
|
import { columns } from "../model/add-user-table-columns";
|
|
|
|
type AddUserDialogProps = {
|
|
onAddUsers: (user: SearchUser[]) => void;
|
|
} & React.ComponentProps<typeof Dialog>;
|
|
|
|
function AddUserDialog({ onAddUsers, ...props }: AddUserDialogProps) {
|
|
const [open, setOpen] = useState(false);
|
|
const [userNm, setUserNm] = useState("");
|
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
|
|
|
|
const query = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.USER_ORGANIZATION],
|
|
queryFn: () => UserOrganizationApi.searchUser({ userNm }),
|
|
enabled: false,
|
|
});
|
|
|
|
const { tableComponent, tableInstance: table } = useDataTable({
|
|
query,
|
|
tableOptions: {
|
|
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) query.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={() => query.refetch()}>
|
|
<i className="diveicon di-search" />
|
|
조회
|
|
</Button>
|
|
</div>
|
|
<div className="h-[40rem] w-full overflow-y-auto">
|
|
{tableComponent}
|
|
</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 { AddUserDialog };
|