215 lines
7.2 KiB
TypeScript
215 lines
7.2 KiB
TypeScript
import type { ColumnDef } from "@tanstack/react-table";
|
|
import type { UseFormReturn } from "react-hook-form";
|
|
import { Checkbox } from "@common/components";
|
|
import type {
|
|
MenuAuthority,
|
|
MenuAuthorityPayload,
|
|
MenuAuthorityFormData,
|
|
} from "@/entities/menu-authority";
|
|
|
|
export const createColumns = (
|
|
form: UseFormReturn<MenuAuthorityFormData>,
|
|
): ColumnDef<MenuAuthority>[] => {
|
|
const getCurrentRow = (i: number) => form.watch("menuAuthority")[i];
|
|
const menuAuthorityData = form.watch("menuAuthority");
|
|
|
|
const handleCheckedChange =
|
|
(i: number, key: keyof MenuAuthorityPayload) => (checked: boolean) => {
|
|
if (getCurrentRow(i)?.mode === "R") {
|
|
form.setValue(`menuAuthority.${i}.mode`, "U");
|
|
}
|
|
form.setValue(`menuAuthority.${i}.${key}`, checked ? "Y" : "N");
|
|
};
|
|
|
|
const handleHeaderCheckedChange = (key: keyof MenuAuthorityPayload) => (checked: boolean) => {
|
|
menuAuthorityData.forEach((_, index) => {
|
|
if (getCurrentRow(index)?.mode === "R") {
|
|
form.setValue(`menuAuthority.${index}.mode`, "U");
|
|
}
|
|
form.setValue(`menuAuthority.${index}.${key}`, checked ? "Y" : "N");
|
|
});
|
|
};
|
|
|
|
const isAllChecked = (key: keyof MenuAuthorityPayload) => {
|
|
return menuAuthorityData.every((item) => item?.[key] === "Y");
|
|
};
|
|
|
|
return [
|
|
{
|
|
accessorKey: "menuNm",
|
|
header: "메뉴명",
|
|
cell: ({ getValue }) => (
|
|
<div className="text-left">{getValue() as string}</div>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "mngAuthrtYn",
|
|
header: () => (
|
|
<div className="flex items-center justify-center gap-2">
|
|
<span>관리권한</span>
|
|
<Checkbox
|
|
checked={isAllChecked("mngAuthrtYn")}
|
|
onCheckedChange={(checked) => {
|
|
if (checked) {
|
|
// 관리권한 체크시 모든 권한 체크
|
|
menuAuthorityData.forEach((_, index) => {
|
|
if (getCurrentRow(index)?.mode === "R") {
|
|
form.setValue(`menuAuthority.${index}.mode`, "U");
|
|
}
|
|
form.setValue(`menuAuthority.${index}.regAuthrtYn`, "Y");
|
|
form.setValue(`menuAuthority.${index}.mdfcnAuthrtYn`, "Y");
|
|
form.setValue(`menuAuthority.${index}.delAuthrtYn`, "Y");
|
|
form.setValue(`menuAuthority.${index}.inqAuthrtYn`, "Y");
|
|
form.setValue(`menuAuthority.${index}.mngAuthrtYn`, "Y");
|
|
});
|
|
} else {
|
|
// 관리권한 해제시 모든 권한 해제
|
|
menuAuthorityData.forEach((_, index) => {
|
|
if (getCurrentRow(index)?.mode === "R") {
|
|
form.setValue(`menuAuthority.${index}.mode`, "U");
|
|
}
|
|
form.setValue(`menuAuthority.${index}.regAuthrtYn`, "N");
|
|
form.setValue(`menuAuthority.${index}.mdfcnAuthrtYn`, "N");
|
|
form.setValue(`menuAuthority.${index}.delAuthrtYn`, "N");
|
|
form.setValue(`menuAuthority.${index}.inqAuthrtYn`, "N");
|
|
form.setValue(`menuAuthority.${index}.mngAuthrtYn`, "N");
|
|
});
|
|
}
|
|
}}
|
|
/>
|
|
</div>
|
|
),
|
|
cell: ({ row }) => {
|
|
return (
|
|
<Checkbox
|
|
checked={getCurrentRow(row.index)?.mngAuthrtYn === "Y"}
|
|
onCheckedChange={(checked) => {
|
|
if (checked) {
|
|
handleCheckedChange(
|
|
row.index,
|
|
"regAuthrtYn",
|
|
)(checked as boolean);
|
|
handleCheckedChange(
|
|
row.index,
|
|
"mdfcnAuthrtYn",
|
|
)(checked as boolean);
|
|
handleCheckedChange(
|
|
row.index,
|
|
"delAuthrtYn",
|
|
)(checked as boolean);
|
|
handleCheckedChange(
|
|
row.index,
|
|
"inqAuthrtYn",
|
|
)(checked as boolean);
|
|
}
|
|
handleCheckedChange(row.index, "mngAuthrtYn")(checked as boolean);
|
|
}}
|
|
/>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "regAuthrtYn",
|
|
header: () => (
|
|
<div className="flex items-center justify-start gap-2">
|
|
<span>등록권한</span>
|
|
<Checkbox
|
|
checked={isAllChecked("regAuthrtYn")}
|
|
onCheckedChange={(checked) => {
|
|
if (checked) {
|
|
// 등록권한 체크시 조회권한도 체크
|
|
menuAuthorityData.forEach((_, index) => {
|
|
if (getCurrentRow(index)?.mode === "R") {
|
|
form.setValue(`menuAuthority.${index}.mode`, "U");
|
|
}
|
|
form.setValue(`menuAuthority.${index}.inqAuthrtYn`, "Y");
|
|
form.setValue(`menuAuthority.${index}.regAuthrtYn`, "Y");
|
|
});
|
|
} else {
|
|
// 등록권한 해제시 해당 권한만 해제
|
|
handleHeaderCheckedChange("regAuthrtYn")(false);
|
|
}
|
|
}}
|
|
/>
|
|
</div>
|
|
),
|
|
cell: ({ row }) => {
|
|
return (
|
|
<Checkbox
|
|
checked={getCurrentRow(row.index)?.regAuthrtYn === "Y"}
|
|
onCheckedChange={(checked) => {
|
|
if (checked) {
|
|
handleCheckedChange(
|
|
row.index,
|
|
"inqAuthrtYn",
|
|
)(checked as boolean);
|
|
}
|
|
handleCheckedChange(row.index, "regAuthrtYn")(checked as boolean);
|
|
}}
|
|
/>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "mdfcnAuthrtYn",
|
|
header: () => (
|
|
<div className="flex items-center justify-start gap-2">
|
|
<span>수정권한</span>
|
|
<Checkbox
|
|
checked={isAllChecked("mdfcnAuthrtYn")}
|
|
onCheckedChange={handleHeaderCheckedChange("mdfcnAuthrtYn")}
|
|
/>
|
|
</div>
|
|
),
|
|
cell: ({ row }) => {
|
|
return (
|
|
<Checkbox
|
|
checked={getCurrentRow(row.index)?.mdfcnAuthrtYn === "Y"}
|
|
onCheckedChange={handleCheckedChange(row.index, "mdfcnAuthrtYn")}
|
|
/>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "delAuthrtYn",
|
|
header: () => (
|
|
<div className="flex items-center justify-start gap-2">
|
|
<span>삭제권한</span>
|
|
<Checkbox
|
|
checked={isAllChecked("delAuthrtYn")}
|
|
onCheckedChange={handleHeaderCheckedChange("delAuthrtYn")}
|
|
/>
|
|
</div>
|
|
),
|
|
cell: ({ row }) => {
|
|
return (
|
|
<Checkbox
|
|
checked={getCurrentRow(row.index)?.delAuthrtYn === "Y"}
|
|
onCheckedChange={handleCheckedChange(row.index, "delAuthrtYn")}
|
|
/>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "inqAuthrtYn",
|
|
header: () => (
|
|
<div className="flex items-center justify-start gap-2">
|
|
<span>조회권한</span>
|
|
<Checkbox
|
|
checked={isAllChecked("inqAuthrtYn")}
|
|
onCheckedChange={handleHeaderCheckedChange("inqAuthrtYn")}
|
|
/>
|
|
</div>
|
|
),
|
|
cell: ({ row }) => {
|
|
return (
|
|
<Checkbox
|
|
checked={getCurrentRow(row.index)?.inqAuthrtYn === "Y"}
|
|
onCheckedChange={handleCheckedChange(row.index, "inqAuthrtYn")}
|
|
/>
|
|
);
|
|
},
|
|
},
|
|
];
|
|
};
|