From b863c8fc2688777d47ae9e2eb30a6b21b5ae114c Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 11 Feb 2025 18:20:45 +0100 Subject: [PATCH 1/2] front: use early return in extractMarkerInformation() loop Reduces indentation, evacuates the edge case up-front. Signed-off-by: Simon Ser --- .../ItineraryMarkers.tsx | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx b/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx index 4f14b38c1bd..a0c0e7e15f5 100644 --- a/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx +++ b/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx @@ -72,41 +72,40 @@ const extractMarkerInformation = ( ): MarkerProperties[] => pathSteps .map((pathStep, index): MarkerProperties | null => { + if (!pathStep.coordinates) return null; + const matchingOp = suggestedOP ? suggestedOP.find((op) => matchPathStepAndOp(pathStep, op)) : undefined; - if (pathStep.coordinates) { - if (pathStep.pointType === MARKER_TYPE.ORIGIN) { - return { - coordinates: pathStep.coordinates, - type: MARKER_TYPE.ORIGIN, - imageSource: showStdcmAssets ? stdcmOrigin : originSVG, - op: matchingOp, - pathStep, - }; - } - - if (pathStep.pointType === MARKER_TYPE.DESTINATION) { - return { - coordinates: pathStep.coordinates, - type: MARKER_TYPE.DESTINATION, - imageSource: showStdcmAssets ? stdcmDestination : destinationSVG, - op: matchingOp, - pathStep, - }; - } + if (pathStep.pointType === MARKER_TYPE.ORIGIN) { + return { + coordinates: pathStep.coordinates, + type: MARKER_TYPE.ORIGIN, + imageSource: showStdcmAssets ? stdcmOrigin : originSVG, + op: matchingOp, + pathStep, + }; + } + if (pathStep.pointType === MARKER_TYPE.DESTINATION) { return { coordinates: pathStep.coordinates, - type: MARKER_TYPE.VIA, - imageSource: showStdcmAssets ? stdcmVia : viaSVG, - index, + type: MARKER_TYPE.DESTINATION, + imageSource: showStdcmAssets ? stdcmDestination : destinationSVG, op: matchingOp, pathStep, }; } - return null; + + return { + coordinates: pathStep.coordinates, + type: MARKER_TYPE.VIA, + imageSource: showStdcmAssets ? stdcmVia : viaSVG, + index, + op: matchingOp, + pathStep, + }; }) .filter((marker): marker is MarkerProperties => marker !== null); From b5f550600b7ffe65538d2e8a4a46b626155c5e63 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 11 Feb 2025 18:29:22 +0100 Subject: [PATCH 2/2] front: de-duplicate extractMarkerInformation() All branches of the if are very similar here. Unfortunately because of the MarkerProperties type definition we need this ternary and spread operator. Signed-off-by: Simon Ser --- .../ItineraryMarkers.tsx | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx b/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx index a0c0e7e15f5..5226992a068 100644 --- a/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx +++ b/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx @@ -65,6 +65,18 @@ const formatPointWithNoName = ( ); +const MARKER_IMAGES = { + [MARKER_TYPE.ORIGIN]: originSVG, + [MARKER_TYPE.DESTINATION]: destinationSVG, + [MARKER_TYPE.VIA]: viaSVG, +}; + +const STDCM_MARKER_IMAGES = { + [MARKER_TYPE.ORIGIN]: stdcmOrigin, + [MARKER_TYPE.DESTINATION]: stdcmDestination, + [MARKER_TYPE.VIA]: stdcmVia, +}; + const extractMarkerInformation = ( pathSteps: MarkerInformation[], showStdcmAssets: boolean, @@ -78,33 +90,18 @@ const extractMarkerInformation = ( ? suggestedOP.find((op) => matchPathStepAndOp(pathStep, op)) : undefined; - if (pathStep.pointType === MARKER_TYPE.ORIGIN) { - return { - coordinates: pathStep.coordinates, - type: MARKER_TYPE.ORIGIN, - imageSource: showStdcmAssets ? stdcmOrigin : originSVG, - op: matchingOp, - pathStep, - }; - } - - if (pathStep.pointType === MARKER_TYPE.DESTINATION) { - return { - coordinates: pathStep.coordinates, - type: MARKER_TYPE.DESTINATION, - imageSource: showStdcmAssets ? stdcmDestination : destinationSVG, - op: matchingOp, - pathStep, - }; - } - + const images = showStdcmAssets ? STDCM_MARKER_IMAGES : MARKER_IMAGES; return { coordinates: pathStep.coordinates, - type: MARKER_TYPE.VIA, - imageSource: showStdcmAssets ? stdcmVia : viaSVG, - index, + imageSource: images[pathStep.pointType], op: matchingOp, pathStep, + ...(pathStep.pointType === MARKER_TYPE.VIA + ? { + type: MARKER_TYPE.VIA, + index, + } + : { type: pathStep.pointType }), }; }) .filter((marker): marker is MarkerProperties => marker !== null);