2022-03-03 19:34:22 +05:00
|
|
|
|
import { memo, useCallback, useEffect, useState } from 'react'
|
2021-12-27 18:06:26 +05:00
|
|
|
|
|
2022-02-07 14:58:38 +05:00
|
|
|
|
import LoaderPortal from '@components/LoaderPortal'
|
|
|
|
|
import { PermissionView, RoleView } from '@components/views'
|
|
|
|
|
import { invokeWebApiWrapperAsync } from '@components/factory'
|
|
|
|
|
import { EditableTable, makeActionHandler, makeColumn, makeTagColumn } from '@components/Table'
|
|
|
|
|
import { AdminPermissionService, AdminUserRoleService } from '@api'
|
|
|
|
|
import { arrayOrDefault } from '@utils'
|
|
|
|
|
import { min1 } from '@utils/validationRules'
|
|
|
|
|
import { hasPermission } from '@utils/permissions'
|
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([])
|
|
|
|
|
|
2022-03-03 19:34:22 +05:00
|
|
|
|
const loadRoles = useCallback(async () => {
|
2021-12-27 18:06:26 +05:00
|
|
|
|
const roles = await AdminUserRoleService.getAll()
|
2022-01-13 20:32:46 +05:00
|
|
|
|
setRoles(arrayOrDefault(roles))
|
2022-03-03 19:34:22 +05:00
|
|
|
|
}, [])
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setColumns([
|
2022-03-03 19:34:22 +05:00
|
|
|
|
makeColumn('Название', 'caption', { width: 100, editable: true, formItemRules: min1 }),
|
2022-01-17 21:46:51 +05:00
|
|
|
|
makeTagColumn('Включённые роли', 'roles', roles, 'id', 'caption', {
|
2022-03-03 19:34:22 +05:00
|
|
|
|
width: 400,
|
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', {
|
2022-03-03 19:34:22 +05:00
|
|
|
|
width: 600,
|
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,
|
2022-02-07 17:44:46 +05:00
|
|
|
|
`Не удалось загрузить список ролей`,
|
|
|
|
|
'Получение списка ролей'
|
2022-03-03 19:34:22 +05:00
|
|
|
|
), [loadRoles])
|
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,
|
2022-02-07 17:44:46 +05:00
|
|
|
|
`Не удалось загрузить список ролей`,
|
|
|
|
|
'Получение списка ролей'
|
2021-12-27 18:06:26 +05:00
|
|
|
|
)
|
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}
|
2022-02-07 17:44:46 +05:00
|
|
|
|
onRowAdd={hasPermission('AdminUserRole.edit') && makeActionHandler('insert', handlerProps, null, 'Добавление роли')}
|
2022-02-07 18:42:35 +05:00
|
|
|
|
onRowEdit={hasPermission('AdminUserRole.edit') && makeActionHandler('update', handlerProps, null, 'Редактирование роли')}
|
2022-02-07 17:44:46 +05:00
|
|
|
|
onRowDelete={hasPermission('AdminUserRole.delete') && makeActionHandler('delete', handlerProps, null, 'Удаление роли')}
|
2021-12-15 11:01:18 +05:00
|
|
|
|
/>
|
|
|
|
|
</LoaderPortal>
|
|
|
|
|
)
|
2021-12-28 14:34:12 +05:00
|
|
|
|
})
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
|
|
|
|
export default RoleController
|