2021-05-12 17:53:35 +05:00
|
|
|
|
import { useState, useEffect } from 'react'
|
2021-07-22 14:22:18 +05:00
|
|
|
|
import { useRouteMatch } from 'react-router-dom'
|
|
|
|
|
import { DepositService } from '../services/api'
|
|
|
|
|
import LoaderPortal from './LoaderPortal'
|
2021-05-19 16:05:01 +05:00
|
|
|
|
import { TreeSelect } from 'antd'
|
2021-05-12 17:53:35 +05:00
|
|
|
|
import { useHistory } from 'react-router-dom'
|
2021-05-31 15:49:50 +05:00
|
|
|
|
import notify from './notify'
|
2021-08-17 13:00:03 +05:00
|
|
|
|
import '../styles/wellTreeSelect.css'
|
2021-05-12 17:53:35 +05:00
|
|
|
|
|
2021-07-26 10:16:20 +05:00
|
|
|
|
export default function WellTreeSelector() {
|
2021-05-19 16:05:01 +05:00
|
|
|
|
const [wellsTree, setWellsTree] = useState([])
|
2021-07-22 14:22:18 +05:00
|
|
|
|
const [showLoader, setShowLoader] = useState(false)
|
2021-05-19 16:05:01 +05:00
|
|
|
|
const history = useHistory()
|
2021-07-22 14:22:18 +05:00
|
|
|
|
const routeMatch = useRouteMatch('/:route/:id')
|
2021-05-19 16:05:01 +05:00
|
|
|
|
|
2021-07-22 14:22:18 +05:00
|
|
|
|
const updateWellsList = async () => {
|
|
|
|
|
setShowLoader(true)
|
2021-05-19 16:05:01 +05:00
|
|
|
|
try {
|
2021-07-22 14:22:18 +05:00
|
|
|
|
const deposits = await DepositService.getDeposits()
|
|
|
|
|
const wellsTree = deposits.map(deposit =>({
|
|
|
|
|
title: deposit.caption,
|
|
|
|
|
key: `/deposit/${deposit.id}`,
|
|
|
|
|
value: `/deposit/${deposit.id}`,
|
|
|
|
|
children: deposit.clusters.map(cluster => ({
|
|
|
|
|
title: cluster.caption,
|
|
|
|
|
key: `/cluster/${cluster.id}`,
|
|
|
|
|
value: `/cluster/${cluster.id}`,
|
|
|
|
|
children: cluster.wells.map(well => ({
|
|
|
|
|
title: well.caption,
|
|
|
|
|
key: `/well/${well.id}`,
|
|
|
|
|
value: `/well/${well.id}`,
|
|
|
|
|
})),
|
|
|
|
|
})),
|
|
|
|
|
}))
|
2021-05-19 16:05:01 +05:00
|
|
|
|
setWellsTree(wellsTree)
|
|
|
|
|
}
|
|
|
|
|
catch (e) {
|
2021-05-31 15:21:37 +05:00
|
|
|
|
notify('Не удалось загрузить список скважин', 'error')
|
2021-07-22 14:22:18 +05:00
|
|
|
|
console.error(`${e.message}`)
|
2021-05-19 16:05:01 +05:00
|
|
|
|
}
|
2021-07-22 14:22:18 +05:00
|
|
|
|
setShowLoader(false)
|
2021-05-19 16:05:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => { updateWellsList() }, [])
|
|
|
|
|
|
2021-07-22 14:22:18 +05:00
|
|
|
|
const onSelect = (value, node) => {
|
2021-05-19 16:05:01 +05:00
|
|
|
|
if (value)
|
2021-07-22 14:22:18 +05:00
|
|
|
|
history.push(value);
|
2021-05-19 16:05:01 +05:00
|
|
|
|
console.log(value)
|
|
|
|
|
}
|
2021-05-12 17:53:35 +05:00
|
|
|
|
|
2021-05-19 16:05:01 +05:00
|
|
|
|
return (
|
2021-07-22 14:22:18 +05:00
|
|
|
|
<LoaderPortal show={showLoader}>
|
2021-05-19 16:05:01 +05:00
|
|
|
|
<TreeSelect
|
2021-05-31 15:02:35 +05:00
|
|
|
|
className='header-tree-select'
|
|
|
|
|
bordered={false}
|
|
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
|
placeholder='Выберите месторождение'
|
2021-05-19 16:05:01 +05:00
|
|
|
|
treeData={wellsTree}
|
|
|
|
|
treeDefaultExpandAll
|
|
|
|
|
onSelect={onSelect}
|
2021-07-22 14:22:18 +05:00
|
|
|
|
value = {routeMatch?.url}
|
2021-05-19 16:05:01 +05:00
|
|
|
|
/>
|
2021-07-22 14:22:18 +05:00
|
|
|
|
</LoaderPortal>
|
2021-05-19 16:05:01 +05:00
|
|
|
|
)
|
2021-05-12 17:53:35 +05:00
|
|
|
|
}
|