269 lines
8.5 KiB
TypeScript
269 lines
8.5 KiB
TypeScript
'use client'
|
|
|
|
import {useEffect, 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 "./UserTableColumn";
|
|
import _ from "lodash";
|
|
import UserUpsertDialog from "@/components/dialog/UserUpsertDialog";
|
|
import {UserInfo, UserInfoSaveRequest, UserUploadResponse} from "@/types/user";
|
|
import UserUploadDialog from "@/components/dialog/UserUploadDialog";
|
|
import {useDialog} from "@/hooks/useDialog";
|
|
import {downloadBase64File, downloadFile, formatKst} from "@/lib/utils";
|
|
import {nextApi} from "@/lib/http-client";
|
|
import {useLoading} from "@/hooks/useLoading";
|
|
import {RoleInfo} from "@/types/role";
|
|
import {DataVerification} from "@/types/common";
|
|
import {apiError} from "@/lib/http-util";
|
|
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";
|
|
|
|
const UserManagePage = () => {
|
|
const { showDialog } = useDialog();
|
|
const mounted = useMounted();
|
|
const { withLoadingAsync } = useLoading();
|
|
const { auth } = useAuth();
|
|
|
|
const [userList, setUserList] = useState<UserInfo[]>([]);
|
|
const [viewList, setViewList] = useState<UserInfo[]>([]);
|
|
const [selectedUserArray, setSelectedUserArray] = useState<UserInfo[]>([]);
|
|
const [userCmpnArray, setUserCmpnArray] = useState<string[]>([]);
|
|
const [userRoleArray, setUserRoleArray] = useState<MultiSelectOption[]>([]);
|
|
|
|
const [searchText, setSearchText] = useState('');
|
|
const [searchCmpn, setSearchCmpn] = useState<string[]>([]);
|
|
const [searchRole, setSearchRole] = useState<string[]>([]);
|
|
|
|
const [dialogMode, setDialogMode] = useState<'create' | 'edit'>('create')
|
|
const [editData, setEditData] = useState<Partial<UserInfoSaveRequest>>();
|
|
|
|
const setUserData = async () => {
|
|
const roleList = await nextApi.get<RoleInfo[]>("/role");
|
|
setUserRoleArray(
|
|
roleList.map((role) => ({
|
|
value: String(role.roleCd),
|
|
label: role.roleNm,
|
|
}))
|
|
);
|
|
|
|
const cmpnList = await nextApi.get<string[]>("/user/cmpn");
|
|
setUserCmpnArray(cmpnList);
|
|
|
|
const userData = await nextApi.get<UserInfo[]>("/user");
|
|
setUserList(userData);
|
|
}
|
|
|
|
// 등록 버튼 클릭
|
|
const handleCreateClick = () => {
|
|
setDialogMode('create');
|
|
setEditData(undefined);
|
|
showDialog('userUpsertDialog');
|
|
}
|
|
|
|
// 중복 체크
|
|
const handleCheckDuplicate = async (userId: string): Promise<DataVerification> => {
|
|
return nextApi.get<DataVerification>("/user/check-user-id", {
|
|
query: { userId },
|
|
});
|
|
}
|
|
|
|
|
|
const handleSubmit = async (data: UserInfoSaveRequest): Promise<void> => {
|
|
if (dialogMode === 'create') {
|
|
await nextApi.put<void>("/user", data);
|
|
} else {
|
|
await nextApi.patch<void>("/user", data);
|
|
}
|
|
await setUserData();
|
|
}
|
|
|
|
const handleEdit = (userId: string) => {
|
|
setDialogMode('edit');
|
|
nextApi.get<UserInfo>(`/user/${userId}`)
|
|
.then((data) => {
|
|
setEditData(data);
|
|
showDialog('userUpsertDialog');
|
|
})
|
|
.catch((e) => {
|
|
const err = apiError(e);
|
|
console.error(err.status, err.code, err.message);
|
|
});
|
|
// 수정 로직 또는 모달 열기
|
|
}
|
|
|
|
const handleDelete = (userId: string) => {
|
|
// 삭제 확인 후 데이터 삭제
|
|
if (confirm('정말 삭제하시겠습니까?')) {
|
|
//삭제
|
|
nextApi.del<void>(`/user/${encodeURIComponent(userId)}`)
|
|
.then(async () => {
|
|
alert("삭제 됐습니다.");
|
|
await setUserData();
|
|
})
|
|
.catch((e) => {
|
|
const err = apiError(e);
|
|
console.error(err.status, err.code, err.message);
|
|
});
|
|
}
|
|
}
|
|
|
|
const uploadFile = async (file: File) => {
|
|
const formData = new FormData();
|
|
formData.append("file", file); // key 이름은 백엔드 @RequestPart("file") 와 동일해야 함
|
|
|
|
const res = await nextApi.postForm<UserUploadResponse>("/user/excel", formData);
|
|
|
|
if (res.hasErrorFile && res.errorFileBase64) {
|
|
downloadBase64File(res.errorFileBase64, res.errorFileName ?? "error.xlsx");
|
|
}
|
|
else{
|
|
await setUserData();
|
|
}
|
|
}
|
|
|
|
const downloadTemplate = async () => {
|
|
const buffer = await nextApi.post<ArrayBuffer>(
|
|
"/user/excel-template",
|
|
undefined,
|
|
{ expectBinary: true }
|
|
);
|
|
|
|
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
|
|
);
|
|
|
|
useEffect(() => {
|
|
withLoadingAsync(setUserData);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
let tempData = _.cloneDeep(userList);
|
|
const keyword = searchText.trim();
|
|
if (keyword){
|
|
tempData = tempData.filter((row) =>
|
|
[row.userId, row.userName].some((v) => v.toLowerCase().includes(keyword.toLowerCase()))
|
|
);
|
|
}
|
|
|
|
if(searchCmpn.length > 0){
|
|
tempData = tempData.filter((row) => searchCmpn.includes(row.userCmpn));
|
|
}
|
|
|
|
if(searchRole.length > 0){
|
|
tempData = tempData.filter((row) => searchRole.includes(String(row.userRole)));
|
|
}
|
|
|
|
setViewList(tempData);
|
|
}, [searchText, searchCmpn, searchRole, userList]);
|
|
|
|
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'
|
|
onRowSelectionChange={setSelectedUserArray}
|
|
defaultPageSize={10}
|
|
pageSizeOptions={[10, 20, 30, 50]}
|
|
getRowId={(row) => row.userId} // row.id = userId
|
|
/>
|
|
</div>
|
|
|
|
<UserUpsertDialog
|
|
mode={dialogMode}
|
|
initialData={editData}
|
|
isInternal={isInternal}
|
|
userCmpn={userCmpn}
|
|
userCmpnArray={userCmpnArray}
|
|
userRoleArray={userRoleArray}
|
|
onSubmit={handleSubmit}
|
|
onCheckDuplicate={handleCheckDuplicate}
|
|
/>
|
|
|
|
<UserUploadDialog
|
|
onUpload={uploadFile}
|
|
onDownloadTemplate={downloadTemplate}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default UserManagePage; |