From c5e80073a23693feeaff89837a96dc7a2c9c273f Mon Sep 17 00:00:00 2001 From: romainvalls Date: Thu, 27 Jun 2024 09:10:53 +0200 Subject: [PATCH] front: fix begin input --- .../IntervalsEditor/IntervalsEditor.tsx | 7 +- .../IntervalsEditorCommonForm.tsx | 15 ++- .../PowerRestrictionsSelectorV2.tsx | 96 ++++++++++----- .../reducers/osrdconf/osrdConfCommon/index.ts | 114 +++++++++++++++++- 4 files changed, 198 insertions(+), 34 deletions(-) diff --git a/front/src/common/IntervalsEditor/IntervalsEditor.tsx b/front/src/common/IntervalsEditor/IntervalsEditor.tsx index a73e66de136..253453b337b 100644 --- a/front/src/common/IntervalsEditor/IntervalsEditor.tsx +++ b/front/src/common/IntervalsEditor/IntervalsEditor.tsx @@ -52,7 +52,12 @@ type IntervalsEditorProps = { /** Total length of the path */ totalLength: number; disableDrag?: boolean; - onResizeFromInput?: (intervalIndex: number, newEnd: number, context: 'begin' | 'end') => void; + onResizeFromInput?: ( + intervalIndex: number, + newEnd: number, + context: 'begin' | 'end', + newBegin?: number + ) => void; } & ( | { intervalType: INTERVAL_TYPES.NUMBER; diff --git a/front/src/common/IntervalsEditor/IntervalsEditorCommonForm.tsx b/front/src/common/IntervalsEditor/IntervalsEditorCommonForm.tsx index 0c374b4bc60..dd01a8f6951 100644 --- a/front/src/common/IntervalsEditor/IntervalsEditorCommonForm.tsx +++ b/front/src/common/IntervalsEditor/IntervalsEditorCommonForm.tsx @@ -13,7 +13,12 @@ type IntervalsEditorFormProps = { interval: IntervalItem; selectedIntervalIndex: number; setData: (newData: IntervalItem[], selectedIntervalIndex?: number) => void; - onInputChange?: (intervalIndex: number, newBegin: number, context: 'begin' | 'end') => void; + onInputChange?: ( + intervalIndex: number, + newEnd: number, + context: 'begin' | 'end', + newBegin: number + ) => void; setSelectedIntervalIndex: (selectedIntervalIndex: number) => void; totalLength: number; defaultValue: string | number; @@ -41,6 +46,7 @@ const IntervalsEditorCommonForm = ({ const resizeSegmentByInput = (newPosition: number, context: 'begin' | 'end') => { const gap = newPosition - interval[context]; + // use absolute value to manage cases where the position is not rounded // ex: begin = 1200.68 and newPosition = 1200 (because input is rounded) if (Math.abs(gap) > 1) { @@ -56,7 +62,12 @@ const IntervalsEditorCommonForm = ({ defaultValue, }); if (onInputChange) { - onInputChange(selectedIntervalIndex, newPosition, context); + onInputChange( + selectedIntervalIndex, + context === 'end' ? newPosition : interval.end, + context, + context === 'begin' ? newPosition : interval.begin + ); // Pass newBegin when context is 'begin' } else { setData(fixedResults, selectedIntervalIndex); } diff --git a/front/src/modules/powerRestriction/components/PowerRestrictionsSelectorV2.tsx b/front/src/modules/powerRestriction/components/PowerRestrictionsSelectorV2.tsx index 4886d6f9c95..15c6e9a9d3c 100644 --- a/front/src/modules/powerRestriction/components/PowerRestrictionsSelectorV2.tsx +++ b/front/src/modules/powerRestriction/components/PowerRestrictionsSelectorV2.tsx @@ -46,7 +46,8 @@ const PowerRestrictionsSelectorV2 = ({ resetPowerRestrictionRangesV2, cutPowerRestrictionRangesV2, deletePowerRestrictionRangesV2, - resizePowerRestrictionRangeV2, + resizeSegmentEndInput, + resizeSegmentBeginInput, } = useOsrdConfActions(); const powerRestrictionRanges = useSelector(getPowerRestrictionV2); const pathSteps = compact(useSelector(getPathSteps)); @@ -199,49 +200,86 @@ const PowerRestrictionsSelectorV2 = ({ } }; - const resizeSegmentByInput = ( + const resizeSegments = ( selectedSegmentIndex: number, newEnd: number, - context: 'begin' | 'end' + context: 'begin' | 'end', + newBegin?: number ) => { - const firstIndex = context === 'end' ? selectedSegmentIndex : selectedSegmentIndex - 1; + const firstIndex = context === 'end' ? selectedSegmentIndex : selectedSegmentIndex; + + const getPathStep = (position: number) => + pathSteps.find((step) => step.positionOnPath === position); + + const getPowerRestriction = (begin: number, end: number): PowerRestrictionV2 | undefined => { + const fromPathStep = getPathStep(begin); + const toPathStep = getPathStep(end); + + if (fromPathStep && toPathStep) { + return powerRestrictionRanges.find( + (restriction) => restriction.from === fromPathStep.id && restriction.to === toPathStep.id + ); + } + return undefined; // Ensure a value is always returned + }; let firstRestriction: PowerRestrictionV2 | undefined; let secondRestriction: PowerRestrictionV2 | undefined; + let previousRestriction: PowerRestrictionV2 | undefined; + if (firstIndex >= 0) { const firstRangeData = intervalsEditorData[firstIndex]; if (firstRangeData.value !== NO_POWER_RESTRICTION) { - const fromPathStep = pathSteps.find((step) => step.positionOnPath === firstRangeData.begin); - const toPathStep = pathSteps.find((step) => step.positionOnPath === firstRangeData.end); - if (fromPathStep && toPathStep) { - firstRestriction = powerRestrictionRanges.find( - (restriction) => - restriction.from === fromPathStep.id && restriction.to === toPathStep.id - ); - } + firstRestriction = getPowerRestriction(firstRangeData.begin, firstRangeData.end); } } - const secondRangeData = intervalsEditorData[firstIndex + 1]; - if (secondRangeData.value !== NO_POWER_RESTRICTION) { - const fromPathStep = pathSteps.find((step) => step.positionOnPath === secondRangeData.begin); - const toPathStep = pathSteps.find((step) => step.positionOnPath === secondRangeData.end); - if (fromPathStep && toPathStep) { - secondRestriction = powerRestrictionRanges.find( - (restriction) => restriction.from === fromPathStep.id && restriction.to === toPathStep.id - ); + if (context === 'end') { + const secondRangeData = intervalsEditorData[firstIndex + 1]; + if (secondRangeData.value !== NO_POWER_RESTRICTION) { + secondRestriction = getPowerRestriction(secondRangeData.begin, secondRangeData.end); + } + + let newEndPathStep = getPathStep(newEnd); + if (!newEndPathStep) { + newEndPathStep = createPathStep(newEnd, cumulativeSums, pathProperties, pathSteps); } - } - let newEndPathStep = pathSteps.find((pathStep) => pathStep.positionOnPath === newEnd); - if (!newEndPathStep) { - newEndPathStep = createPathStep(newEnd, cumulativeSums, pathProperties, pathSteps); + if (firstRestriction && newEndPathStep) { + dispatch( + resizeSegmentEndInput({ + firstRestriction, + secondRestriction, + newEndPathStep, + }) + ); + } } + if (context === 'begin') { + if (firstIndex > 0) { + const previousRangeData = intervalsEditorData[firstIndex - 1]; + if (previousRangeData.value) { + previousRestriction = getPowerRestriction(previousRangeData.begin, previousRangeData.end); + } else { + return; + } + } - if (firstRestriction && newEndPathStep) { - dispatch( - resizePowerRestrictionRangeV2({ firstRestriction, secondRestriction, newEndPathStep }) - ); + if (newBegin) { + let newFromPathStep = getPathStep(newBegin); + if (!newFromPathStep) { + newFromPathStep = createPathStep(newBegin, cumulativeSums, pathProperties, pathSteps); + if (firstRestriction && newFromPathStep) { + dispatch( + resizeSegmentBeginInput({ + firstRestriction, + previousRestriction, + newFromPathStep, + }) + ); + } + } + } } }; @@ -371,7 +409,7 @@ const PowerRestrictionsSelectorV2 = ({ deleteTool: true, }} disableDrag - onResizeFromInput={resizeSegmentByInput} + onResizeFromInput={resizeSegments} />