Исправлена ошибка с makeActionHandler

This commit is contained in:
Александр Сироткин 2021-12-30 10:25:42 +05:00
parent d27d3a1632
commit 7c78cb3c32
10 changed files with 38 additions and 43 deletions

View File

@ -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 (
<Form form={form} component={false}>
<Form form={form}>
<Table
components={{
body: {

View File

@ -118,6 +118,7 @@ export const WellTreeSelector = () => {
onSelect={onSelect}
onChange={onChange}
value={value}
style={{ width: '350px' }}
/>
</LoaderPortal>
)

View File

@ -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)}
/>
</LoaderPortal>

View File

@ -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,

View File

@ -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)}
/>
</LoaderPortal>

View File

@ -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,

View File

@ -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) => <PermissionView info={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)}
/>
</LoaderPortal>

View File

@ -29,8 +29,6 @@ const RoleTag = memo(({ roles, value, onChange }) => {
})))
}, [roles])
console.log(value)
return (
<Select
allowClear

View File

@ -132,7 +132,7 @@ export default function WellController() {
columns={columns}
dataSource={wells}
onRowAdd={makeActionHandler('insert', handlerProps)}
onRowEdit={makeActionHandler('update', handlerProps)}
onRowEdit={makeActionHandler('put', handlerProps)}
onRowDelete={makeActionHandler('delete', handlerProps)}
//additionalButtons={addititonalButtons}
buttonsWidth={95}

View File

@ -1,12 +1,14 @@
import { Rule } from 'rc-field-form/lib/interface'
export const min3: Rule[] = [{
min: 3,
const _min1: Rule = {
min: 1,
max: 255,
message: 'Допустимая длина 3-255 символов'
}]
message: 'Допустимая длина 1-255 символов'
}
export const createLoginRules: Rule[] = [...min3, {
export const min1: Rule[] = [_min1]
export const createLoginRules: Rule[] = [_min1, {
pattern: /^[A-Za-zА-Яа-я][А-Яа-я\w.-]+$/,
whitespace: true,
message: 'Логин должен начинаться с русской или латинской буквы, содержать только (А-яA-z0-9_-.)!'
@ -17,11 +19,7 @@ export const loginRules: Rule[] = [{
message: 'Пожалуйста, введите имя пользователя'
}]
export const nameRules: Rule[] = [{
min: 1,
max: 255,
message: 'Допустимая длина 1-255 символов!'
}, {
export const nameRules: Rule[] = [_min1, {
pattern: /^[A-zА-я][A-zА-я-]*$/,
message: 'Допустимые символы: А-яA-z-'
}]