asb_cloud_front/src/pages/Archive.jsx

146 lines
4.0 KiB
React
Raw Normal View History

2021-05-28 12:04:50 +05:00
import { useRef, useLayoutEffect, useState, useEffect } from 'react'
import {
Button,
DatePicker,
Row,
Col,
2021-05-28 12:04:50 +05:00
Tooltip} from 'antd'
import { useParams } from 'react-router-dom'
import { DataService } from '../services/api'
2021-05-28 12:04:50 +05:00
import {generateUUID} from '../services/UidGenerator'
import { ArchiveColumn } from '../components/ArchiveColumn'
import moment from 'moment'
2021-05-31 15:21:37 +05:00
import notify from '../components/notify'
import LoaderPortal from '../components/LoaderPortal'
const { RangePicker } = DatePicker
2021-05-28 12:04:50 +05:00
const SaveObject = (key, obj) => {
let json = JSON.stringify(obj)
localStorage.setItem(key, json)
}
2021-05-28 12:04:50 +05:00
const LoadObject = (key) => {
let json = localStorage.getItem(key)
return json ? JSON.parse(json) : null
}
2021-05-28 12:04:50 +05:00
export default function Archive() {
let { id } = useParams();
const [saubData, setSaubData] = useState([])
2021-05-28 12:04:50 +05:00
const [chartsCfgs, setChartsCfgs] = useState([])
const [rangeDate, setRangeDate] = useState([moment().subtract(3,'hours'), moment()])
const [geometry, setGeometry] = useState({ratioRest:1, ratio1st:1, wRest:.5, w1st:.5})
const [loader, setLoader] = useState(false)
2021-05-28 12:04:50 +05:00
const chartsCfgsKey = 'chartsCfgs'
const chartsContainerRef = useRef();
const handleReceiveDataSaub = (data) => {
2021-05-28 12:04:50 +05:00
if (data)
setSaubData(data)
2021-05-28 12:04:50 +05:00
}
const onAddChart = () => {
let newChartCfgs = [...chartsCfgs, {id: generateUUID(), yDisplay: false, aspectRatio:1}]
setChartsCfgs(newChartCfgs)
}
2021-05-28 12:04:50 +05:00
const onRemoveChart = (id) => {
let newChartCfgs = chartsCfgs.filter(cfg => cfg.id !== id )
setChartsCfgs(newChartCfgs)
}
const onSaveConfig = ()=>{
SaveObject(chartsCfgsKey, chartsCfgs)
}
2021-05-28 12:04:50 +05:00
const onChangeRange = (range) => {
setRangeDate(range)
}
useLayoutEffect(()=>{
if(chartsContainerRef.current && chartsCfgs?.length){
let w = chartsContainerRef.current.offsetWidth //1792
w = w > 0 ? w : 1792
let ot = chartsContainerRef.current.offsetTop
let ph = chartsContainerRef.current.offsetParent.offsetHeight
let h = ph - ot - 32 //761
h = h > 0 ? h : 761
let chartsCount = chartsCfgs.length
let labelLenght = 8
let borderWidth = 8
let wRest = Math.floor((w - labelLenght)/chartsCount) - borderWidth
let w1st = wRest + labelLenght
let ratio1st = w1st/h
let ratioRest = wRest/h
setGeometry({ratio1st, ratioRest, w1st, wRest})
}
2021-05-28 12:04:50 +05:00
},[chartsContainerRef, chartsCfgs])
useEffect(() => {
let cfgs = LoadObject(chartsCfgsKey)
if(cfgs)
setChartsCfgs(cfgs)
},[])
2021-05-28 12:04:50 +05:00
useEffect(()=>{
SaveObject(chartsCfgsKey, chartsCfgs)
},[chartsCfgs])
useEffect(() => {
let interval = (rangeDate[1] - rangeDate[0]) / 1000
let startDate = rangeDate[0].toISOString()
setLoader(true)
2021-05-28 12:04:50 +05:00
DataService.getData(id, startDate, interval, 2048)
.then(handleReceiveDataSaub)
2021-05-31 15:21:37 +05:00
.catch(error => {
notify(`Не удалось загрузить данные по скважине (${id}) c ${rangeDate[0]} по ${rangeDate[1]}`, 'error')
console.error(error)
})
.finally(()=>setLoader(false))
2021-05-28 12:04:50 +05:00
}, [id, rangeDate]);
let charts = null
if(chartsCfgs.length > 0){
chartsCfgs[0].yDisplay = true
charts = chartsCfgs.map((cfg, i) =>
<Col flex={`${i===0 ? geometry.w1st : geometry.wRest}px`}
key={cfg.id}>
<ArchiveColumn
data={saubData}
rangeDate={rangeDate}
chartRatio={i===0 ? geometry.ratio1st : geometry.ratioRest}
onRemoveChart={onRemoveChart}
onSaveConfig={onSaveConfig}
config={cfg}/>
</Col>)
}
return (<>
2021-05-28 12:04:50 +05:00
<Tooltip title="Добавить график">
<Button
type="primary"
onClick={onAddChart}
disabled={chartsCfgs.length >= 6}>
+
</Button>
</Tooltip>
<RangePicker
showTime
allowClear={false}
onChange = {onChangeRange}
value = {rangeDate}
/>
<LoaderPortal show={loader}>
<Row ref={chartsContainerRef}>
{charts}
</Row>
</LoaderPortal>
</>)
}