2021-12-02 15:21:10 +05:00
|
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
|
import { invokeWebApiWrapperAsync } from '../../components/factory'
|
|
|
|
|
import LoaderPortal from '../../components/LoaderPortal'
|
2021-12-06 14:10:42 +05:00
|
|
|
|
import { EditableTable, makeColumn, makeSelectColumn } from '../../components/Table'
|
|
|
|
|
import { AdminClusterService, AdminDepositService } from '../../services/api'
|
2021-12-02 15:21:10 +05:00
|
|
|
|
|
|
|
|
|
export const ClusterController = () => {
|
|
|
|
|
const [deposits, setDeposits] = useState([])
|
|
|
|
|
const [clusters, setClusters] = useState([])
|
|
|
|
|
const [showLoader, setShowLoader] = useState(false)
|
|
|
|
|
|
|
|
|
|
const clusterColumns = [
|
|
|
|
|
makeSelectColumn('Месторождение', 'idDeposit', deposits, '--', { width: 200, editable: true }),
|
|
|
|
|
makeColumn('Название', 'caption', { width: 200, editable: true }),
|
2021-12-06 14:10:42 +05:00
|
|
|
|
makeColumn('Широта', 'latitude', { width: 150, editable: true }),
|
|
|
|
|
makeColumn('Долгота', 'longitude', { width: 150, editable: true })
|
2021-12-02 15:21:10 +05:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const updateTable = () => invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
2021-12-06 14:10:42 +05:00
|
|
|
|
const clusters = await AdminClusterService.getAll()
|
|
|
|
|
if (!clusters)
|
2021-12-02 15:21:10 +05:00
|
|
|
|
throw Error(`Не удалось загрузить список кустов`)
|
2021-12-06 14:10:42 +05:00
|
|
|
|
setClusters(clusters)
|
2021-12-02 15:21:10 +05:00
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
|
|
|
|
`Не удалось загрузить список кустов`
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
useEffect(() => invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
2021-12-06 14:10:42 +05:00
|
|
|
|
let deposits = await AdminDepositService.getAll()
|
2021-12-02 15:46:47 +05:00
|
|
|
|
deposits = deposits?.map((deposit) => ({ value: deposit.id, label: deposit.caption }))
|
2021-12-02 15:21:10 +05:00
|
|
|
|
setDeposits(deposits ?? [])
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
|
|
|
|
`Не удалось загрузить список месторождений`
|
|
|
|
|
), [])
|
|
|
|
|
|
2021-12-06 14:10:42 +05:00
|
|
|
|
useEffect(updateTable, [])
|
2021-12-02 15:21:10 +05:00
|
|
|
|
|
|
|
|
|
const onAdd = async (cluster) => {
|
|
|
|
|
await AdminClusterService.insert(cluster)
|
|
|
|
|
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 (
|
|
|
|
|
<LoaderPortal show={showLoader}>
|
|
|
|
|
<EditableTable
|
|
|
|
|
size={'small'}
|
|
|
|
|
bordered
|
|
|
|
|
columns={clusterColumns}
|
|
|
|
|
dataSource={clusters}
|
|
|
|
|
onRowAdd={onAdd}
|
|
|
|
|
onRowEdit={onEdit}
|
|
|
|
|
onRowDelete={onDelete}
|
|
|
|
|
/>
|
|
|
|
|
</LoaderPortal>
|
|
|
|
|
)
|
|
|
|
|
}
|