diff --git a/src/components/Table/EditableTable.jsx b/src/components/Table/EditableTable.jsx index 5715e48..a81d8f7 100644 --- a/src/components/Table/EditableTable.jsx +++ b/src/components/Table/EditableTable.jsx @@ -6,6 +6,13 @@ import { invokeWebApiWrapperAsync } from '../factory' const newRowKeyValue = 'newRow' +const actions = [ + [['insert'], (data) => [data]], + [['insertRange'], (data) => [[data].flat(1)]], + [['update', 'put'], (data) => data.id && [data.id, data]], + [['delete'], (data) => data.id && [data.id]], +] + export const makeActionHandler = (action, { idWell, service, setLoader, errorMsg, onComplete }, recordParser) => service && action && ( (record) => invokeWebApiWrapperAsync( async () => { @@ -13,21 +20,11 @@ export const makeActionHandler = (action, { idWell, service, setLoader, errorMsg if (typeof recordParser === 'function') record = recordParser(record) - if (action === 'insert') { - record.key = Date.now() - await service.insert(...addIdWell(record)) - } else if (action === 'insertRange') { - if (!Array.isArray(record)) - record = [record] - await service.insertRange(...addIdWell(record)) - } else if (record.id) { - if (action === 'update') - await service.put(...addIdWell(record.id, record)) - else if (action === 'delete') - await service.delete(...addIdWell(record.id)) - } - if (onComplete) - await onComplete() + const actionId = actions.findIndex((elm) => elm[0].includes(action)) + const params = actions[actionId]?.[1](record) + + if (params) await service[action](...addIdWell(...params)) + await onComplete?.() }, setLoader, errorMsg @@ -208,7 +205,7 @@ export const EditableTable = ({ const mergedColumns = [...columns.map(handleColumn), operationColumn] return ( -
+ { onSelect={onSelect} onChange={onChange} value={value} + style={{ width: '350px' }} /> ) diff --git a/src/pages/AdminPanel/ClusterController.jsx b/src/pages/AdminPanel/ClusterController.jsx index 776cbd0..e0fdc4e 100644 --- a/src/pages/AdminPanel/ClusterController.jsx +++ b/src/pages/AdminPanel/ClusterController.jsx @@ -11,7 +11,7 @@ import { } from '../../components/Table' import { AdminClusterService, AdminDepositService } from '../../services/api' import { arrayOrDefault } from '../../utils' -import { min3 } from '../../utils/validationRules' +import { min1 } from '../../utils/validationRules' import { coordsFixed } from './DepositController' @@ -30,7 +30,7 @@ export default function ClusterController() { width: 200, editable: true, sorter: makeStringSorter('caption'), - formItemRules: min3, + formItemRules: min1, }), makeColumn('Широта', 'latitude', { width: 150, editable: true, render: coordsFixed }), makeColumn('Долгота', 'longitude', { width: 150, editable: true, render: coordsFixed }), @@ -72,7 +72,7 @@ export default function ClusterController() { columns={clusterColumns} dataSource={clusters} onRowAdd={makeActionHandler('insert', handlerProps)} - onRowEdit={makeActionHandler('update', handlerProps)} + onRowEdit={makeActionHandler('put', handlerProps)} onRowDelete={makeActionHandler('delete', handlerProps)} /> diff --git a/src/pages/AdminPanel/CompanyController.jsx b/src/pages/AdminPanel/CompanyController.jsx index b40df95..0d5a27e 100644 --- a/src/pages/AdminPanel/CompanyController.jsx +++ b/src/pages/AdminPanel/CompanyController.jsx @@ -5,14 +5,14 @@ import LoaderPortal from '../../components/LoaderPortal' import { EditableTable, makeColumn, makeActionHandler, makeStringSorter } from '../../components/Table' import { AdminCompanyService } from '../../services/api' import { arrayOrDefault } from '../../utils' -import { min3 } from '../../utils/validationRules' +import { min1 } from '../../utils/validationRules' const companyColumns = [ makeColumn('Название', 'caption', { width: 200, editable: true, sorter: makeStringSorter('caption'), - formItemRules: min3, + formItemRules: min1, }), makeColumn('Тип компании', 'companyTypeCaption', { width: 200, diff --git a/src/pages/AdminPanel/DepositController.jsx b/src/pages/AdminPanel/DepositController.jsx index 941a59e..c37fd04 100644 --- a/src/pages/AdminPanel/DepositController.jsx +++ b/src/pages/AdminPanel/DepositController.jsx @@ -4,12 +4,12 @@ import LoaderPortal from '../../components/LoaderPortal' import { EditableTable, makeColumn, makeActionHandler } from '../../components/Table' import { AdminDepositService } from '../../services/api' import { arrayOrDefault } from '../../utils' -import { min3 } from '../../utils/validationRules' +import { min1 } from '../../utils/validationRules' export const coordsFixed = (coords) => coords?.toPrecision(10) const depositColumns = [ - makeColumn('Название', 'caption', { width: 200, editable: true, formItemRules: min3 }), + makeColumn('Название', 'caption', { width: 200, editable: true, formItemRules: min1 }), makeColumn('Широта', 'latitude', { width: 150, editable: true, render: coordsFixed }), makeColumn('Долгота', 'longitude', { width: 150, editable: true, render: coordsFixed }) ] @@ -44,7 +44,7 @@ export default function DepositController() { columns={depositColumns} dataSource={deposits} onRowAdd={makeActionHandler('insert', handlerProps)} - onRowEdit={makeActionHandler('update', handlerProps)} + onRowEdit={makeActionHandler('put', handlerProps)} onRowDelete={makeActionHandler('delete', handlerProps)} /> diff --git a/src/pages/AdminPanel/PermissionController.jsx b/src/pages/AdminPanel/PermissionController.jsx index b40a596..4e7bb17 100644 --- a/src/pages/AdminPanel/PermissionController.jsx +++ b/src/pages/AdminPanel/PermissionController.jsx @@ -10,13 +10,14 @@ import { } from '../../components/Table' import { AdminPermissionService } from '../../services/api' import { arrayOrDefault } from '../../utils' -import { min3 } from '../../utils/validationRules' +import { min1 } from '../../utils/validationRules' const columns = [ makeColumn('Название', 'name', { editable: true, sorter: makeStringSorter('name'), - formItemRules: min3, + isRequired: true, + formItemRules: min1, }), makeColumn('Описание', 'description', { editable: true, diff --git a/src/pages/AdminPanel/RoleController.jsx b/src/pages/AdminPanel/RoleController.jsx index 7411de9..00da065 100644 --- a/src/pages/AdminPanel/RoleController.jsx +++ b/src/pages/AdminPanel/RoleController.jsx @@ -6,7 +6,7 @@ import { invokeWebApiWrapperAsync } from '../../components/factory' import { EditableTable, makeActionHandler, makeColumn, makeSelectColumn, makeTagColumn } from '../../components/Table' import { AdminPermissionService, AdminUserRoleService } from '../../services/api' import { arrayOrDefault } from '../../utils' -import { min3 } from '../../utils/validationRules' +import { min1 } from '../../utils/validationRules' export const RoleController = memo(() => { const [permissions, setPermissions] = useState([]) @@ -22,12 +22,12 @@ export const RoleController = memo(() => { useEffect(() => { const options = roles?.map((r) => ({ value: r.id, label: r.caption })) ?? [] setColumns([ - makeColumn('Название', 'caption', { width: 200, editable: true, formItemRules: min3 }), + makeColumn('Название', 'caption', { width: 200, editable: true, formItemRules: min1 }), makeSelectColumn('Роль-родитель', 'idParent', options, options[0], { width: 200, editable: true }, { allowClear: true }), - makeTagColumn('Права доступа', 'permissions', permissions, 'id', 'name', { + makeTagColumn('Разрешения', 'permissions', permissions, 'id', 'name', { width: 200, editable: true, render: (permission) => , @@ -64,7 +64,7 @@ export const RoleController = memo(() => { columns={columns} dataSource={roles} onRowAdd={makeActionHandler('insert', handlerProps)} - onRowEdit={makeActionHandler('update', handlerProps)} + onRowEdit={makeActionHandler('put', handlerProps)} onRowDelete={makeActionHandler('delete', handlerProps)} /> diff --git a/src/pages/AdminPanel/UserController.jsx b/src/pages/AdminPanel/UserController.jsx index b84ba44..5f10bde 100644 --- a/src/pages/AdminPanel/UserController.jsx +++ b/src/pages/AdminPanel/UserController.jsx @@ -29,8 +29,6 @@ const RoleTag = memo(({ roles, value, onChange }) => { }))) }, [roles]) - console.log(value) - return (