|
@@ -1,15 +1,46 @@
|
|
|
-import React from 'react'
|
|
|
+import React, { useState, useEffect } from 'react'
|
|
|
import { PlusOutlined, DownOutlined } from '@ant-design/icons'
|
|
|
-import { Button, Popover } from 'antd'
|
|
|
+import { Button, message, Popover } from 'antd'
|
|
|
import './index.less'
|
|
|
+import { useRequest } from 'umi'
|
|
|
+import { createRoleWithMenuId, fetchRoleListByMenuId } from '@/services/user/api'
|
|
|
+import { ModalForm, ProFormText } from '@ant-design/pro-form'
|
|
|
|
|
|
type RoleMenuProps = {
|
|
|
menuId: string
|
|
|
onSelect?: (id: string) => void
|
|
|
}
|
|
|
|
|
|
-const RoleMenu: React.FC<RoleMenuProps> = () => {
|
|
|
- // const [activeId, setActiveId] = useState('')
|
|
|
+const RoleMenu: React.FC<RoleMenuProps> = ({ menuId, onSelect }) => {
|
|
|
+ const [menuRoles, setMenuRoles] = useState<API.MenuRoleItem[]>([])
|
|
|
+ const { run: tryFetchMenuRoles } = useRequest((id: string) => fetchRoleListByMenuId(id), {
|
|
|
+ manual: true,
|
|
|
+ onSuccess: result => {
|
|
|
+ setMenuRoles(result)
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ const { run: tryAddRole } = useRequest(
|
|
|
+ (params: API.CreateRoleParams) => createRoleWithMenuId(params),
|
|
|
+ {
|
|
|
+ manual: true,
|
|
|
+ onSuccess: () => {
|
|
|
+ tryFetchMenuRoles(menuId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ )
|
|
|
+ useEffect(() => {
|
|
|
+ tryFetchMenuRoles(menuId)
|
|
|
+ }, [])
|
|
|
+ const [activeId, setActiveId] = useState('')
|
|
|
+
|
|
|
+ const handleItemClick = (id: string) => {
|
|
|
+ setActiveId(id)
|
|
|
+ if (onSelect) {
|
|
|
+ onSelect()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
const content = (
|
|
|
<div className="popoverList">
|
|
|
<ul>
|
|
@@ -24,21 +55,42 @@ const RoleMenu: React.FC<RoleMenuProps> = () => {
|
|
|
<div className="h-full w-max-234px rounded-4px roleMenu">
|
|
|
<div className="p-4 border-b-1 border-solid border-black border-opacity-10 bg-[#f7f9fa] flex justify-around items-center">
|
|
|
<span className="text-[0.9375rem]">角色列表</span>
|
|
|
- <Button size="small" type="primary" ghost>
|
|
|
- <PlusOutlined />
|
|
|
- 创建角色
|
|
|
- </Button>
|
|
|
+
|
|
|
+ <ModalForm<{ name: string; backstageMenuId: string }>
|
|
|
+ title="添加新的角色"
|
|
|
+ width="500px"
|
|
|
+ trigger={
|
|
|
+ <Button size="small" type="primary" ghost>
|
|
|
+ <PlusOutlined />
|
|
|
+ 创建角色
|
|
|
+ </Button>
|
|
|
+ }
|
|
|
+ onFinish={async values => {
|
|
|
+ await tryAddRole(values)
|
|
|
+ message.success('添加成功')
|
|
|
+ return true
|
|
|
+ }}
|
|
|
+ initialValues={{ backstageMenuId: menuId }}>
|
|
|
+ <ProFormText name="backstageMenuId" hidden />
|
|
|
+ <ProFormText name="name" rules={[{ required: true, message: '请输入角色名' }]} />
|
|
|
+ </ModalForm>
|
|
|
</div>
|
|
|
<div className="p-4 bg-white" style={{ height: 'calc(100% - 1rem*2 - 20px)' }}>
|
|
|
<ul className="p-0 m-0 list-none text-primary flex flex-col flex-1">
|
|
|
- <li
|
|
|
- className="flex justify-between items-center py-2 px-5"
|
|
|
- onClick={() => setActiveId('1')}>
|
|
|
- <span>客户管理员</span>
|
|
|
- <Popover placement="bottomRight" content={content} trigger="click">
|
|
|
- <DownOutlined />
|
|
|
- </Popover>
|
|
|
- </li>
|
|
|
+ {menuRoles.map(item => (
|
|
|
+ <li
|
|
|
+ key={item.id}
|
|
|
+ className={[
|
|
|
+ 'flex justify-between items-center py-2 px-5 cursor-pointer',
|
|
|
+ item.id === activeId ? 'scale-up-center' : ''
|
|
|
+ ].join(' ')}
|
|
|
+ onClick={() => handleItemClick(item.id)}>
|
|
|
+ <span>{item.name}</span>
|
|
|
+ <Popover placement="bottomRight" content={content} trigger="click">
|
|
|
+ <DownOutlined />
|
|
|
+ </Popover>
|
|
|
+ </li>
|
|
|
+ ))}
|
|
|
</ul>
|
|
|
</div>
|
|
|
</div>
|