asb_cloud_front/src/components/WellTreeSelector.jsx
2021-08-24 16:32:32 +05:00

62 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState, useEffect } from 'react'
import { DepositService } from '../services/api'
import LoaderPortal from './LoaderPortal'
import { TreeSelect } from 'antd'
import { useHistory } from 'react-router-dom'
import { notify } from "./factory"
import '../styles/wellTreeSelect.css'
export default function WellTreeSelector() {
const [wellsTree, setWellsTree] = useState([])
const [showLoader, setShowLoader] = useState(false)
const history = useHistory()
const updateWellsList = async () => {
setShowLoader(true)
try {
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}/all`,
value: `/cluster/${cluster.id}/all`,
children: cluster.wells.map(well => ({
title: well.caption,
key: `/well/${well.id}`,
value: `/well/${well.id}`,
})),
})),
}))
setWellsTree(wellsTree)
}
catch (e) {
notify('Не удалось загрузить список скважин', 'error')
console.error(`${e.message}`)
}
setShowLoader(false)
}
useEffect(() => { updateWellsList() }, [])
const onSelect = (value, node) => {
if (value)
history.push(value);
}
return (
<LoaderPortal show={showLoader}>
<TreeSelect
className='header-tree-select'
bordered={false}
dropdownMatchSelectWidth={false}
placeholder='Выберите месторождение'
treeData={wellsTree}
treeDefaultExpandAll
onSelect={onSelect}
/>
</LoaderPortal>
)
}