import React from "react";
import { Button, Input, List, Popconfirm, Spin, Modal, message } from "antd";
import useBool from "@/utils/hooks/useBool";
import classNames from "classnames";
import {
getSysRole,
postSysRole,
putSysRole,
deleteSysRole,
} from "@/service/sysrole";
import "./style.less";
const Header = (props) => {
return (
{props.children}
{props.action}
);
};
export default (props) => {
const { current, onChange } = props;
const [loading, startLoading, cancelLoading] = useBool();
const [visible, openModel, hideModel] = useBool();
const [finished, setFinished] = React.useState(false);
const [inText, setInText] = React.useState();
const [list, setList] = React.useState([]);
const onAdd = () => {
openModel();
setInText();
onChange();
};
const onEdit = (item) => {
openModel();
setInText(item.name);
onChange(item);
};
const onDelete = (item) => {
startLoading();
// 删除角色
deleteSysRole(item.roleId)
.then(() => {
setList(list.filter((x) => x.roleId !== item.roleId));
cancelLoading();
hideModel();
onChange();
})
.catch(() => {
cancelLoading();
});
};
const handleOk = () => {
if (!inText) {
message.warn("请输入角色名称");
return;
}
startLoading();
const data = { name: inText };
if (current) {
// 修改角色
putSysRole(current.roleId, data)
.then((res) => {
setList(list.map((x) => (x.roleId === current.roleId ? res : x)));
cancelLoading();
hideModel();
})
.catch(() => {
cancelLoading();
});
} else {
// 新增角色
postSysRole(data)
.then((res) => {
setList([res].concat(list));
cancelLoading();
hideModel();
onChange(res);
})
.catch(() => {
cancelLoading();
});
}
};
const handleCancel = () => {
if (loading) {
return;
}
hideModel();
};
const queryRoles = React.useCallback((params) => {
startLoading();
getSysRole(params)
.then((res) => {
setList(res.records);
cancelLoading();
setFinished(res.current >= res.pages);
})
.catch(() => {
cancelLoading();
});
}, []);
React.useEffect(() => {
queryRoles({ pageNum: 1, pageSize: 500 });
}, []);
return (
新增
}
>
角色列表
}
style={{ background: "#fff" }}
bordered
dataSource={list}
renderItem={(item) => (
onChange(item)}
actions={[
,
onDelete(item)}
>
,
]}
>
{item.name}
)}
/>
setInText(e.target.value)} />
);
};