Skip to content

Commit d59d183

Browse files
committed
Improve map state after creating a new route
Now the newly created route will be added to the state correctly. Previously there were desync in state which resulted in several bugs: - Refreshing the view would cause the route to get lost - Clicking the map would cause state to lose the route id, resulting in user not being able to do changes to the route
1 parent 478e1e7 commit d59d183

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

ui/src/components/map/Map.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
MutableRefObject,
55
RefObject,
66
forwardRef,
7+
useEffect,
78
useImperativeHandle,
89
useRef,
910
useState,
@@ -40,6 +41,7 @@ import { RouteStopsOverlay } from './routes/RouteStopsOverlay';
4041
import { FunctionalAreaVisualization, StopAreas } from './stop-areas';
4142
import { MemberStopLines, Stops } from './stops';
4243
import { Terminals } from './terminals';
44+
import { useMapUrlStateContext } from './utils/mapUrlState';
4345

4446
type MapViewState = {
4547
readonly mapStopViewState: MapEntityEditorViewState;
@@ -179,10 +181,22 @@ export const MapComponent: ForwardRefRenderFunction<
179181
MapProps
180182
> = ({ className, width = '100vw', height = '100vh' }, externalRef) => {
181183
const [showRoute, setShowRoute] = useState(true);
184+
const {
185+
state: {
186+
displayedRoute: { routeId: urlRouteId },
187+
},
188+
} = useMapUrlStateContext();
189+
const dispatch = useAppDispatch();
182190

183191
const editorRefs = useEditorRefs();
184192
const mapViewState = useMapViewState();
185193

194+
useEffect(() => {
195+
if (urlRouteId) {
196+
dispatch(setSelectedRouteIdAction(urlRouteId));
197+
}
198+
}, [dispatch, urlRouteId]);
199+
186200
useRouteEditorImperativeHandle(externalRef, editorRefs.routeEditorRef);
187201
const onClick = useOnClickMap(editorRefs, mapViewState);
188202

ui/src/components/map/routes/RouteEditor.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
} from '../../../redux';
3333
import { isDateInRange } from '../../../time';
3434
import { RequiredKeys } from '../../../types';
35+
import { Priority } from '../../../types/enums';
3536
import { ConfirmationDialog } from '../../../uiComponents';
3637
import {
3738
showSuccessToast,
@@ -83,6 +84,7 @@ const RouteEditorComponent: ForwardRefRenderFunction<
8384
state: {
8485
filters: { observationDate },
8586
},
87+
setDisplayedRoute,
8688
setFlatUrlState,
8789
resetUrlState,
8890
} = useMapUrlStateContext();
@@ -156,11 +158,17 @@ const RouteEditorComponent: ForwardRefRenderFunction<
156158
// Select created route
157159
dispatch(setSelectedRouteIdAction(newRoute.route_id));
158160

159-
setFlatUrlState((p) => ({ ...p, routeId: newRoute.route_id }));
160-
// If created route is not valid at the selected observation date,
161-
// change observation date to created route's validity start date
162-
// so the user can see the freshly created route
163-
161+
setDisplayedRoute(() => ({
162+
routeId: newRoute.route_id,
163+
lineLabel: null,
164+
routeLabels: [],
165+
routePriorities: [Priority.Standard, Priority.Temporary, Priority.Draft],
166+
showSelectedDaySituation: false,
167+
}));
168+
169+
// // If created route is not valid at the selected observation date,
170+
// // change observation date to created route's validity start date
171+
// // so the user can see the freshly created route
164172
if (
165173
!isDateInRange(
166174
observationDate,
@@ -176,9 +184,13 @@ const RouteEditorComponent: ForwardRefRenderFunction<
176184
showWarningToast(t(($) => $.filters.observationDateAdjusted));
177185
}
178186

179-
// Reset map editor drap mode and remove draft route
180-
// as it is now saved
181-
187+
/*
188+
- Reset map editor drap mode and remove draft route as it is now saved
189+
- Parent has no access to ref here so use removeRoute directly
190+
here instead of onDeleteDrawnRoute which relies on the ref
191+
*/
192+
removeRoute(map?.getMap(), SNAPPING_LINE_LAYER_ID);
193+
dispatch(resetDraftRouteGeometryAction());
182194
dispatch(resetRouteCreatingAction());
183195
};
184196

0 commit comments

Comments
 (0)