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 19, 2024
1 parent fac9976 commit dc038fe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 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 @@ -429,6 +429,7 @@
"pick-track-cancel": "Undo line selection",
"save-switch": "Save the switch"
},
"duplicate-errors": "The track {{track}} is already used by the port {{port}}",
"endpoint": "Endpoint:",
"help": {
"no-move": "Switches cannot be moved, and are automatically placed on their first port.",
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 @@ -429,6 +429,7 @@
"pick-track-cancel": "Annuler la sélection d'une ligne",
"save-switch": "Sauvegarder l'aiguillage"
},
"duplicate-errors": "La voie {{track}} est déjà utilisée par le port {{port}}",
"endpoint": "Extrémité :",
"help": {
"no-move": "Les aiguillages ne peuvent pas être déplacés, et sont automatiquement placés sur leur premier port.",
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) {
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,10 +1,10 @@
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';
import { FaMapMarkedAlt, FaTimesCircle } from 'react-icons/fa';
import type { FieldProps } from '@rjsf/core';
import { keyBy } from 'lodash';
import { isEmpty, isNil, keyBy } from 'lodash';

import EditorContext from 'applications/editor/context';
import { getEntity } from 'applications/editor/data/api';
Expand All @@ -29,6 +29,22 @@ const TrackSectionEndpointSelector = ({ schema, formData, onChange, name }: Fiel
const { state, setState } = useContext(
EditorContext
) as ExtendedEditorContextType<SwitchEditionState>;

const duplicateWith = useMemo(() => {
const allPorts = Object.entries(state.entity.properties?.ports ?? {});
const ports = allPorts
.filter(([_, v]) => !isNil(v) && !isEmpty(v))
.map(([k, v]) => ({
...v,
port: k,
name: `port::${k}`,
}));

if (!ports.length) return [];
const currentPort = ports.find((p) => p.name === name);
return ports.filter((p) => p.name !== name && p.track === currentPort?.track);
}, [state.entity.properties]);

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

Expand Down Expand Up @@ -89,6 +105,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 }, i) => (
<div className="text-danger small font-weight-bold" key={`${name}-${track}-${i}`}>
{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 dc038fe

Please sign in to comment.