-
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 #6 from Nawwar14/module5-task1
- Loading branch information
Showing
11 changed files
with
271 additions
and
118 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
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,77 @@ | ||
import {useRef, useEffect} from 'react'; | ||
import {Icon, Marker, layerGroup} from 'leaflet'; | ||
import useMap from '../../hooks/use-map'; | ||
import 'leaflet/dist/leaflet.css'; | ||
type Offer = { | ||
id: number; | ||
title: string; | ||
price: number; | ||
rating: number; | ||
type: string; | ||
isPremium: boolean; | ||
isFavorite: boolean; | ||
NumberOfPlaces: number; | ||
previewImage: string; | ||
city: { | ||
name: string; | ||
location: { | ||
latitude: number; | ||
longitude: number; | ||
zoom: number; | ||
}; | ||
}; | ||
location: { | ||
latitude: number; | ||
longitude: number; | ||
zoom: number; | ||
}; | ||
}; | ||
const defaultCustomIcon = new Icon({ | ||
iconUrl: 'https://assets.htmlacademy.ru/content/intensive/javascript-1/demo/interactive-map/pin.svg', | ||
iconSize: [40, 40], | ||
iconAnchor: [20, 40] | ||
}); | ||
|
||
const currentCustomIcon = new Icon({ | ||
iconUrl: 'https://assets.htmlacademy.ru/content/intensive/javascript-1/demo/interactive-map/main-pin.svg', | ||
iconSize: [40, 40], | ||
iconAnchor: [20, 40] | ||
}); | ||
type MainPageProps = { | ||
offers: Offer[]; | ||
selectedPoint :Offer; | ||
}; | ||
|
||
function Map(props: MainPageProps): JSX.Element { | ||
const {offers, selectedPoint} = props; | ||
|
||
const mapRef = useRef(null); | ||
const map = useMap(mapRef, 'Амстердам'); | ||
|
||
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( | ||
selectedPoint !== undefined && offer.title === selectedPoint.title | ||
? currentCustomIcon | ||
: defaultCustomIcon, | ||
) | ||
.addTo(markerLayer); | ||
}); | ||
|
||
return () => { | ||
map.removeLayer(markerLayer); | ||
}; | ||
} | ||
}, [map, offers,selectedPoint]); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const URL_MARKER_DEFAULT = | ||
'https://assets.htmlacademy.ru/content/intensive/javascript-1/demo/interactive-map/pin.svg'; | ||
|
||
export const URL_MARKER_CURRENT = | ||
'https://assets.htmlacademy.ru/content/intensive/javascript-1/demo/interactive-map/main-pin.svg'; |
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'; | ||
|
||
function useMap( | ||
mapRef: MutableRefObject<HTMLElement | null>, | ||
city: string | ||
): 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: 52.3, | ||
lng: 4.99 | ||
}, | ||
zoom: 10 | ||
}); | ||
|
||
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; |
Oops, something went wrong.