Skip to content

Commit

Permalink
Merge pull request #6 from Nawwar14/module5-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Nov 16, 2024
2 parents b6f2b60 + f0060ab commit cceb366
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 118 deletions.
16 changes: 15 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
16 changes: 15 additions & 1 deletion src/components/Favorites/Favorite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
23 changes: 19 additions & 4 deletions src/components/MainPage/MainPage.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import {FC} from 'react';
import OfferList from '../Offer/OfferList';

import Map from '../Map/Map';
type Offer = {
id: number;
title: string;
price: number;
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<MainPageProps> = ({ offers }) =>
(
<div className="page page--gray page--main">
Expand Down Expand Up @@ -110,7 +123,9 @@ export const MainPage : FC<MainPageProps> = ({ offers }) =>
</div>
</section>
<div className="cities__right-section">
<section className="cities__map map"></section>
<section className="cities__map map">
<Map offers={offers} selectedPoint={offers[3]} />
</section>
</div>
</div>
</div>
Expand Down
77 changes: 77 additions & 0 deletions src/components/Map/Map.tsx
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;
111 changes: 1 addition & 110 deletions src/components/Offer/Offer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,116 +207,7 @@ export default function Offer () {
</li>
</ul>
<form className="reviews__form form" action="#" method="post">
<label className="reviews__label form__label" htmlFor="review">
Your review
</label>
<div className="reviews__rating-form form__rating">
<input
className="form__rating-input visually-hidden"
name="rating"
value="5"
id="5-stars"
type="radio"
/>
<label
htmlFor="5-stars"
className="reviews__rating-label form__rating-label"
title="perfect"
>
<svg className="form__star-image" width="37" height="33">
<use xlinkHref="#icon-star"></use>
</svg>
</label>

<input
className="form__rating-input visually-hidden"
name="rating"
value="4"
id="4-stars"
type="radio"
/>
<label
htmlFor="4-stars"
className="reviews__rating-label form__rating-label"
title="good"
>
<svg className="form__star-image" width="37" height="33">
<use xlinkHref="#icon-star"></use>
</svg>
</label>

<input
className="form__rating-input visually-hidden"
name="rating"
value="3"
id="3-stars"
type="radio"
/>
<label
htmlFor="3-stars"
className="reviews__rating-label form__rating-label"
title="not bad"
>
<svg className="form__star-image" width="37" height="33">
<use xlinkHref="#icon-star"></use>
</svg>
</label>

<input
className="form__rating-input visually-hidden"
name="rating"
value="2"
id="2-stars"
type="radio"
/>
<label
htmlFor="2-stars"
className="reviews__rating-label form__rating-label"
title="badly"
>
<svg className="form__star-image" width="37" height="33">
<use xlinkHref="#icon-star"></use>
</svg>
</label>

<input
className="form__rating-input visually-hidden"
name="rating"
value="1"
id="1-star"
type="radio"
/>
<label
htmlFor="1-star"
className="reviews__rating-label form__rating-label"
title="terribly"
>
<svg className="form__star-image" width="37" height="33">
<use xlinkHref="#icon-star"></use>
</svg>
</label>
</div>
<textarea
className="reviews__textarea form__textarea"
id="review"
name="review"
placeholder="Tell how was your stay, what you like and what can be improved"
/>
<div className="reviews__button-wrapper">
<p className="reviews__help">
To submit review please make sure to set{' '}
<span className="reviews__star">rating</span> and describe
your stay with at least{' '}
<b className="reviews__text-amount">50 characters</b>.
</p>
<button
className="reviews__submit form__submit button"
type="submit"
disabled
>
Submit
</button>
</div>
<SendCommentForm /> {}
</form>
</section>
</div>
Expand Down
16 changes: 15 additions & 1 deletion src/components/Offer/OfferCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,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 OfferCardProps = {
Expand Down
16 changes: 15 additions & 1 deletion src/components/Offer/OfferList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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 OfferListProps = {
Expand Down
5 changes: 5 additions & 0 deletions src/const.ts
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';
39 changes: 39 additions & 0 deletions src/hooks/use-map.tsx
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:
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors &copy; <a href="https://carto.com/attributions">CARTO</a>'
}
);

instance.addLayer(layer);

setMap(instance);
isRenderedRef.current = true;
}
}, [mapRef, city]);

return map;
}

export default useMap;
Loading

0 comments on commit cceb366

Please sign in to comment.