-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* убирает лишнюю обертку * исправляет моки для Амстердама * добавляет карту
- Loading branch information
Showing
5 changed files
with
167 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import {useEffect, useRef} from 'react'; | ||
import {Icon, layerGroup, Marker} from 'leaflet'; | ||
|
||
import {CITIES, URL_MARKER_CURRENT, URL_MARKER_DEFAULT} from '../../const'; | ||
import 'leaflet/dist/leaflet.css'; | ||
import useMap from '../../hooks/use-map.tsx'; | ||
import {OfferShortInfo} from '../../types/offer.ts'; | ||
import {Nullable} from 'vitest'; | ||
|
||
type MapProps = { | ||
city: typeof CITIES[number]; | ||
offers: OfferShortInfo[]; | ||
activeOffer: Nullable<OfferShortInfo>; | ||
}; | ||
|
||
const defaultCustomIcon = new Icon({ | ||
iconUrl: URL_MARKER_DEFAULT, | ||
iconSize: [40, 40], | ||
iconAnchor: [20, 40] | ||
}); | ||
|
||
const currentCustomIcon = new Icon({ | ||
iconUrl: URL_MARKER_CURRENT, | ||
iconSize: [40, 40], | ||
iconAnchor: [20, 40] | ||
}); | ||
|
||
function Map({city, offers, activeOffer}: MapProps) { | ||
|
||
const mapRef = useRef(null); | ||
const map = useMap(mapRef, city); | ||
|
||
useEffect(() => { | ||
if (map) { | ||
const markerLayer = layerGroup().addTo(map); | ||
offers.forEach((offer) => { | ||
const marker = new Marker({ | ||
lat: offer.location.latitude, | ||
lng: offer.location.longitude | ||
}); | ||
|
||
marker | ||
.setIcon( | ||
activeOffer !== undefined && activeOffer !== null && offer.id === activeOffer.id | ||
? currentCustomIcon | ||
: defaultCustomIcon | ||
) | ||
.addTo(markerLayer); | ||
}); | ||
|
||
map.flyTo([city.location.latitude, city.location.longitude], 12); | ||
|
||
return () => { | ||
map.removeLayer(markerLayer); | ||
}; | ||
} | ||
}, [city, map, offers, activeOffer]); | ||
|
||
return <section ref={mapRef} className="cities__map map" />; | ||
} | ||
|
||
export default Map; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {MutableRefObject, useEffect, useRef, useState} from 'react'; | ||
import {Map, TileLayer} from 'leaflet'; | ||
import {CITIES} from '../const.ts'; | ||
|
||
|
||
function useMap( | ||
mapRef: MutableRefObject<HTMLElement | null>, | ||
city: typeof CITIES[number] | ||
): Map | null { | ||
const [map, setMap] = useState<Map | null>(null); | ||
const isRenderedRef = useRef<boolean>(false); | ||
|
||
useEffect(() => { | ||
if (mapRef.current !== null && !isRenderedRef.current) { | ||
const instance = new Map(mapRef.current, { | ||
center: { | ||
lat: city.location.latitude, | ||
lng: city.location.longitude | ||
}, | ||
zoom: 12 | ||
}); | ||
|
||
const layer = new TileLayer( | ||
'https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png', | ||
{ | ||
attribution: | ||
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>' | ||
} | ||
); | ||
|
||
instance.addLayer(layer); | ||
|
||
setMap(instance); | ||
isRenderedRef.current = true; | ||
} | ||
}, [mapRef, city]); | ||
|
||
return map; | ||
} | ||
|
||
export default useMap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters