import { useState, useEffect } from "react"; import { MapContainer, TileLayer, Marker, useMapEvents } from "react-leaflet"; import LocationRepository from "../../../repository/LocationRepository"; import { useFilter } from "../../providers/FilterProvider"; const NZ_BOUNDS = [ [-47.204642, 165.344238], [-34.307144, 179.824219], ]; function LocationMarker({setNextDisabled}) { const [position, setPosition] = useState(null); const { filters, updateFilters } = useFilter(); const map = useMapEvents({ click(e) { const newPosition = e.latlng; setPosition(newPosition); updateFilters({ coordinates: newPosition }); setNextDisabled(false); }, }); map.whenReady(() => { const savedCoordinates = filters.coordinates; if (!position && savedCoordinates) { setPosition(savedCoordinates); setNextDisabled(false); map.flyTo(savedCoordinates, 9); } }); return position === null ? null : ; } const fitNZBounds = (map) => { map.fitBounds(NZ_BOUNDS); }; function LocationDetailsDisplay(props) { const [locationDetails, setLocationDetails] = useState({}); const { filters } = useFilter(); useEffect(() => { if (filters.coordinates) { LocationRepository.getLocationData(filters).then((result) => { setLocationDetails(result); }); } }, [filters]); const savedCoordinates = filters.coordinates; if (savedCoordinates) { const soilString = `${locationDetails.soil_name} (${locationDetails.soil_code})`; return (
Latitude: {savedCoordinates.lat}
Longitude: {savedCoordinates.lng}
Address: {locationDetails.full_address}
Ecological Region: {locationDetails.ecological_region}{" "}
Ecological District:{" "} {locationDetails.ecological_district}
Soil Order:{" "} {locationDetails.soil_name ? soilString : ""}
); } return null; } export default function Map(props) { return (
); }