Skip to content

Commit

Permalink
front: handle duplicate switch nodes errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Wadjetz committed Jan 16, 2024
1 parent b997a5f commit 847a84e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions front/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@
"select-track": "Select a line on the map"
},
"label": "Switch tool",
"duplicate-errors": "The track {{track}} is already used by the port {{port}}",
"no-track-picked-yet": "No line selected yet",
"switch-type": "Switch type",
"untitled-track": "Untitled track"
Expand Down
1 change: 1 addition & 0 deletions front/public/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@
"no-move": "Les aiguillages ne peuvent pas être déplacés, et sont automatiquement placés sur leur premier port.",
"select-track": "Sélectionnez une ligne sur la carte"
},
"duplicate-errors": "La voie {{track}} est déjà utilisé par le port {{port}}",
"label": "Outil \"Aiguillage\"",
"no-track-picked-yet": "Aucune ligne n'a encore été sélectionnée",
"switch-type": "Type d'aiguillage",
Expand Down
11 changes: 10 additions & 1 deletion front/src/applications/editor/tools/switchEdition/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Select from 'react-select';
import { Layer, Popup, Source } from 'react-map-gl/maplibre';
import nearestPoint from '@turf/nearest-point';
import { featureCollection, point } from '@turf/helpers';
import { first, last, debounce } from 'lodash';
import { first, last, debounce, groupBy, filter } from 'lodash';
import type { Position } from 'geojson';

import type { SwitchEntity, TrackSectionEntity } from 'types';
Expand Down Expand Up @@ -94,6 +94,15 @@ export const SwitchEditionLeftPanel = () => {
}}
onSubmit={async (flatSwitch) => {
const entityToSave = flatSwitchToSwitch(switchType, flatSwitch as FlatSwitchEntity);
const detectedDuplicates = filter(
groupBy(entityToSave.properties.ports, 'track'),
(v, _) => v.length > 1
);

if (detectedDuplicates.length > 0) {
throw new Error(t('Editor.infra-errors.error-type.node_endpoints_not_unique.name'));
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const res: any = await dispatch(
save(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useContext, useEffect, useState } from 'react';
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { useDispatch } from 'react-redux';
import Select from 'react-select';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -29,6 +29,18 @@ const TrackSectionEndpointSelector = ({ schema, formData, onChange, name }: Fiel
const { state, setState } = useContext(
EditorContext
) as ExtendedEditorContextType<SwitchEditionState>;

const duplicateWith = useMemo(() => {
const ports = Object.entries(state.entity.properties?.ports ?? {}).map(([k, v]) => ({
...v,
port: k,
name: `port::${k}`,
}));
const currentPort = ports.find((p) => p.name === name);
const othersPorts = ports.filter((p) => p.name !== name);
return othersPorts.filter((p) => p.track === currentPort?.track);
}, [state.entity.properties]);

const { t } = useTranslation();
const infraID = useInfraID();

Expand Down Expand Up @@ -89,6 +101,14 @@ const TrackSectionEndpointSelector = ({ schema, formData, onChange, name }: Fiel
<div className="mb-4">
{schema.title && <h5>{schema.title}</h5>}
{schema.description && <p>{schema.description}</p>}
{duplicateWith.map(({ track, port }) => (
<div className="text-danger small font-weight-bold">
{t('Editor.tools.switch-edition.duplicate-errors', {
track,
port,
})}
</div>
))}
<div className="d-flex flex-row align-items-center mb-2">
<div className="flex-grow-1 flex-shrink-1 mr-2">
{trackSection ? (
Expand Down

0 comments on commit 847a84e

Please sign in to comment.