parent
40fc0d2781
commit
1db7a4aede
@ -1,9 +1,30 @@ |
||||
import { useMemo, useState } from 'preact/hooks'; |
||||
import { MapComponent } from './map'; |
||||
import { Coordinates, Marker } from './types'; |
||||
|
||||
import './style.css'; |
||||
import { MapComponent } from "./map" |
||||
import { MarkersComponent } from './markers'; |
||||
import { nanoid } from 'nanoid'; |
||||
|
||||
export const RoutePlanner = () => { |
||||
return <section class="route-planner"> |
||||
<section class="markers">Markers</section> |
||||
<section class="map"><MapComponent /></section> |
||||
</section> |
||||
} |
||||
const [markers, setMarkers] = useState<Marker[]>([]); |
||||
const onMapClick = useMemo( |
||||
() => (coordinates: Coordinates) => |
||||
setMarkers((markers) => [ |
||||
...markers, |
||||
{ coordinates, key: nanoid(10) }, |
||||
]), |
||||
[], |
||||
); |
||||
|
||||
return ( |
||||
<section class="route-planner"> |
||||
<section class="markers"> |
||||
<MarkersComponent markers={markers} /> |
||||
</section> |
||||
<section class="map"> |
||||
<MapComponent onMapClick={onMapClick} markers={markers} /> |
||||
</section> |
||||
</section> |
||||
); |
||||
}; |
||||
|
@ -0,0 +1,18 @@ |
||||
import { InternalProps } from './types'; |
||||
|
||||
import 'leaflet/dist/leaflet.css'; |
||||
|
||||
export const MarkersComponent = ({ markers }: InternalProps) => { |
||||
return ( |
||||
<> |
||||
<h3>Markers</h3> |
||||
<ol> |
||||
{markers.map(({ key, coordinates }) => ( |
||||
<li key={key}> |
||||
{coordinates.lat}, {coordinates.lng} |
||||
</li> |
||||
))} |
||||
</ol> |
||||
</> |
||||
); |
||||
}; |
@ -0,0 +1,16 @@ |
||||
import type { LatLng } from 'leaflet'; |
||||
|
||||
export type Coordinates = LatLng; |
||||
|
||||
export type Marker = { |
||||
key: string; |
||||
coordinates: Coordinates; |
||||
}; |
||||
|
||||
export type InternalProps = { |
||||
markers: Marker[]; |
||||
}; |
||||
|
||||
export type InternalMapProps = InternalProps & { |
||||
onMapClick: (coordinates: Coordinates) => void; |
||||
}; |
Loading…
Reference in new issue