243 lines
7.6 KiB
TypeScript
243 lines
7.6 KiB
TypeScript
'use client'
|
|
|
|
import { useMemo, useState } from "react";
|
|
import {Button} from "@/components/ui/button";
|
|
import {Input} from "@/components/ui/input";
|
|
import BodyTitle from "@/components/layout/BodyTitle";
|
|
import { DataTable } from "@/components/data-table/DataTable";
|
|
import { getUserColumns } from "@/components/table-column/UserTableColumn";
|
|
import UserUpsertDialog from "@/components/dialog/UserUpsertDialog";
|
|
import {UserInfo, UserInfoSaveRequest} from "@/types/user";
|
|
import UserUploadDialog from "@/components/dialog/UserUploadDialog";
|
|
import {useDialog} from "@/hooks/useDialog";
|
|
import {downloadBase64File, downloadFile} from "@/lib/utils";
|
|
import {KriMultiSelect} from "@/components/common/KriAutoComplete";
|
|
import {useAuth} from "@/hooks/useAuth";
|
|
import {MultiSelectOption} from "@/components/ui/multi-select";
|
|
import {useMounted} from "@/hooks/useMounted";
|
|
import {LoadingOverlay} from "@/components/common/LoadingOverlay";
|
|
import {DEFAULT_COMPANY_NAME, EXTERNAL_USER_ROLE} from "@/const/userConst";
|
|
import {
|
|
useRoleListQuery,
|
|
useUserCompaniesQuery,
|
|
useUsersQuery,
|
|
useUserDetailQuery,
|
|
useCheckUserIdDuplicateMutation,
|
|
useUpsertUserMutation,
|
|
useDeleteUserMutation,
|
|
useUploadUsersExcelMutation,
|
|
useDownloadUserExcelTemplateMutation,
|
|
} from "@/query/user";
|
|
|
|
const UserManagePage = () => {
|
|
const { showDialog } = useDialog();
|
|
const mounted = useMounted();
|
|
|
|
const { auth } = useAuth();
|
|
const roleQuery = useRoleListQuery();
|
|
const cmpnQuery = useUserCompaniesQuery();
|
|
const usersQuery = useUsersQuery();
|
|
|
|
const checkDupMutation = useCheckUserIdDuplicateMutation();
|
|
const upsertMutation = useUpsertUserMutation();
|
|
const deleteMutation = useDeleteUserMutation();
|
|
const uploadMutation = useUploadUsersExcelMutation();
|
|
const downloadTemplateMutation = useDownloadUserExcelTemplateMutation();
|
|
|
|
const [editUserId, setEditUserId] = useState<string | null>(null);
|
|
const userDetailQuery = useUserDetailQuery(editUserId ?? "", !!editUserId);
|
|
|
|
const userRoleArray: MultiSelectOption[] = useMemo(() => {
|
|
const roles = roleQuery.data ?? [];
|
|
return roles.map((r) => ({ value: String(r.roleCd), label: r.roleNm }));
|
|
}, [roleQuery.data]);
|
|
|
|
const userCmpnArray = cmpnQuery.data ?? [];
|
|
const userList = usersQuery.data ?? [];
|
|
|
|
const [searchText, setSearchText] = useState('');
|
|
const [searchCmpn, setSearchCmpn] = useState<string[]>([]);
|
|
const [searchRole, setSearchRole] = useState<string[]>([]);
|
|
|
|
const [dialogMode, setDialogMode] = useState<'create' | 'edit'>('create')
|
|
|
|
const viewList = useMemo(() => {
|
|
let temp = userList;
|
|
|
|
const keyword = searchText.trim();
|
|
if (keyword) {
|
|
const kw = keyword.toLowerCase();
|
|
temp = temp.filter((row) =>
|
|
[row.userId, row.userName].some((v) => (v ?? "").toLowerCase().includes(kw))
|
|
);
|
|
}
|
|
|
|
if (searchCmpn.length > 0) {
|
|
temp = temp.filter((row) => searchCmpn.includes(row.userCmpn));
|
|
}
|
|
|
|
if (searchRole.length > 0) {
|
|
temp = temp.filter((row) => searchRole.includes(String(row.userRole)));
|
|
}
|
|
|
|
return temp;
|
|
}, [userList, searchText, searchCmpn, searchRole]);
|
|
|
|
// 등록 버튼 클릭
|
|
const handleCreateClick = () => {
|
|
setDialogMode("create");
|
|
setEditUserId(null);
|
|
showDialog("userUpsertDialog");
|
|
};
|
|
|
|
// 중복 체크
|
|
const handleCheckDuplicate = (userId: string) => {
|
|
return checkDupMutation.mutateAsync(userId);
|
|
};
|
|
|
|
|
|
const handleSubmit = async (data: UserInfoSaveRequest): Promise<void> => {
|
|
await upsertMutation.mutateAsync({ mode: dialogMode, data });
|
|
|
|
if (dialogMode === "create") {
|
|
alert("등록 됐습니다.");
|
|
} else {
|
|
alert("수정 됐습니다.");
|
|
}
|
|
};
|
|
|
|
const handleEdit = (userId: string) => {
|
|
setDialogMode("edit");
|
|
setEditUserId(userId);
|
|
showDialog("userUpsertDialog");
|
|
};
|
|
|
|
const handleDelete = async (userId: string) => {
|
|
if (!confirm("정말 삭제하시겠습니까?")) return;
|
|
|
|
await deleteMutation.mutateAsync(userId);
|
|
alert("삭제 됐습니다.");
|
|
};
|
|
|
|
const uploadFile = async (file: File) => {
|
|
const res = await uploadMutation.mutateAsync(file);
|
|
|
|
if (res.hasErrorFile && res.errorFileBase64) {
|
|
downloadBase64File(res.errorFileBase64, res.errorFileName ?? "error.xlsx");
|
|
}
|
|
else{
|
|
alert(`${res.successCount}개의 계정을 생성했습니다.`);
|
|
}
|
|
};
|
|
|
|
const downloadTemplate = async () => {
|
|
const buffer = await downloadTemplateMutation.mutateAsync();
|
|
|
|
if (buffer.byteLength > 0) {
|
|
const blob = new Blob([buffer], {
|
|
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
});
|
|
downloadFile(blob, "사용자 업로드 양식.xlsx");
|
|
alert("다운로드 됐습니다.");
|
|
} else {
|
|
console.log("빈 파일입니다.");
|
|
}
|
|
};
|
|
|
|
const columns = getUserColumns(
|
|
{
|
|
onEdit: handleEdit,
|
|
onDelete: handleDelete
|
|
},
|
|
userRoleArray
|
|
);
|
|
|
|
const isInternal =
|
|
mounted &&
|
|
auth.user?.userRole != null &&
|
|
auth.user.userRole < EXTERNAL_USER_ROLE
|
|
|
|
const userCmpn = auth.user?.userCmpn ?? DEFAULT_COMPANY_NAME;
|
|
|
|
if (!mounted) return <LoadingOverlay force />;
|
|
|
|
return (
|
|
<div className="space-y-6 content-text-sm">
|
|
<BodyTitle titleText={"계정 관리"}/>
|
|
|
|
<div className="flex gap-2 items-center">
|
|
<div className="flex flex-1 items-center gap-2">
|
|
<Input
|
|
placeholder="면접원 ID Or 면접원 이름"
|
|
className="w-70 h-9 bg-white"
|
|
value={searchText}
|
|
onChange={(e) => setSearchText(e.target.value)}
|
|
/>
|
|
{
|
|
isInternal &&
|
|
<KriMultiSelect
|
|
items={userCmpnArray}
|
|
value={searchCmpn}
|
|
onChange={setSearchCmpn}
|
|
placeholder="소속 회사"
|
|
widthClassName="w-70"
|
|
heightClassName="h-9"
|
|
/>
|
|
}
|
|
<KriMultiSelect
|
|
items={userRoleArray}
|
|
value={searchRole}
|
|
onChange={setSearchRole}
|
|
getValue={(item) => item.value}
|
|
getLabel={(item) => item.label}
|
|
placeholder="사용자 권한"
|
|
widthClassName="w-70"
|
|
heightClassName="h-9"
|
|
/>
|
|
</div>
|
|
<Button
|
|
className="content-text-lg"
|
|
onClick={handleCreateClick}
|
|
>
|
|
면접원 등록
|
|
</Button>
|
|
<Button
|
|
onClick={() => showDialog('userUploadDialog')}
|
|
className="gap-2 bg-green-600 hover:bg-green-700 content-text-lg"
|
|
>
|
|
면접원 일괄 등록
|
|
</Button>
|
|
</div>
|
|
|
|
<div className="bg-white rounded-lg border border-gray-200 overflow-hidden">
|
|
<DataTable
|
|
columns={columns}
|
|
data={viewList}
|
|
isClient={true}
|
|
selectMode='single'
|
|
defaultPageSize={10}
|
|
pageSizeOptions={[10, 20, 30, 50]}
|
|
getRowId={(row) => row.userId} // row.id = userId
|
|
/>
|
|
</div>
|
|
|
|
<UserUpsertDialog
|
|
mode={dialogMode}
|
|
initialData={dialogMode === "edit" ? (userDetailQuery.data ?? undefined) : undefined}
|
|
isInternal={isInternal}
|
|
userCmpn={userCmpn}
|
|
userCmpnArray={userCmpnArray}
|
|
userRoleArray={userRoleArray}
|
|
onSubmit={handleSubmit}
|
|
onCheckDuplicate={handleCheckDuplicate}
|
|
/>
|
|
|
|
<UserUploadDialog
|
|
onUpload={uploadFile}
|
|
onDownloadTemplate={downloadTemplate}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default UserManagePage; |