2021-07-20 14:10:01 +05:00
|
|
|
|
import { Map, Overlay } from "pigeon-maps"
|
|
|
|
|
import pointer from '../images/pointer.svg'
|
|
|
|
|
import {Link} from "react-router-dom";
|
|
|
|
|
import LoaderPortal from '../components/LoaderPortal'
|
|
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
|
import {ClusterService} from '../services/api'
|
2021-07-22 10:17:39 +05:00
|
|
|
|
import notify from '../components/notify'
|
2021-07-20 14:10:01 +05:00
|
|
|
|
|
|
|
|
|
const calcViewParams = (clusters) => {
|
2021-07-27 10:20:53 +05:00
|
|
|
|
if ((!clusters) || clusters.length === 0)
|
2021-07-20 14:10:01 +05:00
|
|
|
|
return {center:[60.81226, 70.0562], zoom: 5}
|
|
|
|
|
|
|
|
|
|
const center = clusters.reduce((sum, cluster) => {
|
|
|
|
|
sum[0] += (cluster.latitude / clusters.length)
|
|
|
|
|
sum[1] += (cluster.longitude / clusters.length)
|
|
|
|
|
return sum
|
|
|
|
|
}, [0, 0])
|
|
|
|
|
|
|
|
|
|
const maxDeg = clusters.reduce((max, cluster) => {
|
|
|
|
|
const dLatitude = Math.abs(center[0] - cluster.latitude)
|
|
|
|
|
const dLongitude = Math.abs(center[1] - cluster.longitude)
|
|
|
|
|
const d = dLatitude > dLongitude ? dLatitude : dLongitude
|
|
|
|
|
return d > max ? d : max
|
|
|
|
|
}, 0)
|
|
|
|
|
|
|
|
|
|
return {center, zoom: maxDeg*25}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function Deposit() {
|
|
|
|
|
const [clustersData, setClustersData] = useState([])
|
|
|
|
|
const [showLoader, setShowLoader] = useState(false)
|
|
|
|
|
|
2021-07-26 14:44:00 +05:00
|
|
|
|
useEffect(()=>{
|
|
|
|
|
const updateClusters = async()=>{
|
|
|
|
|
setShowLoader(true)
|
|
|
|
|
try{
|
|
|
|
|
const data = await ClusterService.getClusters()
|
|
|
|
|
setClustersData(data)
|
|
|
|
|
}
|
|
|
|
|
catch(ex) {
|
|
|
|
|
notify(`Не удалось загрузить список кустов`, 'error')
|
|
|
|
|
console.log(ex)
|
|
|
|
|
}
|
|
|
|
|
setShowLoader(false)
|
2021-07-22 10:17:39 +05:00
|
|
|
|
}
|
2021-07-26 14:44:00 +05:00
|
|
|
|
updateClusters()
|
|
|
|
|
}, [])
|
2021-07-20 14:10:01 +05:00
|
|
|
|
|
|
|
|
|
const viewParams = calcViewParams(clustersData)
|
|
|
|
|
|
2021-07-26 14:44:00 +05:00
|
|
|
|
const markers = clustersData.map(cluster =>
|
|
|
|
|
<Overlay
|
|
|
|
|
width={32}
|
|
|
|
|
anchor={[cluster.latitude, cluster.longitude]}
|
|
|
|
|
key={`${cluster.latitude} ${cluster.longitude}`}
|
|
|
|
|
>
|
|
|
|
|
<Link to={`/cluster/${cluster.id}`}>
|
|
|
|
|
<img width={40} src={pointer} alt="+"/>
|
|
|
|
|
<span>{cluster.caption}</span>
|
|
|
|
|
</Link>
|
|
|
|
|
|
|
|
|
|
</Overlay >)
|
|
|
|
|
|
2021-07-20 14:10:01 +05:00
|
|
|
|
return (
|
|
|
|
|
<LoaderPortal show={showLoader}>
|
|
|
|
|
<Map
|
|
|
|
|
height='100vh'
|
|
|
|
|
center={viewParams.center}
|
2021-07-26 14:44:00 +05:00
|
|
|
|
zoom={viewParams.zoom}>
|
|
|
|
|
{markers}
|
2021-07-20 14:10:01 +05:00
|
|
|
|
</Map>
|
|
|
|
|
</LoaderPortal>
|
|
|
|
|
);
|
|
|
|
|
}
|