Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: editor - compute track section length on creation #5732

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ export const FormLineStringLength: React.FC<WidgetProps> = (props) => {
*/
useEffect(() => {
const distance = getLineStringDistance(formContext.geometry);
if (formContext.isCreation) {
setTimeout(() => onChange(distance), 0);
} else {
setMin(Math.round(distance - distance * DISTANCE_ERROR_RANGE));
setMax(Math.round(distance + distance * DISTANCE_ERROR_RANGE));
setGeoLength(distance);
}
}, [formContext.geometry, formContext.isCreation, onChange]);
setMin(Math.round(distance - distance * DISTANCE_ERROR_RANGE));
setMax(Math.round(distance + distance * DISTANCE_ERROR_RANGE));
setGeoLength(distance);
}, [formContext.geometry, formContext.isCreation]);

return (
<div>
Expand All @@ -49,7 +45,7 @@ export const FormLineStringLength: React.FC<WidgetProps> = (props) => {
}}
/>
)}
{geoLength && (length < min || length > max) && (
{geoLength > 0 && (length < min || length > max) && (
<p className="text-warning">
{t('Editor.errors.length-out-of-sync-with-geometry', { min, max })}.{' '}
<button
Expand Down
8 changes: 6 additions & 2 deletions front/src/applications/editor/tools/trackEdition/tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { featureCollection } from '@turf/helpers';
import nearestPointOnLine, { NearestPointOnLine } from '@turf/nearest-point-on-line';

import { save } from 'reducers/editor';
import { entityDoUpdate } from 'common/IntervalsDataViz/data';
import { entityDoUpdate, getLineStringDistance } from 'common/IntervalsDataViz/data';
import { ConfirmModal } from 'common/BootstrapSNCF/ModalSNCF';
import { getMapMouseEventNearestFeature } from 'utils/mapHelper';
import { NEW_ENTITY_ID } from 'applications/editor/data/utils';
Expand Down Expand Up @@ -195,7 +195,6 @@ const TrackEditionTool: Tool<TrackEditionState> = {
// Interactions:
onClickMap(e, { setState, state }) {
const { editionState, anchorLinePoints, track, nearestPoint } = state;

if (editionState.type === 'addPoint') {
// Adding a point on an existing section:
if (
Expand Down Expand Up @@ -230,6 +229,11 @@ const TrackEditionTool: Tool<TrackEditionState> = {
}

newState.nearestPoint = null;

// compute new length if it's newly tracksection
if (newState.track.properties.id === NEW_ENTITY_ID) {
newState.track.properties.length = getLineStringDistance(newState.track.geometry);
}
setState(newState);
} else {
setState({
Expand Down