support for markers removal

main
Inga 🏳‍🌈 1 year ago
parent 34eea5d8f0
commit 26410e5a07
  1. 27
      src/routePlanner/index.tsx
  2. 8
      src/routePlanner/map.tsx
  3. 7
      src/routePlanner/markers.tsx
  4. 6
      src/routePlanner/types.ts

@ -8,13 +8,32 @@ import { nanoid } from 'nanoid';
export const RoutePlanner = () => { export const RoutePlanner = () => {
const [markers, setMarkers] = useState<Marker[]>([]); const [markers, setMarkers] = useState<Marker[]>([]);
const onMarkerRemove = useMemo(
() => (keyToRemove: string) =>
setMarkers((markers) =>
markers
.filter(({ key }) => key !== keyToRemove)
.map((marker, i) => ({ ...marker, index: i + 1 })),
),
[],
);
const onMapClick = useMemo( const onMapClick = useMemo(
() => (coordinates: Coordinates) => () => (coordinates: Coordinates) => {
const key = nanoid(10);
const remove = () => onMarkerRemove(key);
setMarkers((markers) => [ setMarkers((markers) => [
...markers, ...markers,
{ coordinates, key: nanoid(10) }, {
]), key,
[], index: markers.length + 1,
remove,
coordinates,
},
]);
},
[onMarkerRemove],
); );
return ( return (

@ -71,8 +71,9 @@ export const MapComponent = ({ markers, onMapClick }: InternalMapProps) => {
} }
layers.push( layers.push(
...markers.flatMap((marker, i) => { ...markers.flatMap((marker) => {
const circle = leaflet.circleMarker(marker.coordinates, { const circle = leaflet.circleMarker(marker.coordinates, {
bubblingMouseEvents: false,
color: 'black', color: 'black',
fill: true, fill: true,
fillOpacity: 100, fillOpacity: 100,
@ -84,11 +85,13 @@ export const MapComponent = ({ markers, onMapClick }: InternalMapProps) => {
permanent: true, permanent: true,
direction: 'center', direction: 'center',
className: 'text', className: 'text',
content: `${i + 1}`, content: `${marker.index}`,
}, },
circle, circle,
) )
.setLatLng(marker.coordinates); .setLatLng(marker.coordinates);
circle.on('click', marker.remove);
return [circle, tooltip]; return [circle, tooltip];
}), }),
); );
@ -100,6 +103,7 @@ export const MapComponent = ({ markers, onMapClick }: InternalMapProps) => {
return () => { return () => {
for (const layer of layers) { for (const layer of layers) {
layer.removeFrom(map); layer.removeFrom(map);
layer.remove();
} }
}; };
}, [mapRef, markers]); }, [mapRef, markers]);

@ -7,8 +7,11 @@ export const MarkersComponent = ({ markers }: InternalProps) => {
<> <>
<h3>Markers</h3> <h3>Markers</h3>
<ol> <ol>
{markers.map(({ key }, i) => ( {markers.map((marker) => (
<li key={key}>{`Waypoint ${i + 1}`}</li> <li key={marker.key}>
{`Waypoint ${marker.index}`}
<button onClick={marker.remove}>🗑</button>
</li>
))} ))}
</ol> </ol>
</> </>

@ -4,6 +4,8 @@ export type Coordinates = LatLng;
export type Marker = { export type Marker = {
key: string; key: string;
remove: () => void;
index: number;
coordinates: Coordinates; coordinates: Coordinates;
}; };
@ -14,3 +16,7 @@ export type InternalProps = {
export type InternalMapProps = InternalProps & { export type InternalMapProps = InternalProps & {
onMapClick: (coordinates: Coordinates) => void; onMapClick: (coordinates: Coordinates) => void;
}; };
export type MarkerListElementProps = {
marker: Marker;
};

Loading…
Cancel
Save