418 lines
12 KiB
TypeScript
418 lines
12 KiB
TypeScript
import dayjs from "dayjs";
|
|
import React, { useEffect, useState, useCallback, memo } from "react";
|
|
import { useFieldArray, useForm, type UseFormReturn } from "react-hook-form";
|
|
import { useConfirm } from "@frontend-opensource/use-react-hooks";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import {
|
|
useQuery,
|
|
useQueryClient,
|
|
keepPreviousData,
|
|
} from "@tanstack/react-query";
|
|
import {
|
|
Button,
|
|
Checkbox,
|
|
Input,
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@common/components";
|
|
import {
|
|
UserOrganizationApi,
|
|
userOrganizationFormSchema,
|
|
type UserOrganizationFormData,
|
|
type UserOrganizationPayload,
|
|
type User,
|
|
type SearchUser,
|
|
type MoveUserOrganizationPayload,
|
|
} from "@/entities/user-organization";
|
|
import { CodeApi, type ICode } from "@/entities/code";
|
|
import type { OrganizationTree } from "@/entities/organization";
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import { useAlertStore, useMutationWrapper } from "@/shared/lib";
|
|
import { AddUserDialog } from "./add-user-dialog";
|
|
import { MoveOrganizationDialog } from "./move-organization-dialog";
|
|
|
|
type UserOrganizationSectionProps = {
|
|
ognzSn: number;
|
|
};
|
|
|
|
function UserOrganizationSection({ ognzSn }: UserOrganizationSectionProps) {
|
|
const queryClient = useQueryClient();
|
|
const { confirm } = useConfirm();
|
|
const alert = useAlertStore((state) => state.setMessage);
|
|
|
|
const [userList, setUserList] = useState<User[]>([]);
|
|
const [selectedUsers, setSelectedUsers] = useState<User[]>([]);
|
|
|
|
const isAllSelected =
|
|
userList.length > 0 && selectedUsers.length === userList.length;
|
|
|
|
const { data: userOrganizationList } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.USER_ORGANIZATION, ognzSn],
|
|
queryFn: () => UserOrganizationApi.getUserOrganizationList({ ognzSn }),
|
|
enabled: !!ognzSn,
|
|
placeholderData: keepPreviousData,
|
|
});
|
|
|
|
const { data: jobPositionCodes } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.CODE, "JPBSCLCD"],
|
|
queryFn: () => CodeApi.getCode("JPBSCLCD"),
|
|
});
|
|
|
|
const { data: jobGradeCodes } = useQuery({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.CODE, "JDGDCLCD"],
|
|
queryFn: () => CodeApi.getCode("JDGDCLCD"),
|
|
});
|
|
|
|
const { mutate: saveUserOrganization } = useMutationWrapper({
|
|
mutationFn: (data: UserOrganizationPayload[]) =>
|
|
UserOrganizationApi.saveUserOrganization(data),
|
|
onSuccess: () => {
|
|
alert("저장되었습니다.");
|
|
queryClient.invalidateQueries({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.USER_ORGANIZATION, ognzSn],
|
|
});
|
|
},
|
|
});
|
|
|
|
const { mutate: moveUserOrganization } = useMutationWrapper({
|
|
mutationFn: (data: MoveUserOrganizationPayload[]) =>
|
|
UserOrganizationApi.moveUserOrganization(data),
|
|
onSuccess: () => {
|
|
alert("이동되었습니다.");
|
|
queryClient.invalidateQueries({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.USER_ORGANIZATION, ognzSn],
|
|
});
|
|
},
|
|
});
|
|
|
|
const form = useForm<UserOrganizationFormData>({
|
|
resolver: zodResolver(userOrganizationFormSchema),
|
|
values: {
|
|
userOrganization:
|
|
userOrganizationList?.content.map((user) => ({
|
|
mode: user.mode,
|
|
ognzSn: user.ognzSn,
|
|
userUnqId: user.userUnqId,
|
|
jbpsSeCd: user.jbpsSeCd,
|
|
jbgdSeCd: user.jbgdSeCd,
|
|
sortSeq: user.sortSeq,
|
|
})) ?? [],
|
|
},
|
|
});
|
|
|
|
const { append } = useFieldArray({
|
|
name: "userOrganization",
|
|
control: form.control,
|
|
});
|
|
|
|
const formData = form.watch("userOrganization");
|
|
const formValidation = userOrganizationFormSchema.safeParse({
|
|
userOrganization: formData,
|
|
});
|
|
const isFormValid = formValidation.success;
|
|
|
|
const handleAddUsers = (users: SearchUser[]) => {
|
|
const generateUserData = (user: SearchUser) => ({
|
|
mode: "C",
|
|
ognzSn,
|
|
userUnqId: user.userUnqId,
|
|
jbpsSeCd: "",
|
|
jbgdSeCd: "",
|
|
sortSeq: 0,
|
|
});
|
|
|
|
const newFormData = users.map(generateUserData);
|
|
append(newFormData);
|
|
|
|
const newUserList = users.map((user) => ({
|
|
...generateUserData(user),
|
|
userUnqNm: user.userNm,
|
|
}));
|
|
const existingUserIds = new Set(newUserList.map((user) => user.userUnqId));
|
|
const filteredUserList = userList.filter(
|
|
(user) => !existingUserIds.has(user.userUnqId),
|
|
);
|
|
setUserList([...filteredUserList, ...newUserList]);
|
|
};
|
|
|
|
const handleToggleSelectUser = useCallback((user: User) => {
|
|
setSelectedUsers((prevSelected) => {
|
|
const isSelected = prevSelected.some(
|
|
(item) => item.userUnqId === user.userUnqId,
|
|
);
|
|
|
|
if (isSelected) {
|
|
return prevSelected.filter((item) => item.userUnqId !== user.userUnqId);
|
|
} else {
|
|
return [...prevSelected, user];
|
|
}
|
|
});
|
|
}, []);
|
|
|
|
const handleToggleSelectAll = () => {
|
|
if (isAllSelected) {
|
|
setSelectedUsers([]);
|
|
} else {
|
|
setSelectedUsers([...userList]);
|
|
}
|
|
};
|
|
|
|
const handleDeleteUsers = () => {
|
|
const selectedUserIds = selectedUsers.map((user) => user.userUnqId);
|
|
const updatedFormData = form.getValues("userOrganization").map((item) => {
|
|
if (selectedUserIds.includes(item.userUnqId)) {
|
|
return { ...item, mode: "D" };
|
|
}
|
|
return item;
|
|
});
|
|
form.setValue("userOrganization", updatedFormData);
|
|
setUserList((prevUsers) =>
|
|
prevUsers.filter((user) => !selectedUserIds.includes(user.userUnqId)),
|
|
);
|
|
setSelectedUsers([]);
|
|
};
|
|
|
|
const handleSaveUsers = async () => {
|
|
if (await confirm("저장하시겠습니까?")) {
|
|
const filteredData = form
|
|
.watch("userOrganization")
|
|
.filter((item) => item.mode !== "R");
|
|
saveUserOrganization(filteredData);
|
|
}
|
|
};
|
|
|
|
const handleMoveOrganization = async (organization: OrganizationTree) => {
|
|
const payloadData: MoveUserOrganizationPayload[] = selectedUsers.map(
|
|
(user) => ({
|
|
ognzSn: user.ognzSn,
|
|
toOgnzSn: organization.ognzSn!,
|
|
userUnqId: user.userUnqId,
|
|
}),
|
|
);
|
|
moveUserOrganization(payloadData);
|
|
};
|
|
|
|
const isUserSelected = useCallback(
|
|
(userUnqId: string) =>
|
|
selectedUsers.some((item) => item.userUnqId === userUnqId),
|
|
[selectedUsers],
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (userOrganizationList?.content) {
|
|
setUserList(userOrganizationList.content);
|
|
setSelectedUsers([]);
|
|
}
|
|
}, [userOrganizationList]);
|
|
|
|
return (
|
|
<div className="card card-border">
|
|
<div className="card-header">
|
|
<h3 className="text-2xl font-medium text-label">소속원 목록</h3>
|
|
<div className="flex gap-2">
|
|
<MoveOrganizationDialog
|
|
buttonDisabled={
|
|
userOrganizationList?.content.length === 0 ||
|
|
selectedUsers.length === 0
|
|
}
|
|
handleMoveOrganization={handleMoveOrganization}
|
|
/>
|
|
<AddUserDialog onAddUsers={handleAddUsers} />
|
|
<Button
|
|
size="sm"
|
|
color="red"
|
|
onClick={handleDeleteUsers}
|
|
disabled={selectedUsers.length === 0}
|
|
>
|
|
선택삭제
|
|
</Button>
|
|
<Button size="sm" onClick={handleSaveUsers} disabled={!isFormValid}>
|
|
저장
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
<div className="card max-h-[calc(100vh-58rem)] overflow-auto !p-0">
|
|
<Table>
|
|
<colgroup>
|
|
<col width="5%" />
|
|
<col width="5%" />
|
|
<col width="auto" />
|
|
<col width="10%" />
|
|
<col width="10%" />
|
|
<col width="10%" />
|
|
<col width="15%" />
|
|
<col width="15%" />
|
|
</colgroup>
|
|
<TableHeader className="sticky top-0">
|
|
<TableRow>
|
|
<TableHead>
|
|
<Checkbox
|
|
checked={isAllSelected}
|
|
onCheckedChange={handleToggleSelectAll}
|
|
/>
|
|
</TableHead>
|
|
<TableHead>NO</TableHead>
|
|
<TableHead>이름</TableHead>
|
|
<TableHead>직위</TableHead>
|
|
<TableHead>직급</TableHead>
|
|
<TableHead>정렬순서</TableHead>
|
|
<TableHead>최종수정자</TableHead>
|
|
<TableHead>최종수정일</TableHead>
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
{userList.map((user, index) => (
|
|
<TableRow key={user.userUnqId}>
|
|
<TableCell align="center">
|
|
<Checkbox
|
|
checked={isUserSelected(user.userUnqId)}
|
|
onCheckedChange={() => handleToggleSelectUser(user)}
|
|
/>
|
|
</TableCell>
|
|
<UserCells
|
|
form={form}
|
|
user={user}
|
|
index={index}
|
|
jobPositionCodeData={jobPositionCodes}
|
|
jobGradeCodeData={jobGradeCodes}
|
|
/>
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
type UserCellsProps = {
|
|
form: UseFormReturn<UserOrganizationFormData>;
|
|
user: User;
|
|
index: number;
|
|
jobPositionCodeData?: ICode[];
|
|
jobGradeCodeData?: ICode[];
|
|
};
|
|
|
|
const UserCells = memo(
|
|
({
|
|
form,
|
|
user,
|
|
index,
|
|
jobPositionCodeData,
|
|
jobGradeCodeData,
|
|
}: UserCellsProps) => {
|
|
const [jbpsSeCd, setJbpsSeCd] = useState(user.jbpsSeCd);
|
|
const [jbgdSeCd, setJbgdSeCd] = useState(user.jbgdSeCd);
|
|
|
|
const updateMode = (index: number) => {
|
|
const mode = form.watch(`userOrganization.${index}.mode`);
|
|
if (mode === "R") {
|
|
form.setValue(`userOrganization.${index}.mode`, "U");
|
|
}
|
|
};
|
|
|
|
const updateFormValue =
|
|
(index: number, key: keyof UserOrganizationPayload) =>
|
|
(value: string | number) => {
|
|
updateMode(index);
|
|
form.setValue(`userOrganization.${index}.${key}`, value);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<TableCell align="center">{index + 1}</TableCell>
|
|
<TableCell align="center">{user.userUnqNm}</TableCell>
|
|
<TableCell align="center">
|
|
<CodeSelect
|
|
codeData={jobPositionCodeData}
|
|
defaultValue={user.jbpsSeCd}
|
|
onValueChange={(value) => {
|
|
setJbpsSeCd(value);
|
|
updateFormValue(index, "jbpsSeCd")(value);
|
|
}}
|
|
{...form.register(`userOrganization.${index}.jbpsSeCd`)}
|
|
/>
|
|
{!jbpsSeCd && (
|
|
<p className="pt-1 text-base text-destructive">
|
|
직위를 선택해주세요.
|
|
</p>
|
|
)}
|
|
</TableCell>
|
|
<TableCell align="center">
|
|
<CodeSelect
|
|
codeData={jobGradeCodeData}
|
|
defaultValue={user.jbgdSeCd}
|
|
onValueChange={(value) => {
|
|
setJbgdSeCd(value);
|
|
updateFormValue(index, "jbgdSeCd")(value);
|
|
}}
|
|
{...form.register(`userOrganization.${index}.jbgdSeCd`)}
|
|
/>
|
|
{!jbgdSeCd && (
|
|
<p className="pt-1 text-base text-destructive">
|
|
직급을 선택해주세요.
|
|
</p>
|
|
)}
|
|
</TableCell>
|
|
<TableCell align="center">
|
|
<Input
|
|
type="number"
|
|
min={0}
|
|
defaultValue={user.sortSeq}
|
|
onChange={(e) =>
|
|
updateFormValue(index, "sortSeq")(parseInt(e.target.value))
|
|
}
|
|
className="w-[6rem] !text-[1.3rem]"
|
|
/>
|
|
</TableCell>
|
|
<TableCell align="center">{user.mdfrNm}</TableCell>
|
|
<TableCell align="center">
|
|
{dayjs(user.mdfcnDt).format("YYYY-MM-DD")}
|
|
</TableCell>
|
|
</>
|
|
);
|
|
},
|
|
);
|
|
|
|
UserCells.displayName = "UserCells";
|
|
|
|
type CodeSelectProps = React.ComponentPropsWithoutRef<typeof Select> & {
|
|
codeData?: ICode[];
|
|
};
|
|
|
|
const CodeSelect = memo(({ codeData, ...props }: CodeSelectProps) => {
|
|
if (!codeData) return null;
|
|
|
|
const placeholderText =
|
|
codeData.find((item) => item.lwpoCd === props.defaultValue)?.lwpoCdNm ??
|
|
"선택";
|
|
|
|
return (
|
|
<Select {...props}>
|
|
<SelectTrigger className="w-[10rem]">
|
|
<SelectValue placeholder={placeholderText} />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{codeData.map((item) => (
|
|
<SelectItem key={item.lwpoCd} value={item.lwpoCd}>
|
|
{item.lwpoCdNm}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
);
|
|
});
|
|
|
|
CodeSelect.displayName = "CodeSelect";
|
|
|
|
export { UserOrganizationSection };
|