1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-26 23:14:19 +00:00
pds-2021-g2-nest/nest_frontend/components/interactive/BoxConditionMap.js

117 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-05-18 00:04:06 +00:00
import React, { useCallback, useContext, useEffect, useState } from "react"
2021-05-11 14:37:15 +00:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
2021-05-18 00:48:34 +00:00
import { faMapPin, faPlus } from "@fortawesome/free-solid-svg-icons"
2021-04-30 20:10:00 +00:00
import ButtonIconOnly from "../base/ButtonIconOnly"
import useRepositoryEditor from "../../hooks/useRepositoryEditor"
2021-05-07 03:02:20 +00:00
import Condition from "../../utils/Condition"
2021-05-18 00:04:06 +00:00
import ContextLanguage from "../../contexts/ContextLanguage"
import BoxMap from "../base/BoxMap"
2021-04-30 20:10:00 +00:00
2021-05-07 14:30:12 +00:00
/**
* https://wiki.openstreetmap.org/wiki/Zoom_levels
2021-05-11 14:37:15 +00:00
*/
2021-05-07 14:30:12 +00:00
const MPIXEL = [
156412,
78206,
39103,
19551,
9776,
4888,
2444,
1222,
610.984,
305.492,
152.746,
76.373,
38.187,
19.093,
9.547,
4.773,
2.387,
1.193,
0.596,
0.298,
0.149,
]
2021-05-01 00:33:46 +00:00
2021-04-30 20:10:00 +00:00
/**
* A {@link BoxFull} that allows the user to select a geographical location to use to filter tweets.
*
* @param props - Additional props to pass to the box.
* @returns {JSX.Element}
* @constructor
*/
export default function BoxConditionMap({ ...props }) {
2021-05-20 09:39:40 +00:00
const [position, setPosition] = useState()
const [zoom, setZoom] = useState()
2021-05-07 14:30:12 +00:00
const [map, setMap] = useState(null)
2021-05-11 14:37:15 +00:00
const { addCondition } = useRepositoryEditor()
2021-05-18 00:48:34 +00:00
const { strings } = useContext(ContextLanguage)
2021-04-30 20:10:00 +00:00
2021-05-07 14:30:12 +00:00
const onMove = useCallback(
() => {
setPosition(map.getCenter())
},
2021-05-11 14:37:15 +00:00
[map],
2021-05-07 14:30:12 +00:00
)
const onZoom = useCallback(
() => {
setZoom(map.getZoom())
},
2021-05-11 14:37:15 +00:00
[map],
2021-05-07 14:30:12 +00:00
)
useEffect(
() => {
2021-05-11 14:37:15 +00:00
if(map === null) {
return
}
2021-05-07 14:30:12 +00:00
map.on("move", onMove)
map.on("zoom", onZoom)
return () => {
map.off("move", onMove)
map.off("zoom", onZoom)
}
},
2021-05-11 16:05:01 +00:00
[map, onMove, onZoom],
2021-05-07 03:02:20 +00:00
)
2021-04-30 20:10:00 +00:00
2021-05-07 03:02:20 +00:00
const onButtonClick = () => {
2021-05-07 14:30:12 +00:00
const mapSize = map.getSize()
const minSize = Math.min(mapSize.x, mapSize.y)
const radius = minSize * MPIXEL[zoom]
2021-05-07 03:02:20 +00:00
addCondition(new Condition(
"COORDINATES",
2021-05-11 14:37:15 +00:00
`< ${radius} ${position.lat} ${position.lng}`,
2021-05-07 03:02:20 +00:00
))
2021-04-30 20:10:00 +00:00
}
return (
<BoxMap
2021-05-07 14:30:12 +00:00
header={
2021-05-17 23:33:08 +00:00
<span>
2021-05-18 00:04:06 +00:00
{strings.searchBy}
2021-05-17 23:33:08 +00:00
&nbsp;
<FontAwesomeIcon icon={faMapPin}/>
&nbsp;
2021-05-18 00:04:06 +00:00
{strings.byZone}
2021-05-17 23:33:08 +00:00
</span>
2021-05-07 14:30:12 +00:00
}
setMap={setMap}
button={
<ButtonIconOnly
icon={faPlus}
color={"Green"}
onClick={onButtonClick}
2021-04-30 20:10:00 +00:00
/>
}
{...props}
/>
2021-04-30 20:10:00 +00:00
)
}