Методы добавления/удаления/редактирования изменены на фабрику

This commit is contained in:
goodmice 2021-12-07 19:36:04 +05:00
parent 09a068ab04
commit cd57cba6f8
6 changed files with 69 additions and 108 deletions

View File

@ -2,9 +2,26 @@ import { Form, Table, Button, Popconfirm } from "antd"
import { EditOutlined, SaveOutlined, PlusOutlined, CloseCircleOutlined, DeleteOutlined } from '@ant-design/icons' import { EditOutlined, SaveOutlined, PlusOutlined, CloseCircleOutlined, DeleteOutlined } from '@ant-design/icons'
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { EditableCell } from './EditableCell' import { EditableCell } from './EditableCell'
import { invokeWebApiWrapperAsync } from "../factory";
const newRowKeyValue = 'newRow' const newRowKeyValue = 'newRow'
export const makeActionHandler = (action, { service, setLoader, errorMsg, onComplete }) => service && action && ((record) => invokeWebApiWrapperAsync(
async () => {
if (action === 'insert') {
await service.insert(record)
} else if (record.id) {
if (action === 'update')
await service.delete(record.id)
else if (action === 'delete')
await service.put(record.id, record)
}
onComplete?.()
},
setLoader,
errorMsg
))
export const tryAddKeys = (items) => { export const tryAddKeys = (items) => {
if(!items?.length || !items[0]) if(!items?.length || !items[0])
return [] return []

View File

@ -1,10 +1,10 @@
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { invokeWebApiWrapperAsync } from '../../components/factory' import { invokeWebApiWrapperAsync } from '../../components/factory'
import LoaderPortal from '../../components/LoaderPortal' import LoaderPortal from '../../components/LoaderPortal'
import { EditableTable, makeColumn, makeSelectColumn } from '../../components/Table' import { EditableTable, makeColumn, makeSelectColumn, makeActionHandler } from '../../components/Table'
import { AdminClusterService, AdminDepositService } from '../../services/api' import { AdminClusterService, AdminDepositService } from '../../services/api'
export const ClusterController = () => { export default function ClusterController() {
const [deposits, setDeposits] = useState([]) const [deposits, setDeposits] = useState([])
const [clusters, setClusters] = useState([]) const [clusters, setClusters] = useState([])
const [showLoader, setShowLoader] = useState(false) const [showLoader, setShowLoader] = useState(false)
@ -19,8 +19,6 @@ export const ClusterController = () => {
const updateTable = () => invokeWebApiWrapperAsync( const updateTable = () => invokeWebApiWrapperAsync(
async () => { async () => {
const clusters = await AdminClusterService.getAll() const clusters = await AdminClusterService.getAll()
if (!clusters)
throw Error(`Не удалось загрузить список кустов`)
setClusters(clusters) setClusters(clusters)
}, },
setShowLoader, setShowLoader,
@ -39,21 +37,11 @@ export const ClusterController = () => {
useEffect(updateTable, []) useEffect(updateTable, [])
const onAdd = async (cluster) => { const handlerProps = {
await AdminClusterService.insert(cluster) service: AdminClusterService,
updateTable() setLoader: setShowLoader,
} errorMsg: `Не удалось выполнить операцию`,
onComplete: updateTable,
const onEdit = async (cluster) => {
if (!cluster.id) return
await AdminClusterService.put(cluster.id, cluster)
updateTable()
}
const onDelete = async (cluster) => {
if (!cluster.id) return
await AdminClusterService.delete(cluster.id)
updateTable()
} }
return ( return (
@ -63,9 +51,9 @@ export const ClusterController = () => {
bordered bordered
columns={clusterColumns} columns={clusterColumns}
dataSource={clusters} dataSource={clusters}
onRowAdd={onAdd} onRowAdd={makeActionHandler('insert', handlerProps)}
onRowEdit={onEdit} onRowEdit={makeActionHandler('update', handlerProps)}
onRowDelete={onDelete} onRowDelete={makeActionHandler('delete', handlerProps)}
/> />
</LoaderPortal> </LoaderPortal>
) )

View File

@ -1,7 +1,7 @@
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { invokeWebApiWrapperAsync } from '../../components/factory' import { invokeWebApiWrapperAsync } from '../../components/factory'
import LoaderPortal from '../../components/LoaderPortal' import LoaderPortal from '../../components/LoaderPortal'
import { EditableTable, makeColumn } from '../../components/Table' import { EditableTable, makeColumn, makeActionHandler } from '../../components/Table'
import { AdminCompanyService } from '../../services/api' import { AdminCompanyService } from '../../services/api'
const companyColumns = [ const companyColumns = [
@ -9,14 +9,13 @@ const companyColumns = [
makeColumn('Тип компании', 'companyTypeCaption', { width: 200, editable: true }) makeColumn('Тип компании', 'companyTypeCaption', { width: 200, editable: true })
] ]
export const CompanyController = () => { export default function CompanyController() {
const [companies, setCompanies] = useState([]) const [companies, setCompanies] = useState([])
const [showLoader, setShowLoader] = useState(false) const [showLoader, setShowLoader] = useState(false)
const updateTable = () => invokeWebApiWrapperAsync( const updateTable = () => invokeWebApiWrapperAsync(
async() => { async() => {
const companies = await AdminCompanyService.getAll() const companies = await AdminCompanyService.getAll()
if (!companies) throw Error(`Не удалось загрузить список кустов`)
setCompanies(companies) setCompanies(companies)
}, },
setShowLoader, setShowLoader,
@ -25,21 +24,11 @@ export const CompanyController = () => {
useEffect(updateTable, []) useEffect(updateTable, [])
const onAdd = async (company) => { const handlerProps = {
await AdminCompanyService.insert(company) service: AdminCompanyService,
updateTable() setLoader: setShowLoader,
} errorMsg: `Не удалось выполнить операцию`,
onComplete: updateTable,
const onEdit = async (company) => {
if(!company.id) return
await AdminCompanyService.put(company.id, company)
updateTable()
}
const onDelete = async (company) => {
if(!company.id) return
await AdminCompanyService.delete(company.id)
updateTable()
} }
return ( return (
@ -49,9 +38,9 @@ export const CompanyController = () => {
bordered bordered
columns={companyColumns} columns={companyColumns}
dataSource={companies} dataSource={companies}
onRowAdd={onAdd} onRowAdd={makeActionHandler('insert', handlerProps)}
onRowEdit={onEdit} onRowEdit={makeActionHandler('update', handlerProps)}
onRowDelete={onDelete} onRowDelete={makeActionHandler('delete', handlerProps)}
/> />
</LoaderPortal> </LoaderPortal>
) )

View File

@ -1,7 +1,7 @@
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { invokeWebApiWrapperAsync } from '../../components/factory' import { invokeWebApiWrapperAsync } from '../../components/factory'
import LoaderPortal from '../../components/LoaderPortal' import LoaderPortal from '../../components/LoaderPortal'
import { EditableTable, makeColumn } from '../../components/Table' import { EditableTable, makeColumn, makeActionHandler } from '../../components/Table'
import { AdminDepositService } from '../../services/api' import { AdminDepositService } from '../../services/api'
const depositColumns = [ const depositColumns = [
@ -10,14 +10,13 @@ const depositColumns = [
makeColumn('Долгота', 'longitude', { width: 150, editable: true }) makeColumn('Долгота', 'longitude', { width: 150, editable: true })
] ]
export const DepositController = () => { export default function DepositController() {
const [deposits, setDeposits] = useState([]) const [deposits, setDeposits] = useState([])
const [showLoader, setShowLoader] = useState(false) const [showLoader, setShowLoader] = useState(false)
const updateTable = () => invokeWebApiWrapperAsync( const updateTable = () => invokeWebApiWrapperAsync(
async() => { async() => {
const deposits = await AdminDepositService.getAll() const deposits = await AdminDepositService.getAll()
if (!deposits) throw Error(`Не удалось загрузить список месторождении`)
setDeposits(deposits) setDeposits(deposits)
}, },
setShowLoader, setShowLoader,
@ -26,21 +25,11 @@ export const DepositController = () => {
useEffect(updateTable, []) useEffect(updateTable, [])
const onAdd = async (deposit) => { const handlerProps = {
await AdminDepositService.insert(deposit) service: AdminDepositService,
updateTable() setLoader: setShowLoader,
} errorMsg: `Не удалось выполнить операцию`,
onComplete: updateTable,
const onEdit = async (deposit) => {
if(!deposit.id) return
await AdminDepositService.put(deposit.id, deposit)
updateTable()
}
const onDelete = async (deposit) => {
if(!deposit.id) return
await AdminDepositService.delete(deposit.id)
updateTable()
} }
return ( return (
@ -50,9 +39,9 @@ export const DepositController = () => {
bordered bordered
columns={depositColumns} columns={depositColumns}
dataSource={deposits} dataSource={deposits}
onRowAdd={onAdd} onRowAdd={makeActionHandler('insert', handlerProps)}
onRowEdit={onEdit} onRowEdit={makeActionHandler('update', handlerProps)}
onRowDelete={onDelete} onRowDelete={makeActionHandler('delete', handlerProps)}
/> />
</LoaderPortal> </LoaderPortal>
) )

View File

@ -1,11 +1,11 @@
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { invokeWebApiWrapperAsync } from '../../components/factory' import { invokeWebApiWrapperAsync } from '../../components/factory'
import LoaderPortal from '../../components/LoaderPortal' import LoaderPortal from '../../components/LoaderPortal'
import { EditableTable, makeColumn, makeSelectColumn } from '../../components/Table' import { EditableTable, makeColumn, makeSelectColumn, makeActionHandler } from '../../components/Table'
import { AdminCompanyService, AdminUserService } from '../../services/api' import { AdminCompanyService, AdminUserService } from '../../services/api'
import { loginRules, nameRules, phoneRules, emailRules } from '../../utils/validationRules' import { createLoginRules, nameRules, phoneRules, emailRules } from '../../utils/validationRules'
export const UserController = () => { export default function UserController() {
const [companies, setCompanies] = useState([]) const [companies, setCompanies] = useState([])
const [users, setUsers] = useState([]) const [users, setUsers] = useState([])
const [showLoader, setShowLoader] = useState(false) const [showLoader, setShowLoader] = useState(false)
@ -13,7 +13,7 @@ export const UserController = () => {
const userColumns = [ const userColumns = [
makeColumn('Логин', 'login', { makeColumn('Логин', 'login', {
editable: true, editable: true,
formItemRules: [{ required: true }, ...loginRules] formItemRules: [{ required: true }, ...createLoginRules]
}), }),
makeColumn('Фамилия', 'surname', { makeColumn('Фамилия', 'surname', {
editable: true, editable: true,
@ -33,7 +33,6 @@ export const UserController = () => {
const updateTable = () => invokeWebApiWrapperAsync( const updateTable = () => invokeWebApiWrapperAsync(
async() => { async() => {
const users = await AdminUserService.getAll() const users = await AdminUserService.getAll()
if (!users) throw Error(`Не удалось загрузить список пользователей`)
setUsers(users) setUsers(users)
}, },
setShowLoader, setShowLoader,
@ -52,21 +51,11 @@ export const UserController = () => {
useEffect(updateTable, []) useEffect(updateTable, [])
const onAdd = async (user) => { const handlerProps = {
await AdminUserService.insert(user) service: AdminUserService,
updateTable() setLoader: setShowLoader,
} errorMsg: `Не удалось выполнить операцию`,
onComplete: updateTable
const onEdit = async (user) => {
if(!user.id) return
await AdminUserService.put(user.id, user)
updateTable()
}
const onDelete = async (user) => {
if(!user.id) return
await AdminUserService.delete(user.id)
updateTable()
} }
return ( return (
@ -76,9 +65,9 @@ export const UserController = () => {
bordered bordered
columns={userColumns} columns={userColumns}
dataSource={users} dataSource={users}
onRowAdd={onAdd} onRowAdd={makeActionHandler('insert', handlerProps)}
onRowEdit={onEdit} onRowEdit={makeActionHandler('update', handlerProps)}
onRowDelete={onDelete} onRowDelete={makeActionHandler('delete', handlerProps)}
/> />
</LoaderPortal> </LoaderPortal>
) )

View File

@ -1,7 +1,7 @@
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { invokeWebApiWrapperAsync } from '../../components/factory' import { invokeWebApiWrapperAsync } from '../../components/factory'
import LoaderPortal from '../../components/LoaderPortal' import LoaderPortal from '../../components/LoaderPortal'
import { EditableTable, makeColumn, makeSelectColumn } from '../../components/Table' import { EditableTable, makeColumn, makeSelectColumn, makeActionHandler } from '../../components/Table'
import { AdminClusterService, AdminWellService } from '../../services/api' import { AdminClusterService, AdminWellService } from '../../services/api'
const wellTypes = [ const wellTypes = [
@ -9,7 +9,7 @@ const wellTypes = [
{ value: 2, label: 'Горизонтальная' } { value: 2, label: 'Горизонтальная' }
] ]
export const WellController = () => { export default function WellController() {
const [clusters, setClusters] = useState([]) const [clusters, setClusters] = useState([])
const [wells, setWells] = useState([]) const [wells, setWells] = useState([])
const [showLoader, setShowLoader] = useState(false) const [showLoader, setShowLoader] = useState(false)
@ -26,7 +26,6 @@ export const WellController = () => {
const updateTable = () => invokeWebApiWrapperAsync( const updateTable = () => invokeWebApiWrapperAsync(
async () => { async () => {
const wells = await AdminWellService.getAll() const wells = await AdminWellService.getAll()
if (!wells) throw Error(`Не удалось загрузить список скважин`)
setWells(wells) setWells(wells)
}, },
setShowLoader, setShowLoader,
@ -45,21 +44,11 @@ export const WellController = () => {
useEffect(updateTable, []) useEffect(updateTable, [])
const onAdd = async (well) => { const handlerProps = {
await AdminWellService.insert(well) service: AdminWellService,
updateTable() setLoader: setShowLoader,
} errorMsg: `Не удалось выполнить операцию`,
onComplete: updateTable
const onEdit = async (well) => {
if (!well.id) return
await AdminWellService.put(well.id, well)
updateTable()
}
const onDelete = async (well) => {
if (!well.id) return
await AdminWellService.delete(well.id)
updateTable()
} }
return ( return (
@ -69,9 +58,9 @@ export const WellController = () => {
bordered bordered
columns={wellColumns} columns={wellColumns}
dataSource={wells} dataSource={wells}
onRowAdd={onAdd} onRowAdd={makeActionHandler('insert', handlerProps)}
onRowEdit={onEdit} onRowEdit={makeActionHandler('update', handlerProps)}
onRowDelete={onDelete} onRowDelete={makeActionHandler('delete', handlerProps)}
/> />
</LoaderPortal> </LoaderPortal>
) )