index.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { useState, useEffect } from 'react'
  2. import { Button, message } from 'antd'
  3. import { PlusOutlined } from '@ant-design/icons'
  4. import consts from '@/consts'
  5. import { apiaddDepartment } from '@/services/department'
  6. import { connect } from 'dva'
  7. import { ModalForm, ProFormText, ProFormSelect } from '@ant-design/pro-form'
  8. const RoleMenu = props => {
  9. const { dispatch, departments = [], parentId } = props
  10. const [state, setState] = useState({
  11. params: {},
  12. visible: false
  13. })
  14. const [addDepartment, setAddDepartment] = useState({
  15. visible: false,
  16. loading: false
  17. })
  18. const handleAddDepartment = async values => {
  19. setAddDepartment({ ...addDepartment, loading: true })
  20. const { code = -1 } = await apiaddDepartment({ ...values, parentId })
  21. if (code === consts.RET_CODE.SUCCESS) {
  22. message.success('新增成功')
  23. setState({ ...state, data: values })
  24. }
  25. // 请求完了
  26. setAddDepartment({ ...addDepartment, loading: false, visible: false })
  27. // 刷新数据
  28. dispatch({
  29. type: 'refresh/commitAction',
  30. payload: 'department'
  31. })
  32. }
  33. useEffect(() => {
  34. setState({ ...state, params: { ...state.params, parentId } })
  35. }, [parentId])
  36. useEffect(() => {
  37. if (!departments.length) {
  38. dispatch({
  39. type: 'staff/fetchDepartments'
  40. })
  41. }
  42. }, [])
  43. return (
  44. <div className="h-full w-max-234px rounded-4px roleMenu">
  45. <div className="p-4 border-b-1 border-solid border-black border-opacity-10 bg-[#f7f9fa] flex justify-around items-center">
  46. <span className="text-[0.9375rem]">组织架构</span>
  47. <ModalForm
  48. title="创建部门"
  49. width="500px"
  50. trigger={
  51. <Button size="small" type="primary" ghost>
  52. <PlusOutlined />
  53. 创建部门
  54. </Button>
  55. }
  56. onFinish={handleAddDepartment}
  57. parentId={{ parentId }}>
  58. <ProFormText name="backstageMenuId" hidden />
  59. <ProFormText name="name" label="新增部门" rules={[{ message: '请输入部门名称' }]} />
  60. <ProFormSelect
  61. width="md"
  62. options={departments.map(item => ({ label: item.name, value: item.id }))}
  63. name="parentId"
  64. label="上级部门"
  65. />
  66. </ModalForm>
  67. </div>
  68. <div className="p-4 bg-white" style={{ height: 'calc(100% - 1rem*2 - 20px)' }}>
  69. <ul className="p-0 m-0 list-none text-primary flex flex-col flex-1">
  70. {departments.map(item => (
  71. <li
  72. key={item.id}
  73. className="flex justify-between items-center py-2 px-5 cursor-pointer">
  74. <span>{item.name}</span>
  75. </li>
  76. ))}
  77. </ul>
  78. </div>
  79. </div>
  80. )
  81. }
  82. // export default RoleMenu
  83. export default connect(({ staff, refresh }) => ({
  84. departments: staff.departments,
  85. shouldUpdate: refresh.department
  86. }))(RoleMenu)