Переписаны крошки WellTreeSelector

This commit is contained in:
Александр Сироткин 2021-12-23 12:42:52 +05:00
parent 2637720762
commit f5c6496d38

View File

@ -26,9 +26,36 @@ export type TreeNodeData = {
children?: TreeNodeData[] children?: TreeNodeData[]
} }
const getLabel = (wellsTree: any[], value?: string): string | undefined => {
if (!value) return value
const type = value.replaceAll('/', ' ').trim().split(' ')[0]
let deposit: any, cluster: any, well: any
switch (type) {
case 'cluster':
deposit = wellsTree.find((deposit) => (
cluster = deposit.children?.find((cluster: any) => cluster.value === value)
))
if (deposit && cluster)
return `${deposit.title} / ${cluster.title}`
break
case 'well':
deposit = wellsTree.find((deposit) => (
cluster = deposit.children?.find((cluster: any) => (
well = cluster.children?.find((well: any) => well.value === value)
))
))
if (deposit && cluster && well)
return `${deposit.title} / ${cluster.title} / ${well.title}`
break
default: break
}
return value
}
export const WellTreeSelector = () => { export const WellTreeSelector = () => {
const [wellsTree, setWellsTree] = useState<any>([]) // TODO: Исправить тип (необходимо разобраться с типом значения rc-select) const [wellsTree, setWellsTree] = useState<any[]>([]) // TODO: Исправить тип (необходимо разобраться с типом значения rc-select)
const [showLoader, setShowLoader] = useState<boolean>(false) const [showLoader, setShowLoader] = useState<boolean>(false)
const [value, setValue] = useState<string>()
const history = useHistory() const history = useHistory()
const routeMatch = useRouteMatch('/:route/:id') const routeMatch = useRouteMatch('/:route/:id')
@ -39,26 +66,14 @@ export const WellTreeSelector = () => {
const wellsTree: TreeNodeData[] = deposits.map(deposit =>({ const wellsTree: TreeNodeData[] = deposits.map(deposit =>({
title: deposit.caption, title: deposit.caption,
key: `/deposit/${deposit.id}`, key: `/deposit/${deposit.id}`,
value: {
value: `/deposit/${deposit.id}`,
label: deposit.caption,
},
icon: <DepositIcon width={24} height={24}/>, icon: <DepositIcon width={24} height={24}/>,
children: deposit.clusters?.map(cluster => ({ children: deposit.clusters?.map(cluster => ({
title: cluster.caption, title: cluster.caption,
key: `/cluster/${cluster.id}`, key: `/cluster/${cluster.id}`,
value: {
value: `/cluster/${cluster.id}`,
label: `${deposit.caption} / ${cluster.caption}`,
},
icon: <ClusterIcon width={24} height={24}/>, icon: <ClusterIcon width={24} height={24}/>,
children: cluster.wells?.map(well => ({ children: cluster.wells?.map(well => ({
title: well.caption, title: well.caption,
key: `/well/${well.id}`, key: `/well/${well.id}`,
value: {
value: `/well/${well.id}`,
label: `${deposit.caption} / ${cluster.caption} / ${well.caption}`
},
icon: <WellIcon icon: <WellIcon
width={24} width={24}
height={24} height={24}
@ -69,12 +84,18 @@ export const WellTreeSelector = () => {
})), })),
})) }))
setWellsTree(wellsTree) setWellsTree(wellsTree)
setValue(getLabel(wellsTree, routeMatch?.url))
}, },
setShowLoader, setShowLoader,
`Не удалось загрузить список скважин` `Не удалось загрузить список скважин`
) )
}, []) }, [])
const onChange = (value: string): void => {
if (wellsTree)
setValue(getLabel(wellsTree, value))
}
const onSelect = (value: string): void => { const onSelect = (value: string): void => {
if (value) history.push(value) if (value) history.push(value)
} }
@ -82,7 +103,6 @@ export const WellTreeSelector = () => {
return ( return (
<LoaderPortal show={showLoader}> <LoaderPortal show={showLoader}>
<TreeSelect <TreeSelect
labelInValue
treeIcon treeIcon
className={'header-tree-select'} className={'header-tree-select'}
bordered={false} bordered={false}
@ -91,7 +111,8 @@ export const WellTreeSelector = () => {
treeData={wellsTree} treeData={wellsTree}
treeDefaultExpandAll treeDefaultExpandAll
onSelect={onSelect} onSelect={onSelect}
value = {routeMatch?.url} onChange={onChange}
value={value}
/> />
</LoaderPortal> </LoaderPortal>
) )