-
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.
Merge pull request #5 from uniksssss/module5-task1
- Loading branch information
Showing
11 changed files
with
149 additions
and
27 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
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,44 @@ | ||
import { useRef, useEffect } from 'react'; | ||
import { Icon, Marker, layerGroup } from 'leaflet'; | ||
import useMap from '../hooks/use-Map'; | ||
import { URL_MARKER_DEFAULT } from '../const'; | ||
import 'leaflet/dist/leaflet.css'; | ||
import { OffersType } from '../types/types'; | ||
|
||
type MapProps = { | ||
offers: OffersType[]; | ||
selectedOffer: OffersType; | ||
}; | ||
|
||
const defaultCustomIcon = new Icon({ | ||
iconUrl: URL_MARKER_DEFAULT, | ||
iconSize: [40, 40], | ||
iconAnchor: [20, 40], | ||
}); | ||
|
||
function Map({offers, selectedOffer}: MapProps){ | ||
const mapRef = useRef(null); | ||
const map = useMap(mapRef, selectedOffer); | ||
|
||
useEffect(() => { | ||
if (map) { | ||
const markerLayer = layerGroup().addTo(map); | ||
offers.forEach((offer) => { | ||
const marker = new Marker({ | ||
lat: offer.city.location.latitude, | ||
lng: offer.city.location.longitude | ||
}); | ||
|
||
marker.setIcon(defaultCustomIcon).addTo(markerLayer); | ||
}); | ||
|
||
return () => { | ||
map.removeLayer(markerLayer); | ||
}; | ||
} | ||
}, [map, offers, selectedOffer]); | ||
|
||
return <div style={{ height: '500px' }} ref={mapRef}></div>; | ||
} | ||
|
||
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
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
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 |
---|---|---|
@@ -1,14 +1,17 @@ | ||
enum AppRoute{ | ||
Root = '/', | ||
Login = '/login', | ||
Favorites = '/favorites', | ||
Offer = '/offer/:id' | ||
enum AppRoute { | ||
Root = '/', | ||
Login = '/login', | ||
Favorites = '/favorites', | ||
Offer = '/offer/:id', | ||
} | ||
|
||
export enum AuthorizationStatus { | ||
Auth = 'AUTH', | ||
NoAuth = 'NO_AUTH', | ||
Unknown = 'UNKNOWN', | ||
Auth = 'AUTH', | ||
NoAuth = 'NO_AUTH', | ||
Unknown = 'UNKNOWN', | ||
} | ||
|
||
export const URL_MARKER_DEFAULT = | ||
'https://assets.htmlacademy.ru/content/intensive/javascript-1/demo/interactive-map/pin.svg'; | ||
|
||
export default AppRoute; |
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,39 @@ | ||
import { useEffect, useState, MutableRefObject, useRef } from 'react'; | ||
import { Map, TileLayer } from 'leaflet'; | ||
import { OffersType } from '../types/types'; | ||
|
||
function useMap( | ||
mapRef: MutableRefObject<HTMLElement | null>, | ||
{city}: OffersType): 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: city.location.zoom | ||
}); | ||
|
||
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.location]); | ||
|
||
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