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'
|
|
|
|
|
import LoaderPortal from './LoaderPortal'
|
|
|
|
|
import { DepositService } from '../services/api'
|
|
|
|
|
import { invokeWebApiWrapperAsync } from './factory'
|
2021-12-21 13:03:15 +05:00
|
|
|
|
import { WellIcon, WellIconState } from './icons'
|
2021-11-19 11:46:11 +05:00
|
|
|
|
import { ReactComponent as DepositIcon } from '../images/DepositIcon.svg'
|
|
|
|
|
import { ReactComponent as ClusterIcon } from '../images/ClusterIcon.svg'
|
|
|
|
|
import { DepositDto } from '../services/api'
|
2021-11-19 18:13:28 +05:00
|
|
|
|
import { RawDate } from '../utils/DateTimeUtils'
|
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-11-19 18:13:28 +05:00
|
|
|
|
export const checkIsWellOnline = (lastTelemetryDate?: RawDate): boolean => {
|
2021-12-21 13:03:15 +05:00
|
|
|
|
if (!lastTelemetryDate || isNaN(+new Date(lastTelemetryDate))) return false
|
2021-11-19 11:46:11 +05:00
|
|
|
|
return Date.now() - +new Date(lastTelemetryDate) < 600_000
|
|
|
|
|
}
|
|
|
|
|
|
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-11-29 15:15:58 +05:00
|
|
|
|
export const WellTreeSelector = () => {
|
2021-12-21 13:03:15 +05:00
|
|
|
|
const [wellsTree, setWellsTree] = useState<any>([]) // TODO: Исправить тип (необходимо разобраться с типом значения rc-select)
|
2021-11-19 11:46:11 +05:00
|
|
|
|
const [showLoader, setShowLoader] = useState<boolean>(false)
|
|
|
|
|
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}`,
|
2021-12-21 13:03:15 +05:00
|
|
|
|
value: {
|
|
|
|
|
value: `/deposit/${deposit.id}`,
|
|
|
|
|
label: deposit.caption,
|
|
|
|
|
},
|
2021-11-19 11:46:11 +05:00
|
|
|
|
icon: <DepositIcon width={24} height={24}/>,
|
|
|
|
|
children: deposit.clusters?.map(cluster => ({
|
|
|
|
|
title: cluster.caption,
|
|
|
|
|
key: `/cluster/${cluster.id}`,
|
2021-12-21 13:03:15 +05:00
|
|
|
|
value: {
|
|
|
|
|
value: `/cluster/${cluster.id}`,
|
|
|
|
|
label: `${deposit.caption} / ${cluster.caption}`,
|
|
|
|
|
},
|
2021-11-19 11:46:11 +05:00
|
|
|
|
icon: <ClusterIcon width={24} height={24}/>,
|
|
|
|
|
children: cluster.wells?.map(well => ({
|
|
|
|
|
title: well.caption,
|
|
|
|
|
key: `/well/${well.id}`,
|
2021-12-21 13:03:15 +05:00
|
|
|
|
value: {
|
|
|
|
|
value: `/well/${well.id}`,
|
|
|
|
|
label: `${deposit.caption} / ${cluster.caption} / ${well.caption}`
|
|
|
|
|
},
|
2021-11-19 11:46:11 +05:00
|
|
|
|
icon: <WellIcon
|
|
|
|
|
width={24}
|
|
|
|
|
height={24}
|
|
|
|
|
state={getWellState(well.idState)}
|
|
|
|
|
online={checkIsWellOnline(well.lastTelemetryDate)}
|
|
|
|
|
/>
|
|
|
|
|
})),
|
|
|
|
|
})),
|
|
|
|
|
}))
|
|
|
|
|
setWellsTree(wellsTree)
|
|
|
|
|
},
|
|
|
|
|
setShowLoader,
|
|
|
|
|
`Не удалось загрузить список скважин`
|
|
|
|
|
)
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const onSelect = (value: string): void => {
|
|
|
|
|
if (value) history.push(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<LoaderPortal show={showLoader}>
|
|
|
|
|
<TreeSelect
|
2021-12-21 13:03:15 +05:00
|
|
|
|
labelInValue
|
2021-11-19 11:46:11 +05:00
|
|
|
|
treeIcon
|
|
|
|
|
className={'header-tree-select'}
|
|
|
|
|
bordered={false}
|
|
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
|
placeholder={'Выберите месторождение'}
|
|
|
|
|
treeData={wellsTree}
|
|
|
|
|
treeDefaultExpandAll
|
|
|
|
|
onSelect={onSelect}
|
|
|
|
|
value = {routeMatch?.url}
|
|
|
|
|
/>
|
|
|
|
|
</LoaderPortal>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default WellTreeSelector
|