2021-12-27 18:06:26 +05:00
|
|
|
|
import { memo, useEffect, useState } from 'react'
|
|
|
|
|
|
2021-12-15 11:01:18 +05:00
|
|
|
|
import LoaderPortal from '../../components/LoaderPortal'
|
2021-12-27 18:06:26 +05:00
|
|
|
|
import { PermissionView } from '../../components/Views'
|
2021-12-15 11:01:18 +05:00
|
|
|
|
import { invokeWebApiWrapperAsync } from '../../components/factory'
|
2021-12-28 14:34:12 +05:00
|
|
|
|
import { EditableTable, makeActionHandler, makeColumn, makeSelectColumn, makeTagColumn } from '../../components/Table'
|
2021-12-27 18:06:26 +05:00
|
|
|
|
import { AdminPermissionService, AdminUserRoleService } from '../../services/api'
|
|
|
|
|
import { arrayOrDefault } from '../../utils'
|
2021-12-30 10:25:42 +05:00
|
|
|
|
import { min1 } from '../../utils/validationRules'
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
2021-12-28 14:34:12 +05:00
|
|
|
|
export const RoleController = memo(() => {
|
2021-12-27 18:06:26 +05:00
|
|
|
|
const [permissions, setPermissions] = useState([])
|
2021-12-15 11:01:18 +05:00
|
|
|
|
const [roles, setRoles] = useState([])
|
|
|
|
|
const [showLoader, setShowLoader] = useState(false)
|
|
|
|
|
const [columns, setColumns] = useState([])
|
|
|
|
|
|
2021-12-27 18:06:26 +05:00
|
|
|
|
const loadRoles = async () => {
|
|
|
|
|
const roles = await AdminUserRoleService.getAll()
|
|
|
|
|
setRoles(Array.isArray(roles) ? roles : [])
|
|
|
|
|
}
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const options = roles?.map((r) => ({ value: r.id, label: r.caption })) ?? []
|
|
|
|
|
setColumns([
|
2021-12-30 10:25:42 +05:00
|
|
|
|
makeColumn('Название', 'caption', { width: 200, editable: true, formItemRules: min1 }),
|
2021-12-15 11:01:18 +05:00
|
|
|
|
makeSelectColumn('Роль-родитель', 'idParent', options, options[0], {
|
|
|
|
|
width: 200,
|
|
|
|
|
editable: true
|
2021-12-27 18:06:26 +05:00
|
|
|
|
}, { allowClear: true }),
|
2021-12-30 10:25:42 +05:00
|
|
|
|
makeTagColumn('Разрешения', 'permissions', permissions, 'id', 'name', {
|
2021-12-15 11:01:18 +05:00
|
|
|
|
width: 200,
|
|
|
|
|
editable: true,
|
2021-12-28 14:34:12 +05:00
|
|
|
|
render: (permission) => <PermissionView info={permission} />,
|
2021-12-27 18:06:26 +05:00
|
|
|
|
}),
|
2021-12-15 11:01:18 +05:00
|
|
|
|
])
|
2021-12-27 18:06:26 +05:00
|
|
|
|
}, [roles, permissions])
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
2021-12-27 18:06:26 +05:00
|
|
|
|
useEffect(() => invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
|
|
|
|
const permissions = await AdminPermissionService.getAll()
|
|
|
|
|
setPermissions(arrayOrDefault(permissions))
|
|
|
|
|
await loadRoles()
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
|
|
|
|
`Не удалось загрузить список прав`
|
|
|
|
|
), [])
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
|
|
|
|
const handlerProps = {
|
|
|
|
|
service: AdminUserRoleService,
|
|
|
|
|
setLoader: setShowLoader,
|
|
|
|
|
errorMsg: `Не удалось выполнить операцию`,
|
2021-12-27 18:06:26 +05:00
|
|
|
|
onComplete: async () => invokeWebApiWrapperAsync(
|
|
|
|
|
loadRoles,
|
|
|
|
|
setShowLoader,
|
|
|
|
|
`Не удалось загрузить список прав`
|
|
|
|
|
)
|
2021-12-15 11:01:18 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<LoaderPortal show={showLoader}>
|
|
|
|
|
<EditableTable
|
|
|
|
|
size={'small'}
|
|
|
|
|
bordered
|
|
|
|
|
columns={columns}
|
|
|
|
|
dataSource={roles}
|
|
|
|
|
onRowAdd={makeActionHandler('insert', handlerProps)}
|
2021-12-30 10:25:42 +05:00
|
|
|
|
onRowEdit={makeActionHandler('put', handlerProps)}
|
2021-12-15 11:01:18 +05:00
|
|
|
|
onRowDelete={makeActionHandler('delete', handlerProps)}
|
|
|
|
|
/>
|
|
|
|
|
</LoaderPortal>
|
|
|
|
|
)
|
2021-12-28 14:34:12 +05:00
|
|
|
|
})
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
|
|
|
|
export default RoleController
|