|
| 1 | +import { useState, useEffect } from 'react' |
| 2 | +import IndexCard from '../../IndexCard' |
| 3 | +import { |
| 4 | + MapContainer, |
| 5 | + TileLayer, |
| 6 | + Marker, |
| 7 | + Popup, |
| 8 | + ZoomControl, |
| 9 | + Tooltip, |
| 10 | + useMap |
| 11 | +} from 'react-leaflet' |
| 12 | +import 'leaflet/dist/leaflet.css' |
| 13 | +import L from 'leaflet' |
| 14 | + |
| 15 | +// Custom icons |
| 16 | +const starIcon = new L.Icon({ |
| 17 | + iconUrl: `/elements/star-sticker.svg`, |
| 18 | + iconSize: new L.Point(60, 60), |
| 19 | + popupAnchor: [0, 0] |
| 20 | +}) |
| 21 | + |
| 22 | +const pinIcon = new L.Icon({ |
| 23 | + iconUrl: `/elements/thumbtack.png`, |
| 24 | + iconSize: new L.Point(30, 30), |
| 25 | + popupAnchor: [0, 0] |
| 26 | +}) |
| 27 | + |
| 28 | +const StyledMapContainer = MapContainer |
| 29 | + |
| 30 | +// Component to update map center dynamically |
| 31 | +function UpdateMapCenter({ latitude, longitude }) { |
| 32 | + const map = useMap() |
| 33 | + useEffect(() => { |
| 34 | + if (latitude && longitude) { |
| 35 | + map.setView([latitude, longitude], 10) // Center map to new location |
| 36 | + } |
| 37 | + }, [latitude, longitude, map]) |
| 38 | + return null |
| 39 | +} |
| 40 | + |
| 41 | +export default function Map({ full, latitude, longitude }) { |
| 42 | + // Default center (only used if no lat/lng is given) |
| 43 | + const defaultCenter = [35.683, -25.099] |
| 44 | + |
| 45 | + const [events, setEvents] = useState([]) |
| 46 | + const [mapCenter, setMapCenter] = useState(defaultCenter) |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + if (latitude && longitude) { |
| 50 | + setMapCenter([latitude, longitude]) // Update map center dynamically |
| 51 | + } |
| 52 | + }, [latitude, longitude]) |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + async function fetchEvents() { |
| 56 | + try { |
| 57 | + const response = await fetch('/api/locations') |
| 58 | + const data = await response.json() |
| 59 | + setEvents(data) |
| 60 | + } catch (error) { |
| 61 | + console.error('Error fetching events:', error) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + fetchEvents() |
| 66 | + }, []) |
| 67 | + |
| 68 | + return ( |
| 69 | + <StyledMapContainer |
| 70 | + center={mapCenter} |
| 71 | + zoom={latitude && longitude ? 10 : 2.5} // Zoom in if location is given |
| 72 | + style={{ |
| 73 | + width: full ? '80vw' : '80%', |
| 74 | + height: '100vh', |
| 75 | + zIndex: 0 |
| 76 | + }} |
| 77 | + zoomControl={!full} |
| 78 | + > |
| 79 | + {/* Dynamically update center */} |
| 80 | + <UpdateMapCenter latitude={latitude} longitude={longitude} /> |
| 81 | + |
| 82 | + <TileLayer |
| 83 | + attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' |
| 84 | + url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" |
| 85 | + /> |
| 86 | + |
| 87 | + {/* Show marker at user-provided location */} |
| 88 | + {latitude && longitude && ( |
| 89 | + <Marker position={[latitude, longitude]} icon={starIcon}> |
| 90 | + <Popup>You are here!</Popup> |
| 91 | + </Marker> |
| 92 | + )} |
| 93 | + |
| 94 | + {/* Show event markers */} |
| 95 | + {events.map((event, idx) => ( |
| 96 | + <Marker |
| 97 | + position={[event.lat, event.lng]} |
| 98 | + key={idx} |
| 99 | + icon={event.flagship ? starIcon : pinIcon} |
| 100 | + > |
| 101 | + <Popup> |
| 102 | + <IndexCard |
| 103 | + id={event.id} |
| 104 | + title={event.name} |
| 105 | + slug={event.slug} |
| 106 | + format={event.format} |
| 107 | + location={event.location} |
| 108 | + /> |
| 109 | + </Popup> |
| 110 | + <Tooltip>{event.name}</Tooltip> |
| 111 | + </Marker> |
| 112 | + ))} |
| 113 | + |
| 114 | + {full && <ZoomControl position="topright" />} |
| 115 | + </StyledMapContainer> |
| 116 | + ) |
| 117 | +} |
0 commit comments