2021-05-12 17:53:35 +05:00
|
|
|
import { useState, useEffect } from 'react'
|
2021-04-08 12:29:19 +05:00
|
|
|
import { WellService } from '../services/api'
|
2021-04-02 17:22:34 +05:00
|
|
|
import Loader from '../components/Loader'
|
2021-05-12 17:53:35 +05:00
|
|
|
import { Table } from 'antd' // TreeSelect
|
2021-04-02 17:22:34 +05:00
|
|
|
import { useHistory } from 'react-router-dom'
|
|
|
|
|
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'Месторождение',
|
|
|
|
dataIndex: 'deposit',
|
|
|
|
key: 'deposit',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Куст',
|
|
|
|
dataIndex: 'cluster',
|
|
|
|
key: 'cluster',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Скважина',
|
|
|
|
dataIndex: 'caption',
|
|
|
|
key: 'caption',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Данные',
|
|
|
|
dataIndex: 'lastData',
|
|
|
|
key: 'lastData',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-04-16 15:49:35 +05:00
|
|
|
const groupBy = (table, ...keys) => {
|
|
|
|
let key = keys[0]
|
|
|
|
|
|
|
|
let groups = table.reduce((rv, item) => {
|
|
|
|
let keyValue = item[key]
|
|
|
|
let group = rv.find(o=>o.title === keyValue)
|
|
|
|
if(!group)
|
|
|
|
{
|
|
|
|
group = {
|
|
|
|
title: keyValue,
|
|
|
|
value: keys.length === 1 ? item : `${key} ${keyValue} ${item['id']}`,
|
|
|
|
selectable: keys.length === 1,
|
|
|
|
children:[]}
|
|
|
|
rv.push(group)
|
|
|
|
}
|
|
|
|
if(keys.length > 1)
|
|
|
|
group.children.push(item);
|
|
|
|
return rv;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if(keys.length > 1){
|
|
|
|
for(let group of groups){
|
|
|
|
group.children = groupBy(group.children, ...keys.slice(1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-04-02 17:22:34 +05:00
|
|
|
export default function Wells(props){
|
|
|
|
const [wells, setWells] = useState([])
|
2021-05-12 17:53:35 +05:00
|
|
|
const [wellsTree, setWellsTree] = useState([]) // wellsTree,
|
2021-04-02 17:22:34 +05:00
|
|
|
const [loader, setLoader] = useState(false)
|
|
|
|
const history = useHistory()
|
|
|
|
|
|
|
|
let updateWellsList = async () => {
|
|
|
|
setLoader(true)
|
|
|
|
try{
|
2021-05-12 17:53:35 +05:00
|
|
|
let newWells = (await WellService.getWells()).map(w =>{return {key:w.id, ...w}})
|
2021-04-16 15:49:35 +05:00
|
|
|
let wellsTree = groupBy(newWells, 'deposit', 'cluster', 'caption')
|
2021-05-19 16:05:01 +05:00
|
|
|
console.log(Wells.wellsTree)
|
2021-04-09 17:59:35 +05:00
|
|
|
setWells( newWells )
|
2021-04-16 15:49:35 +05:00
|
|
|
setWellsTree(wellsTree)
|
2021-04-02 17:22:34 +05:00
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
console.error(`${e.message}`);
|
|
|
|
}
|
|
|
|
setLoader(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(()=>{updateWellsList()}, [])
|
|
|
|
|
|
|
|
return(<>
|
2021-05-12 17:53:35 +05:00
|
|
|
<h2>Скважины</h2>
|
2021-04-02 17:22:34 +05:00
|
|
|
<Table
|
|
|
|
dataSource={wells}
|
|
|
|
columns={columns}
|
2021-04-16 15:49:35 +05:00
|
|
|
onRow={(record) => {
|
2021-04-02 17:22:34 +05:00
|
|
|
return {
|
2021-04-08 12:29:19 +05:00
|
|
|
onClick: event => {history.push(`well/${record.id}`)},
|
2021-04-02 17:22:34 +05:00
|
|
|
};
|
|
|
|
}}/>
|
|
|
|
{loader&&<Loader/>}
|
|
|
|
</>)
|
|
|
|
}
|