2024-12-30 08:26:40 +08:00
|
|
|
import React, { useMemo, useContext, useCallback, useState } from "react";
|
2025-01-06 08:45:23 +08:00
|
|
|
import { ObjectType } from "@nice/common"
|
2024-12-30 08:26:40 +08:00
|
|
|
import { ICellRendererParams, SortDirection } from "ag-grid-community";
|
|
|
|
|
import { ColDef } from "@ag-grid-community/core";
|
|
|
|
|
import AgServerTable from "../../presentation/ag-server-table";
|
|
|
|
|
import { DeleteOutlined, EditFilled, EllipsisOutlined, PlusOutlined } from "@ant-design/icons";
|
|
|
|
|
import { Menu, MenuItem } from "../../presentation/dropdown-menu";
|
|
|
|
|
import { DeptEditorContext } from "./dept-editor";
|
|
|
|
|
import { CustomCellRendererProps } from "ag-grid-react";
|
|
|
|
|
import { message, Tag } from "antd";
|
2025-01-06 08:45:23 +08:00
|
|
|
import { CrudOperation, emitDataChange, useDepartment } from "@nice/client";
|
2024-09-10 10:31:24 +08:00
|
|
|
|
|
|
|
|
export default function DepartmentList() {
|
2024-12-30 08:26:40 +08:00
|
|
|
const { setEditId, setModalOpen, setParentId } = useContext(DeptEditorContext);
|
|
|
|
|
// 将 params 转换为 state
|
|
|
|
|
const [params, setParams] = useState({ parentId: null });
|
|
|
|
|
const { softDeleteByIds } = useDepartment()
|
|
|
|
|
const OpreationRenderer = ({ props }: { props: ICellRendererParams }) => {
|
|
|
|
|
const handleEdit = () => {
|
|
|
|
|
setEditId(props?.data?.id);
|
|
|
|
|
setModalOpen(true);
|
2024-09-10 10:31:24 +08:00
|
|
|
};
|
2024-12-30 08:26:40 +08:00
|
|
|
const handleCreate = () => {
|
|
|
|
|
setParentId(props.data?.id)
|
|
|
|
|
setModalOpen(true);
|
2024-09-10 10:31:24 +08:00
|
|
|
}
|
2024-12-30 08:26:40 +08:00
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<Menu
|
|
|
|
|
node={
|
|
|
|
|
<EllipsisOutlined className=" hover:bg-textHover p-1 rounded" />
|
|
|
|
|
}>
|
|
|
|
|
<MenuItem
|
|
|
|
|
label="添加子节点"
|
|
|
|
|
icon={<PlusOutlined></PlusOutlined>}
|
|
|
|
|
onClick={handleCreate}
|
|
|
|
|
/>
|
|
|
|
|
<MenuItem
|
|
|
|
|
label="编辑"
|
|
|
|
|
icon={<EditFilled />}
|
|
|
|
|
onClick={handleEdit}
|
|
|
|
|
/>
|
|
|
|
|
<MenuItem
|
|
|
|
|
label="移除"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
softDeleteByIds.mutateAsync({
|
|
|
|
|
ids: [props?.data?.id],
|
|
|
|
|
}, {
|
|
|
|
|
onSettled: () => {
|
|
|
|
|
message.success("删除成功");
|
|
|
|
|
emitDataChange(ObjectType.DEPARTMENT, props.data as any, CrudOperation.DELETED)
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
icon={<DeleteOutlined></DeleteOutlined>}></MenuItem>
|
|
|
|
|
</Menu>
|
|
|
|
|
</div>
|
2024-09-10 10:31:24 +08:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-30 08:26:40 +08:00
|
|
|
const columnDefs = useMemo<ColDef[]>(() => {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
headerName: "是否为域",
|
|
|
|
|
field: "is_domain",
|
|
|
|
|
cellRenderer: (props: CustomCellRendererProps) => {
|
|
|
|
|
return <Tag color={props.value ? "cyan" : "blue"}>
|
|
|
|
|
{props.value ? '域节点' : '普通节点'}
|
|
|
|
|
</Tag>
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
field: "order",
|
|
|
|
|
hide: true,
|
|
|
|
|
sort: "asc" as SortDirection
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
headerName: "操作",
|
2025-03-02 21:51:17 +08:00
|
|
|
// sortable: true,
|
2024-12-30 08:26:40 +08:00
|
|
|
cellRenderer: (props: CustomCellRendererProps) => (
|
|
|
|
|
<OpreationRenderer props={props} />
|
|
|
|
|
),
|
|
|
|
|
maxWidth: 80,
|
|
|
|
|
},
|
|
|
|
|
].filter(Boolean);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const autoGroupColumnDef = useMemo(() => ({
|
|
|
|
|
rowDrag: true,
|
|
|
|
|
headerName: "单位名",
|
|
|
|
|
field: "name",
|
|
|
|
|
filter: "agTextColumnFilter",
|
|
|
|
|
}), []);
|
|
|
|
|
|
|
|
|
|
const getServerSideGroupKey = useCallback((item) => item.id, []);
|
|
|
|
|
const isServerSideGroup = useCallback((item) => item.has_children, []);
|
2024-09-10 10:31:24 +08:00
|
|
|
|
|
|
|
|
return (
|
2024-12-30 08:26:40 +08:00
|
|
|
<AgServerTable
|
|
|
|
|
height={"calc(100vh - 48px - 49px)"}
|
|
|
|
|
columnDefs={columnDefs}
|
|
|
|
|
objectType={ObjectType.DEPARTMENT}
|
|
|
|
|
treeData={true}
|
|
|
|
|
params={params} // 使用 state 中的 params
|
|
|
|
|
getServerSideGroupKey={getServerSideGroupKey}
|
|
|
|
|
isServerSideGroup={isServerSideGroup}
|
|
|
|
|
autoGroupColumnDef={autoGroupColumnDef}
|
|
|
|
|
/>
|
2024-09-10 10:31:24 +08:00
|
|
|
);
|
|
|
|
|
}
|