2021-12-27 18:06:26 +05:00
|
|
|
|
import { Select, Tag } from 'antd'
|
|
|
|
|
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'
|
|
|
|
|
import { EditableTable, makeActionHandler, makeColumn, makeSelectColumn } from '../../components/Table'
|
2021-12-27 18:06:26 +05:00
|
|
|
|
import { AdminPermissionService, AdminUserRoleService } from '../../services/api'
|
|
|
|
|
import { arrayOrDefault } from '../../utils'
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
2021-12-27 18:06:26 +05:00
|
|
|
|
const PermissionTag = memo(({ permissions, value, onChange }) => {
|
|
|
|
|
const [options, setOptions] = useState([])
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
2021-12-27 18:06:26 +05:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
setOptions(permissions.map((elm) => ({ key: Date.now(), value: `${elm.id}`, label: elm.name })))
|
|
|
|
|
}, [permissions])
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
2021-12-27 18:06:26 +05:00
|
|
|
|
console.log({ permissions, value })
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
2021-12-27 18:06:26 +05:00
|
|
|
|
const onSelectChange = (values) => {
|
|
|
|
|
const arr = values.map((id) => permissions.find((elm) => `${elm.id}` === id))
|
|
|
|
|
onChange?.(arr)
|
2021-12-15 11:01:18 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 18:06:26 +05:00
|
|
|
|
const selectValue = value?.map((val) => `${val.id}`)
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
|
|
|
|
return (
|
2021-12-27 18:06:26 +05:00
|
|
|
|
<Select
|
|
|
|
|
showSearch
|
|
|
|
|
mode={'tags'}
|
|
|
|
|
options={options}
|
|
|
|
|
value={selectValue}
|
|
|
|
|
onChange={onSelectChange}
|
|
|
|
|
/>
|
2021-12-15 11:01:18 +05:00
|
|
|
|
)
|
2021-12-27 18:06:26 +05:00
|
|
|
|
})
|
2021-12-15 11:01:18 +05:00
|
|
|
|
|
|
|
|
|
export const RoleController = () => {
|
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([
|
|
|
|
|
makeColumn('Название', 'caption', { width: 200, editable: true }),
|
|
|
|
|
makeSelectColumn('Роль-родитель', 'idParent', options, options[0], {
|
|
|
|
|
width: 200,
|
|
|
|
|
editable: true
|
2021-12-27 18:06:26 +05:00
|
|
|
|
}, { allowClear: true }),
|
2021-12-15 11:01:18 +05:00
|
|
|
|
makeColumn('Права доступа', 'permissions', {
|
|
|
|
|
width: 200,
|
|
|
|
|
editable: true,
|
2021-12-27 18:06:26 +05:00
|
|
|
|
input: <PermissionTag permissions={permissions} />,
|
|
|
|
|
render: (item) => item?.map((elm) => (
|
|
|
|
|
<Tag key={elm.name} color={'blue'}>
|
|
|
|
|
<PermissionView info={elm} />
|
|
|
|
|
</Tag>
|
|
|
|
|
)) ?? '-',
|
|
|
|
|
}),
|
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)}
|
|
|
|
|
onRowEdit={makeActionHandler('update', handlerProps)}
|
|
|
|
|
onRowDelete={makeActionHandler('delete', handlerProps)}
|
|
|
|
|
/>
|
|
|
|
|
</LoaderPortal>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default RoleController
|