2021-11-19 11:46:11 +05:00
|
|
|
|
import { TreeSelect } from 'antd'
|
2021-12-21 13:03:15 +05:00
|
|
|
|
import { DefaultValueType } from 'rc-tree-select/lib/interface'
|
|
|
|
|
import { useState, useEffect, ReactNode } from 'react'
|
2021-11-19 11:46:11 +05:00
|
|
|
|
import { useHistory, useRouteMatch } from 'react-router-dom'
|
2021-12-27 15:31:37 +05:00
|
|
|
|
|
2021-12-27 15:58:31 +05:00
|
|
|
|
import { isRawDate } from '../utils'
|
2021-12-27 15:31:37 +05:00
|
|
|
|
import LoaderPortal from './LoaderPortal'
|
|
|
|
|
import { WellIcon, WellIconState } from './icons'
|
2021-12-27 15:58:31 +05:00
|
|
|
|
import { invokeWebApiWrapperAsync } from './factory'
|
|
|
|
|
import { DepositService, DepositDto } from '../services/api'
|
|
|
|
|
|
|
|
|
|
import { ReactComponent as DepositIcon } from '../images/DepositIcon.svg'
|
|
|
|
|
import { ReactComponent as ClusterIcon } from '../images/ClusterIcon.svg'
|
2021-12-27 15:31:37 +05:00
|
|
|
|
|
2021-12-21 13:03:15 +05:00
|
|
|
|
import '../styles/wellTreeSelect.css'
|
2021-11-19 11:46:11 +05:00
|
|
|
|
|
|
|
|
|
export const getWellState = (idState?: number): WellIconState => idState === 1 ? 'active' : 'unknown'
|
2021-12-27 15:58:31 +05:00
|
|
|
|
export const checkIsWellOnline = (lastTelemetryDate: unknown): boolean =>
|
|
|
|
|
isRawDate(lastTelemetryDate) && (Date.now() - +new Date(lastTelemetryDate) < 600_000)
|
2021-11-19 11:46:11 +05:00
|
|
|
|
|
2021-12-21 13:03:15 +05:00
|
|
|
|
export type TreeNodeData = {
|
2021-11-19 11:46:11 +05:00
|
|
|
|
title?: string | null
|
|
|
|
|
key?: string
|
2021-12-21 13:03:15 +05:00
|
|
|
|
value?: DefaultValueType
|
|
|
|
|
icon?: ReactNode
|
2021-11-19 11:46:11 +05:00
|
|
|
|
children?: TreeNodeData[]
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-27 15:58:31 +05:00
|
|
|
|
const getLabel = (wellsTree: TreeNodeData[], value?: string): string | undefined => {
|
2021-12-23 12:42:52 +05:00
|
|
|
|
if (!value) return value
|
|
|
|
|
const type = value.replaceAll('/', ' ').trim().split(' ')[0]
|
2021-12-27 15:58:31 +05:00
|
|
|
|
let deposit: TreeNodeData | undefined
|
|
|
|
|
let cluster: TreeNodeData | undefined
|
|
|
|
|
let well: TreeNodeData | undefined
|
2021-12-23 12:42:52 +05:00
|
|
|
|
switch (type) {
|
|
|
|
|
case 'cluster':
|
|
|
|
|
deposit = wellsTree.find((deposit) => (
|
2021-12-27 15:58:31 +05:00
|
|
|
|
cluster = deposit.children?.find((cluster: TreeNodeData) => cluster.key === value)
|
2021-12-23 12:42:52 +05:00
|
|
|
|
))
|
|
|
|
|
if (deposit && cluster)
|
|
|
|
|
return `${deposit.title} / ${cluster.title}`
|
|
|
|
|
break
|
|
|
|
|
case 'well':
|
|
|
|
|
deposit = wellsTree.find((deposit) => (
|
2021-12-27 15:58:31 +05:00
|
|
|
|
cluster = deposit.children?.find((cluster: TreeNodeData) => (
|
|
|
|
|
well = cluster.children?.find((well: TreeNodeData) => well.key === value)
|
2021-12-23 12:42:52 +05:00
|
|
|
|
))
|
|
|
|
|
))
|
|
|
|
|
if (deposit && cluster && well)
|
|
|
|
|
return `${deposit.title} / ${cluster.title} / ${well.title}`
|
|
|
|
|
break
|
|
|
|
|
default: break
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 15:15:58 +05:00
|
|
|
|
export const WellTreeSelector = () => {
|
2021-12-27 15:58:31 +05:00
|
|
|
|
const [wellsTree, setWellsTree] = useState<any[]>([]) // TODO: Исправить тип (необходимо разобраться с типом value rc-select)
|
2021-11-19 11:46:11 +05:00
|
|
|
|
const [showLoader, setShowLoader] = useState<boolean>(false)
|
2021-12-23 12:42:52 +05:00
|
|
|
|
const [value, setValue] = useState<string>()
|
2021-11-19 11:46:11 +05:00
|
|
|
|
const history = useHistory()
|
|
|
|
|
const routeMatch = useRouteMatch('/:route/:id')
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
|
|
|
|
const deposits: Array<DepositDto> = await DepositService.getDeposits()
|
|
|
|
|
const wellsTree: TreeNodeData[] = deposits.map(deposit =>({
|
|
|
|
|
title: deposit.caption,
|
|
|
|
|
key: `/deposit/${deposit.id}`,
|
|
|
|
|
icon: <DepositIcon width={24} height={24}/>,
|
|
|
|
|
children: deposit.clusters?.map(cluster => ({
|
|
|
|
|
title: cluster.caption,
|
|
|
|
|
key: `/cluster/${cluster.id}`,
|
|
|
|
|
icon: <ClusterIcon width={24} height={24}/>,
|
|
|
|
|
children: cluster.wells?.map(well => ({
|
|
|
|
|
title: well.caption,
|
|
|
|
|
key: `/well/${well.id}`,
|
|
|
|
|
icon: <WellIcon
|
|
|
|
|
width={24}
|
|
|
|
|
height={24}
|
|
|
|
|
state={getWellState(well.idState)}
|
|
|
|
|
online={checkIsWellOnline(well.lastTelemetryDate)}
|
|
|
|
|
/>
|
|
|
|
|
})),
|
|
|
|
|
})),
|
|
|
|
|
}))
|
|
|
|
|
setWellsTree(wellsTree)
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
|
|
|
|
`Не удалось загрузить список скважин`
|
|
|
|
|
)
|
|
|
|
|
}, [])
|
|
|
|
|
|
2021-12-29 12:02:36 +05:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
setValue(getLabel(wellsTree, routeMatch?.url))
|
|
|
|
|
}, [wellsTree, routeMatch])
|
|
|
|
|
|
2021-12-23 12:42:52 +05:00
|
|
|
|
const onChange = (value: string): void => {
|
|
|
|
|
if (wellsTree)
|
|
|
|
|
setValue(getLabel(wellsTree, value))
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 11:46:11 +05:00
|
|
|
|
const onSelect = (value: string): void => {
|
|
|
|
|
if (value) history.push(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<LoaderPortal show={showLoader}>
|
|
|
|
|
<TreeSelect
|
|
|
|
|
treeIcon
|
|
|
|
|
className={'header-tree-select'}
|
|
|
|
|
bordered={false}
|
|
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
|
placeholder={'Выберите месторождение'}
|
|
|
|
|
treeData={wellsTree}
|
|
|
|
|
treeDefaultExpandAll
|
|
|
|
|
onSelect={onSelect}
|
2021-12-23 12:42:52 +05:00
|
|
|
|
onChange={onChange}
|
|
|
|
|
value={value}
|
2021-12-30 10:25:42 +05:00
|
|
|
|
style={{ width: '350px' }}
|
2021-11-19 11:46:11 +05:00
|
|
|
|
/>
|
|
|
|
|
</LoaderPortal>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default WellTreeSelector
|