Skip to content

Commit 4ba06b3

Browse files
committed
Move copy stop feature under EditStopLayer
1 parent 645a638 commit 4ba06b3

File tree

4 files changed

+74
-92
lines changed

4 files changed

+74
-92
lines changed

ui/src/components/map/stops/CopyStopLayer.tsx

Lines changed: 0 additions & 51 deletions
This file was deleted.

ui/src/components/map/stops/EditStopLayer.tsx

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { forwardRef, useImperativeHandle, useMemo } from 'react';
1+
import { DateTime } from 'luxon';
2+
import { forwardRef, useImperativeHandle, useMemo, useState } from 'react';
23
import { MapLayerMouseEvent } from 'react-map-gl/maplibre';
34
import { useDispatch } from 'react-redux';
45
import { ReusableComponentsVehicleModeEnum } from '../../../generated/graphql';
@@ -7,7 +8,9 @@ import {
78
MapEntityEditorViewState,
89
Operation,
910
closeTimingPlaceModalAction,
11+
isCopyMode,
1012
isModalOpen,
13+
selectCopyStopId,
1114
selectEditedStopAreaData,
1215
selectMapStopViewState,
1316
setCopyStopIdAction,
@@ -29,6 +32,8 @@ import {
2932
mapStopToCommonConflictItem,
3033
} from '../../routes-and-lines/common/ConflictResolverModal';
3134
import { EditStoplayerRef } from '../refTypes';
35+
import { CopyStopConfirmationDialog } from './CopyStopConfirmationDialog';
36+
import { CopyStopModal } from './CopyStopModal';
3237
import { DeleteStopConfirmationDialog } from './DeleteStopConfirmationDialog';
3338
import { EditStopConfirmationDialog } from './EditStopConfirmationDialog';
3439
import { EditStopModal } from './EditStopModal';
@@ -40,6 +45,7 @@ import {
4045
useDeleteStopUtils,
4146
useEditStopUtils,
4247
} from './hooks';
48+
import { useMapCopyStopUtils } from './hooks/useMapCopyStopUtils';
4349
import { LineToActiveStopArea } from './LineToActiveStopArea';
4450
import { LineToClosestInfraLink } from './LineToClosestInfraLink';
4551
import { Stop } from './Stop';
@@ -107,27 +113,46 @@ function useDefaultValues(
107113
type EditStopLayerProps = {
108114
readonly draftLocation: Point | null;
109115
readonly onEditingFinished: (netexId: string | null) => void;
116+
readonly onCopyFinished: (
117+
netexId: string,
118+
validityStart: DateTime,
119+
validityEnd?: DateTime,
120+
) => void;
110121
readonly onPopupClose: () => void;
111122
readonly selectedStopId: string | null;
112123
};
113124

114125
export const EditStopLayer = forwardRef<EditStoplayerRef, EditStopLayerProps>(
115-
({ draftLocation, onEditingFinished, onPopupClose, selectedStopId }, ref) => {
116-
const { stopInfo, loading } = useGetStopInfoForEditingOnMap(selectedStopId);
117-
useMapDataLayerLoader(
118-
Operation.FetchStopInfo,
119-
!!stopInfo || !selectedStopId,
120-
loading,
121-
);
122-
126+
(
127+
{
128+
draftLocation,
129+
onEditingFinished,
130+
onCopyFinished,
131+
onPopupClose,
132+
selectedStopId,
133+
},
134+
ref,
135+
) => {
123136
const dispatch = useDispatch();
124137

138+
const copyStopId = useAppSelector(selectCopyStopId);
125139
const mapStopViewState = useAppSelector(selectMapStopViewState);
126140
const setMapStopViewState = useAppAction(setMapStopViewStateAction);
127141
const setCopyStopId = useAppAction(setCopyStopIdAction);
128142

143+
const { stopInfo, loading } = useGetStopInfoForEditingOnMap(
144+
selectedStopId ?? copyStopId,
145+
);
146+
useMapDataLayerLoader(
147+
Operation.FetchStopInfo,
148+
!!stopInfo || (!selectedStopId && !copyStopId),
149+
loading,
150+
);
151+
129152
const defaultValues = useDefaultValues(draftLocation, stopInfo);
130153

154+
const [copyDialogOpen, setCopyDialogOpen] = useState(false);
155+
131156
const onCloseEditors = () => {
132157
setMapStopViewState(MapEntityEditorViewState.NONE);
133158
dispatch(closeTimingPlaceModalAction());
@@ -154,6 +179,19 @@ export const EditStopLayer = forwardRef<EditStoplayerRef, EditStopLayerProps>(
154179
const { deleteChanges, onDeleteStop, onConfirmDelete, onCancelDelete } =
155180
useDeleteStopUtils(stopInfo, onFinishEditing);
156181

182+
const {
183+
defaultStopFormValues,
184+
onStartCopyStop,
185+
onCancelCopyStop,
186+
onCloseCopyModal,
187+
onCopyStopFormSubmit,
188+
} = useMapCopyStopUtils(
189+
stopInfo,
190+
onCopyFinished,
191+
onPopupClose,
192+
setCopyDialogOpen,
193+
);
194+
157195
if (createChanges && editChanges) {
158196
throw new Error('Undefined state');
159197
}
@@ -184,9 +222,10 @@ export const EditStopLayer = forwardRef<EditStoplayerRef, EditStopLayerProps>(
184222
dispatch(setSelectedRouteIdAction(undefined));
185223
};
186224

187-
const onStartCopyStop = () => {
225+
const onInitCopyStop = () => {
188226
if (selectedStopId) {
189227
setCopyStopId(selectedStopId);
228+
setCopyDialogOpen(true);
190229
}
191230
};
192231

@@ -215,7 +254,7 @@ export const EditStopLayer = forwardRef<EditStoplayerRef, EditStopLayerProps>(
215254
onMove={onStartMoveStop}
216255
onDelete={onDeleteStop}
217256
onClose={onCloseEditors}
218-
onCopy={onStartCopyStop}
257+
onCopy={onInitCopyStop}
219258
/>
220259
)}
221260

@@ -256,6 +295,21 @@ export const EditStopLayer = forwardRef<EditStoplayerRef, EditStopLayerProps>(
256295
deleteChanges={deleteChanges}
257296
/>
258297
)}
298+
299+
<CopyStopConfirmationDialog
300+
isOpen={copyDialogOpen}
301+
onConfirm={onStartCopyStop}
302+
onCancel={onCancelCopyStop}
303+
/>
304+
305+
{isCopyMode(mapStopViewState) && !!defaultStopFormValues && (
306+
<CopyStopModal
307+
defaultValues={defaultStopFormValues}
308+
onCancel={onCloseCopyModal}
309+
onClose={onCloseCopyModal}
310+
onSubmit={onCopyStopFormSubmit}
311+
/>
312+
)}
259313
</>
260314
);
261315
},

ui/src/components/map/stops/Stops.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
Operation,
1616
isEditorOpen,
1717
isPlacingOrMoving,
18-
selectCopyStopId,
1918
selectDraftLocation,
2019
selectSelectedStopAreaId,
2120
selectSelectedStopId,
@@ -39,7 +38,6 @@ import { EditStoplayerRef, StopsRef } from '../refTypes';
3938
import { MapStop, MapStopArea, MapTerminal } from '../types';
4039
import { useMapUrlStateContext } from '../utils/mapUrlState';
4140
import { useMapViewState } from '../utils/useMapViewState';
42-
import { CopyStopLayer } from './CopyStopLayer';
4341
import { CreateStopMarker } from './CreateStopMarker';
4442
import { EditStopLayer } from './EditStopLayer';
4543
import {
@@ -107,7 +105,6 @@ export const StopsImpl: ForwardRefRenderFunction<StopsRef, StopsProps> = (
107105
setFlatUrlState,
108106
} = useMapUrlStateContext();
109107

110-
const copyStopId = useAppSelector(selectCopyStopId);
111108
const selectedStopId = useAppSelector(selectSelectedStopId);
112109
const selectedStopAreaId = useAppSelector(selectSelectedStopAreaId);
113110
const selectedTerminalId = useAppSelector(selectSelectedTerminalId);
@@ -277,15 +274,8 @@ export const StopsImpl: ForwardRefRenderFunction<StopsRef, StopsProps> = (
277274
selectedStopId={selectedStopId ?? null}
278275
draftLocation={draftLocation ?? null}
279276
onEditingFinished={onEditingFinished}
280-
onPopupClose={onPopupClose}
281-
/>
282-
)}
283-
284-
{/* Copy a stop on the map */}
285-
{copyStopId && (
286-
<CopyStopLayer
287-
onPopupClose={onPopupClose}
288277
onCopyFinished={onCopyFinished}
278+
onPopupClose={onPopupClose}
289279
/>
290280
)}
291281

ui/src/components/map/stops/hooks/useMapCopyStopUtils.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { useDispatch } from 'react-redux';
55
import { useAppAction, useAppSelector } from '../../../../hooks';
66
import {
77
MapEntityEditorViewState,
8-
Operation,
98
closeTimingPlaceModalAction,
109
selectCopyStopId,
1110
selectDraftLocation,
@@ -17,12 +16,8 @@ import {
1716
import { parseDate } from '../../../../time';
1817
import { Point } from '../../../../types';
1918
import { showDangerToast } from '../../../../utils';
20-
import { useMapDataLayerLoader } from '../../../common/hooks';
2119
import { StopFormState } from '../../../forms/stop';
22-
import {
23-
StopInfoForEditingOnMap,
24-
useGetStopInfoForEditingOnMap,
25-
} from '../../../forms/stop/utils/useGetStopInfoForEditingOnMap';
20+
import { StopInfoForEditingOnMap } from '../../../forms/stop/utils/useGetStopInfoForEditingOnMap';
2621
import {
2722
mapStopFormStateToInputs,
2823
useCopyStop,
@@ -52,6 +47,7 @@ function useDefaultValues(
5247
}
5348

5449
export function useMapCopyStopUtils(
50+
stopInfo: StopInfoForEditingOnMap | null,
5551
onCopyFinished: (
5652
netexId: string,
5753
validityStart: DateTime,
@@ -77,13 +73,6 @@ export function useMapCopyStopUtils(
7773
const setMapStopViewState = useAppAction(setMapStopViewStateAction);
7874
const setDraftStopLocation = useAppAction(setDraftLocationAction);
7975

80-
const { stopInfo, loading } = useGetStopInfoForEditingOnMap(copyStopId);
81-
useMapDataLayerLoader(
82-
Operation.FetchStopInfo,
83-
!!stopInfo || !copyStopId,
84-
loading,
85-
);
86-
8776
const getStopDetails = useGetStopDetailsLazy();
8877
const copyStop = useCopyStop();
8978

@@ -101,13 +90,13 @@ export function useMapCopyStopUtils(
10190
};
10291

10392
const onCancelCopyStop = () => {
93+
setDialogOpen(false);
10494
setSelectedStopId(copyStopId);
10595
setCopyStopId(undefined);
10696
setDraftStopLocation(undefined);
107-
setDialogOpen(true);
10897
};
10998

110-
const onCloseModal = () => {
99+
const onCloseCopyModal = () => {
111100
onCancelCopyStop();
112101

113102
if (stopInfo) {
@@ -119,7 +108,7 @@ export function useMapCopyStopUtils(
119108
}
120109
};
121110

122-
const onStopFormSubmit = async (
111+
const onCopyStopFormSubmit = async (
123112
changes: CreateChanges,
124113
state: StopFormState,
125114
) => {
@@ -156,7 +145,7 @@ export function useMapCopyStopUtils(
156145
});
157146

158147
setCopyStopId(undefined);
159-
setDialogOpen(true);
148+
setDialogOpen(false);
160149
onCopyFinished(
161150
quayId,
162151
parseDate(state.validityStart),
@@ -168,7 +157,7 @@ export function useMapCopyStopUtils(
168157
defaultStopFormValues: defaultValues,
169158
onStartCopyStop,
170159
onCancelCopyStop,
171-
onCloseModal,
172-
onStopFormSubmit,
160+
onCloseCopyModal,
161+
onCopyStopFormSubmit,
173162
};
174163
}

0 commit comments

Comments
 (0)