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'
|
2022-01-17 21:46:51 +05:00
|
|
|
|
import { PermissionView, RoleView } from '../../components/views'
|
2021-12-15 11:01:18 +05:00
|
|
|
|
import { invokeWebApiWrapperAsync } from '../../components/factory'
|
2022-01-17 21:46:51 +05:00
|
|
|
|
import { EditableTable, makeActionHandler, makeColumn, 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()
|
2022-01-13 20:32:46 +05:00
|
|
|
|
setRoles(arrayOrDefault(roles))
|
2021-12-27 18:06:26 +05:00
|
|
|
|
}
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setColumns([
|
2021-12-30 10:25:42 +05:00
|
|
|
|
makeColumn('Название', 'caption', { width: 200, editable: true, formItemRules: min1 }),
|
2022-01-17 21:46:51 +05:00
|
|
|
|
makeTagColumn('Включённые роли', 'roles', roles, 'id', 'caption', {
|
2021-12-15 11:01:18 +05:00
|
|
|
|
width: 200,
|
2022-01-17 21:46:51 +05:00
|
|
|
|
editable: true,
|
|
|
|
|
render: (role) => <RoleView role={role} />
|
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
|
|
|
|
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
|
|
|
|
|
bordered
|
2022-01-17 21:46:51 +05:00
|
|
|
|
size={'small'}
|
2021-12-15 11:01:18 +05:00
|
|
|
|
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
|