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: handle duplicate switch nodes errors #6351

Merged
merged 1 commit into from
Jan 22, 2024
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
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
15 changes: 14 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,19 @@ 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 portWithTracks = filter(state.entity?.properties?.ports ?? {}, (p) => !!p?.track);
const portsKeys = Object.keys(state.entity?.properties?.ports ?? {});
const detectedDuplicates = filter(
groupBy(portWithTracks, 'track'),
(v, _) => v.length > 1
);
return (
state.portEditionState.type !== 'idle' ||
portWithTracks.length !== portsKeys.length ||
!!detectedDuplicates.length ||
isLoading ||
false
);
},
async onClick({ setIsFormSubmited }) {
if (setIsFormSubmited) {
Expand Down