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 2b09f41
Show file tree
Hide file tree
Showing 5 changed files with 39 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
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const SwitchEditionLeftPanel = () => {
}}
onSubmit={async (flatSwitch) => {
const entityToSave = flatSwitchToSwitch(switchType, flatSwitch as FlatSwitchEntity);

// 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
11 changes: 10 additions & 1 deletion front/src/applications/editor/tools/switchEdition/tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { save } from 'reducers/editor';
import { SwitchEntity, SwitchType } from 'types';
import { ConfirmModal } from 'common/BootstrapSNCF/ModalSNCF';

import { filter, groupBy } from 'lodash';
import { NEW_ENTITY_ID } from '../../data/utils';
import { Tool } from '../editorContextTypes';
import { DEFAULT_COMMON_TOOL_STATE } from '../commonToolState';
Expand Down Expand Up @@ -49,7 +50,15 @@ const SwitchEditionTool: Tool<SwitchEditionState> = {
icon: AiFillSave,
labelTranslationKey: 'Editor.tools.switch-edition.actions.save-switch',
isDisabled({ isLoading, state }) {
return state.portEditionState.type !== 'idle' || isLoading || false;
const ports = filter(state.entity?.properties?.ports ?? {}, (p) => !!p?.track);
const detectedDuplicates = filter(groupBy(ports, 'track'), (v, _) => v.length > 1);
return (
state.portEditionState.type !== 'idle' ||
ports.length === 0 ||
!!detectedDuplicates.length ||
isLoading ||
false
);
},
async onClick({ setIsFormSubmited }) {
if (setIsFormSubmited) {
Expand Down

0 comments on commit 2b09f41

Please sign in to comment.