Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Main page: Select a road
Browse files Browse the repository at this point in the history
  • Loading branch information
Seb-sti1 committed Nov 8, 2023
1 parent 5c78ef6 commit 59c4233
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 197 deletions.
178 changes: 0 additions & 178 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions frontend/src/Components/Map/DetectMapClick.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useMapEvents } from 'react-leaflet';
import { LatLng } from 'leaflet';
import { FC } from 'react';

interface Props {
/** The callback function */
onClick: (latLng: LatLng) => void;
}

/**
* A component that detects a click on the map and calls the callback function
*/
const DetectMapClick: FC<Props> = ({ onClick }) => {
useMapEvents({
click: (e) => {
onClick(e.latlng);
},
});
return null;
};

export default DetectMapClick;
1 change: 1 addition & 0 deletions frontend/src/Components/Map/ForceMapUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const ForceMapUpdate: React.FC<Props> = ({ triggerUpdate, position }) => {
map.invalidateSize();
}, [triggerUpdate]);

// TODO: not working when moving too little
useEffect(() => {
if (position) {
map.flyTo(position);
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/Components/Map/Road.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IRoad } from '../../models/path';
import { LatLng, LatLngLiteral } from 'leaflet';
import { Polyline } from 'react-leaflet';
import React, { ReactElement, useEffect } from 'react';
import L from 'leaflet';

interface Props {
/** The roads */
Expand Down Expand Up @@ -47,11 +48,12 @@ const Road: React.FC<Props> = ({ road, onClick }) => {
pathOptions={{ weight: 5, opacity: opacity }}
positions={data}
eventHandlers={{
click: ({ latlng }) => {
if (onClick) onClick(latlng);
click: (e) => {
L.DomEvent.stopPropagation(e); // used to prevent the map to detect the click
if (onClick) onClick(e.latlng);
},
mouseover: () => {
setOpacity(0.8);
setOpacity(0.9);
},
mouseout: () => {
setOpacity(0.2);
Expand Down
35 changes: 21 additions & 14 deletions frontend/src/Components/Map/Roads.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,45 @@ import React from 'react';
import { IRoad } from '../../models/path';
import Road from './Road';
import { LatLng } from '../../models/models';
import { useNavigate } from 'react-router-dom';

interface Props {
/** The roads */
roads?: IRoad[];
/** The selected road index */
selectedRoadIdx?: number;
/** The event handlers, when a road is selected */
onSelectedRoad?: (road: IRoad, position: LatLng) => void;
onSelectedRoad?: (index: number, road: IRoad, position: LatLng) => void;
}

/**
* A components that renders the roads on the map
*/
const Roads: React.FC<Props> = ({ roads, onSelectedRoad }) => {
const navigate = useNavigate();

const Roads: React.FC<Props> = ({ roads, selectedRoadIdx, onSelectedRoad }) => {
return (
<>
{roads?.map((road) => (
{selectedRoadIdx === undefined || selectedRoadIdx === -1 || !roads ? (
roads?.map((road, index) => (
<Road
key={road.way_name}
road={road}
onClick={(position) => {
if (onSelectedRoad) {
onSelectedRoad(index, road, position);
}
}}
/>
))
) : (
<Road
key={road.way_name}
road={road}
key={roads[selectedRoadIdx].way_name}
road={roads[selectedRoadIdx]}
onClick={(position) => {
console.log(road, position);
// TODO: remove when possible to select a road
navigate('/road-details');

if (onSelectedRoad) {
onSelectedRoad(road, position);
onSelectedRoad(selectedRoadIdx, roads[selectedRoadIdx], position);
}
}}
/>
))}
)}
</>
);
};
Expand Down
Loading

0 comments on commit 59c4233

Please sign in to comment.