diff --git a/src/App.tsx b/src/App.tsx index a21e1a8..87c602a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -11,8 +11,22 @@ type Offer = { rating: number; type: string; isPremium: boolean; - previewImage: string; + isFavorite: boolean; NumberOfPlaces: number; + previewImage: string; + city: { + name: string; + location: { + latitude: number; + longitude: number; + zoom: number; + }; + }; + location: { + latitude: number; + longitude: number; + zoom: number; + }; }; type AppProps = { diff --git a/src/components/Favorites/Favorite.tsx b/src/components/Favorites/Favorite.tsx index b7525f0..c8d4033 100644 --- a/src/components/Favorites/Favorite.tsx +++ b/src/components/Favorites/Favorite.tsx @@ -6,8 +6,22 @@ type Offer = { rating: number; type: string; isPremium: boolean; - previewImage: string; + isFavorite: boolean; NumberOfPlaces: number; + previewImage: string; + city: { + name: string; + location: { + latitude: number; + longitude: number; + zoom: number; + }; + }; + location: { + latitude: number; + longitude: number; + zoom: number; + }; }; type FavoriteProps = { offers: Offer[]; diff --git a/src/components/MainPage/MainPage.tsx b/src/components/MainPage/MainPage.tsx index ee9a53a..90845e8 100644 --- a/src/components/MainPage/MainPage.tsx +++ b/src/components/MainPage/MainPage.tsx @@ -1,6 +1,6 @@ import {FC} from 'react'; import OfferList from '../Offer/OfferList'; - +import Map from '../Map/Map'; type Offer = { id: number; title: string; @@ -8,13 +8,26 @@ type Offer = { rating: number; type: string; isPremium: boolean; - previewImage: string; + isFavorite: boolean; NumberOfPlaces: number; + previewImage: string; + city: { + name: string; + location: { + latitude: number; + longitude: number; + zoom: number; + }; + }; + location: { + latitude: number; + longitude: number; + zoom: number; + }; }; type MainPageProps = { offers: Offer[]; }; - export const MainPage : FC = ({ offers }) => (
@@ -110,7 +123,9 @@ export const MainPage : FC = ({ offers }) =>
-
+
+ +
diff --git a/src/components/Map/Map.tsx b/src/components/Map/Map.tsx new file mode 100644 index 0000000..631251f --- /dev/null +++ b/src/components/Map/Map.tsx @@ -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
; +} + +export default Map; diff --git a/src/components/Offer/Offer.tsx b/src/components/Offer/Offer.tsx index 6c9511b..9d5bd83 100644 --- a/src/components/Offer/Offer.tsx +++ b/src/components/Offer/Offer.tsx @@ -207,116 +207,7 @@ export default function Offer () {
- -
- - - - - - - - - - - - - - -
-