From 34fbdf2224748575c845e74433d36ca7eaf08e05 Mon Sep 17 00:00:00 2001
From: mayonnaise <90057279+Mayanzev@users.noreply.github.com>
Date: Sun, 24 Mar 2024 23:36:48 +0300
Subject: [PATCH] module5-task1
---
src/components/map/map.tsx | 41 +++++++++++++++++++++++++++
src/hooks/use-map.tsx | 40 ++++++++++++++++++++++++++
src/mocks/cities.ts | 7 +++++
src/mocks/points.ts | 20 +++++++++++++
src/pages/main-screen/main-screen.tsx | 7 ++++-
src/types/city.ts | 5 ++++
src/types/point.ts | 4 +++
7 files changed, 123 insertions(+), 1 deletion(-)
create mode 100644 src/components/map/map.tsx
create mode 100644 src/hooks/use-map.tsx
create mode 100644 src/mocks/cities.ts
create mode 100644 src/mocks/points.ts
create mode 100644 src/types/city.ts
create mode 100644 src/types/point.ts
diff --git a/src/components/map/map.tsx b/src/components/map/map.tsx
new file mode 100644
index 0000000..3d92139
--- /dev/null
+++ b/src/components/map/map.tsx
@@ -0,0 +1,41 @@
+import React, { useEffect } from 'react';
+import { Marker, layerGroup } from 'leaflet';
+import useMap from '../../hooks/use-map';
+import { City } from '../../types/city';
+import { Point } from '../../types/point';
+import 'leaflet/dist/leaflet.css';
+
+type MapProps = {
+ city: City;
+ points: Point[];
+ selectedPoint: Point | undefined;
+}
+
+function Map(props: MapProps): JSX.Element {
+ const {city, points, selectedPoint} = props;
+
+ const mapRef = React.useRef(null);
+ const map = useMap(mapRef, city);
+
+ useEffect(() => {
+ if(map) {
+ const markerLayer = layerGroup().addTo(map);
+ points.forEach((point) => {
+ const marker = new Marker({
+ lat: point.lat,
+ lng: point.lng
+ });
+ marker
+ .addTo(markerLayer);
+ });
+
+ return () => {
+ map.removeLayer(markerLayer);
+ };
+ }
+ }, [map, points, selectedPoint]);
+
+ return
;
+}
+
+export default Map;
diff --git a/src/hooks/use-map.tsx b/src/hooks/use-map.tsx
new file mode 100644
index 0000000..55d6bea
--- /dev/null
+++ b/src/hooks/use-map.tsx
@@ -0,0 +1,40 @@
+import { useEffect, useState, MutableRefObject, useRef } from 'react';
+import { Map, TileLayer } from 'leaflet';
+import { City } from '../types/city';
+
+function useMap(
+ mapRef: MutableRefObject,
+ city: City
+): Map | null {
+ const [map, setMap] = useState