asb_cloud_front/src/pages/AdminPanel/PermissionController.jsx

69 lines
2.4 KiB
React
Raw Normal View History

import { memo, useCallback, useEffect, useState } from 'react'
import {
EditableTable,
makeActionHandler,
makeColumn,
makeStringSorter
} from '@components/Table'
import LoaderPortal from '@components/LoaderPortal'
import { invokeWebApiWrapperAsync } from '@components/factory'
import { AdminPermissionService } from '@api'
import { arrayOrDefault } from '@utils'
import { min1 } from '@utils/validationRules'
import { hasPermission } from '@utils/permissions'
const columns = [
makeColumn('Название', 'name', {
editable: true,
sorter: makeStringSorter('name'),
isRequired: true,
formItemRules: min1,
}),
makeColumn('Описание', 'description', {
editable: true,
sorter: makeStringSorter('description'),
}),
]
export const PermissionController = memo(() => {
const [showLoader, setShowLoader] = useState(false)
const [permissions, setPermissions] = useState([])
const updateTable = useCallback(async () => invokeWebApiWrapperAsync(
async () => {
const permission = await AdminPermissionService.getAll()
setPermissions(arrayOrDefault(permission))
},
setShowLoader,
`Не удалось загрузить список прав`,
'Получение списка прав'
), [])
useEffect(() => updateTable(), [updateTable])
const handlerProps = {
service: AdminPermissionService,
setLoader: setShowLoader,
errorMsg: `Не удалось выполнить операцию`,
onComplete: updateTable
}
return (
<LoaderPortal show={showLoader}>
<EditableTable
bordered
size={'small'}
columns={columns}
dataSource={permissions}
pagination={{ showSizeChanger: true }}
onRowAdd={hasPermission('AdminPermission.edit') && makeActionHandler('insert', handlerProps, null, 'Добавление права')}
onRowEdit={hasPermission('AdminPermission.edit') && makeActionHandler('update', handlerProps, null, 'Редактирование права')}
onRowDelete={hasPermission('AdminPermission.delete') && makeActionHandler('delete', handlerProps, null, 'Удаление права')}
/>
</LoaderPortal>
)
})
export default PermissionController