2022-03-18 17:37:05 +05:00
|
|
|
|
import { memo, useCallback, useEffect, useMemo, useState } from 'react'
|
|
|
|
|
import { Input } from 'antd'
|
2021-12-29 12:00:02 +05:00
|
|
|
|
|
2021-12-21 15:46:05 +05:00
|
|
|
|
import {
|
|
|
|
|
EditableTable,
|
|
|
|
|
makeActionHandler,
|
|
|
|
|
makeColumn,
|
|
|
|
|
makeStringSorter
|
2022-02-07 14:58:38 +05:00
|
|
|
|
} from '@components/Table'
|
|
|
|
|
import { invokeWebApiWrapperAsync } from '@components/factory'
|
|
|
|
|
import { AdminPermissionService } from '@api'
|
|
|
|
|
import { arrayOrDefault } from '@utils'
|
|
|
|
|
import { min1 } from '@utils/validationRules'
|
|
|
|
|
import { hasPermission } from '@utils/permissions'
|
2021-12-21 15:46:05 +05:00
|
|
|
|
|
|
|
|
|
const columns = [
|
|
|
|
|
makeColumn('Название', 'name', {
|
|
|
|
|
editable: true,
|
|
|
|
|
sorter: makeStringSorter('name'),
|
2021-12-30 10:25:42 +05:00
|
|
|
|
isRequired: true,
|
|
|
|
|
formItemRules: min1,
|
2021-12-21 15:46:05 +05:00
|
|
|
|
}),
|
|
|
|
|
makeColumn('Описание', 'description', {
|
|
|
|
|
editable: true,
|
|
|
|
|
sorter: makeStringSorter('description'),
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
|
2022-02-10 15:49:39 +05:00
|
|
|
|
export const PermissionController = memo(() => {
|
2021-12-21 15:46:05 +05:00
|
|
|
|
const [permissions, setPermissions] = useState([])
|
2022-03-18 17:37:05 +05:00
|
|
|
|
const [showLoader, setShowLoader] = useState(false)
|
|
|
|
|
const [searchValue, setSearchValue] = useState('')
|
2021-12-21 15:46:05 +05:00
|
|
|
|
|
2022-03-18 19:40:52 +05:00
|
|
|
|
const filteredPermissions = useMemo(() => permissions.filter((permission) => permission && (!searchValue || [
|
|
|
|
|
permission.name ?? '',
|
|
|
|
|
permission.description ?? '',
|
|
|
|
|
].join(' ').includes(searchValue.toLowerCase()))
|
|
|
|
|
), [permissions, searchValue])
|
|
|
|
|
|
2022-03-03 19:34:22 +05:00
|
|
|
|
const updateTable = useCallback(async () => invokeWebApiWrapperAsync(
|
2021-12-21 15:46:05 +05:00
|
|
|
|
async () => {
|
|
|
|
|
const permission = await AdminPermissionService.getAll()
|
2021-12-22 12:45:30 +05:00
|
|
|
|
setPermissions(arrayOrDefault(permission))
|
2021-12-21 15:46:05 +05:00
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
2022-02-07 17:44:46 +05:00
|
|
|
|
`Не удалось загрузить список прав`,
|
|
|
|
|
'Получение списка прав'
|
2022-03-03 19:34:22 +05:00
|
|
|
|
), [])
|
2021-12-21 15:46:05 +05:00
|
|
|
|
|
2022-03-03 19:34:22 +05:00
|
|
|
|
useEffect(() => updateTable(), [updateTable])
|
2021-12-21 15:46:05 +05:00
|
|
|
|
|
2022-03-18 17:37:05 +05:00
|
|
|
|
const handlerProps = useMemo(() => ({
|
2021-12-21 15:46:05 +05:00
|
|
|
|
service: AdminPermissionService,
|
|
|
|
|
setLoader: setShowLoader,
|
|
|
|
|
errorMsg: `Не удалось выполнить операцию`,
|
|
|
|
|
onComplete: updateTable
|
2022-03-18 17:37:05 +05:00
|
|
|
|
}), [updateTable])
|
2021-12-21 15:46:05 +05:00
|
|
|
|
|
|
|
|
|
return (
|
2022-03-18 17:37:05 +05:00
|
|
|
|
<>
|
|
|
|
|
<Input.Search
|
|
|
|
|
style={{ margin: '15px 0' }}
|
2022-03-18 19:40:52 +05:00
|
|
|
|
placeholder={'Введите текст для поиска (по полям: Название, Описание)...'}
|
2022-03-18 17:37:05 +05:00
|
|
|
|
onChange={(e) => setSearchValue(e.target.value)}
|
|
|
|
|
value={searchValue}
|
|
|
|
|
loading={showLoader}
|
|
|
|
|
/>
|
2021-12-21 15:46:05 +05:00
|
|
|
|
<EditableTable
|
|
|
|
|
bordered
|
|
|
|
|
size={'small'}
|
|
|
|
|
columns={columns}
|
2022-03-18 17:37:05 +05:00
|
|
|
|
loading={showLoader}
|
|
|
|
|
dataSource={filteredPermissions}
|
2022-01-12 23:58:14 +05:00
|
|
|
|
pagination={{ showSizeChanger: true }}
|
2022-02-07 17:44:46 +05:00
|
|
|
|
onRowAdd={hasPermission('AdminPermission.edit') && makeActionHandler('insert', handlerProps, null, 'Добавление права')}
|
2022-02-07 18:46:54 +05:00
|
|
|
|
onRowEdit={hasPermission('AdminPermission.edit') && makeActionHandler('update', handlerProps, null, 'Редактирование права')}
|
2022-02-07 17:44:46 +05:00
|
|
|
|
onRowDelete={hasPermission('AdminPermission.delete') && makeActionHandler('delete', handlerProps, null, 'Удаление права')}
|
2022-03-10 20:46:02 +05:00
|
|
|
|
tableName={'admin_permission_controller'}
|
2021-12-21 15:46:05 +05:00
|
|
|
|
/>
|
2022-03-18 17:37:05 +05:00
|
|
|
|
</>
|
2021-12-21 15:46:05 +05:00
|
|
|
|
)
|
2022-02-10 15:49:39 +05:00
|
|
|
|
})
|
2021-12-21 15:46:05 +05:00
|
|
|
|
|
|
|
|
|
export default PermissionController
|