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: source extension for editor #7323

Merged
merged 1 commit into from
Apr 26, 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
12 changes: 12 additions & 0 deletions front/public/locales/en/infraEditor.json
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,18 @@
}
}
},
"TrackSectionSourceExtension": {
"properties": {
"name": {
"title": "Source",
"description": "Name of the source"
},
"id": {
"title": "Source ID",
"description": "ID of the element within the source"
}
}
},
"TvmSystem": {
"properties": {
"next_signaling_systems": {
Expand Down
1 change: 1 addition & 0 deletions front/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@
"of-type": "of type",
"selection": "{{count}} item selected",
"selection_plural": "{{count}} items selected",
"sources": "Sources",
"title": "Select",
"unselect": "Deselect"
},
Expand Down
12 changes: 12 additions & 0 deletions front/public/locales/fr/infraEditor.json
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,18 @@
}
}
},
"TrackSectionSourceExtension": {
"properties": {
"name": {
"title": "Source",
"description": "Nom de la source"
},
"id": {
"title": "Source ID",
"description": "Identifiant de l'élément dans la source"
}
}
},
"TvmSystem": {
"properties": {
"next_signaling_systems": {
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 @@ -377,6 +377,7 @@
"of-type": "de type",
"selection": "{{count}} élément sélectionné",
"selection_plural": "{{count}} éléments sélectionnés",
"sources": "Sources",
"title": "Sélection",
"unselect": "Désélectionner"
},
Expand Down
15 changes: 14 additions & 1 deletion front/src/applications/editor/components/EntitySumUp.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { type FC, useEffect, useState } from 'react';
import React, { type FC, useEffect, useState, Fragment } from 'react';

import cx from 'classnames';
import type { TFunction } from 'i18next';
Expand Down Expand Up @@ -113,6 +113,7 @@ function getSumUpContent(
let text = '';
const subtexts: (string | JSX.Element)[] = [];
const classes = { ...DEFAULT_CLASSES, ...(classesOverride || {}) };
const sources: string[] = [];

switch (entity.objType) {
case 'TrackSection': {
Expand All @@ -125,6 +126,10 @@ function getSumUpContent(
text = trackSection.properties.id;
}
if (attrs.line_name) subtexts.unshift(attrs.line_name);
const source = trackSection.properties.extensions?.source;
if (source) {
sources.push(`${source.name} ${source.id}`);
}
break;
}
// @ts-expect-error: Here we only deal with the installation_type, the rest is handled with BufferStop and Detector.
Expand Down Expand Up @@ -280,6 +285,14 @@ function getSumUpContent(
{s}
</div>
))}
{sources.length > 0 && (
<div>
<span className={cx(classes.strong, 'mr-2')}>
{t(`Editor.tools.select-items.sources`)}
</span>
{sources.join(', ')}
</div>
)}
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React, { useContext, useEffect, useRef } from 'react';
import React, { useContext, useEffect, useRef, useMemo } from 'react';

import type { WidgetProps } from '@rjsf/utils';
import { isNil, omit } from 'lodash';
import { useTranslation } from 'react-i18next';

import EditorForm from 'applications/editor/components/EditorForm';
import EntityError from 'applications/editor/components/EntityError';
import EditorContext from 'applications/editor/context';
import { NEW_ENTITY_ID } from 'applications/editor/data/utils';
import {
NEW_ENTITY_ID,
getJsonSchemaForLayer,
getLayerForObjectType,
} from 'applications/editor/data/utils';
import type {
TrackEditionState,
TrackSectionEntity,
Expand All @@ -32,7 +37,7 @@ const TrackEditionLeftPanel: React.FC = () => {
const dispatch = useAppDispatch();
const { t } = useTranslation();
const infraID = useInfraID();
const { state, setState, isFormSubmited, setIsFormSubmited } = useContext(
const { state, setState, isFormSubmited, setIsFormSubmited, editorState } = useContext(
EditorContext
) as ExtendedEditorContextType<TrackEditionState>;
const submitBtnRef = useRef<HTMLButtonElement>(null);
Expand All @@ -49,14 +54,40 @@ const TrackEditionLeftPanel: React.FC = () => {
}
}, [isFormSubmited]);

const schema = useMemo(
() =>
getJsonSchemaForLayer(
editorState.editorSchema,
getLayerForObjectType(editorState.editorSchema, track.objType) || ''
),
[editorState.editorSchema, track.objType]
);

return (
<>
<EditorForm
data={track}
// Remove the source from schema if there is no source in the object
// To avoid to display it on the form
overrideSchema={
isNil(track.properties.extensions?.source)
? omit(schema, ['$defs.TrackSectionExtensions.properties.source'])
: schema
}
overrideUiSchema={{
length: {
'ui:widget': CustomLengthInput,
},
extensions: {
source: {
id: {
'ui:readonly': true,
},
name: {
'ui:readonly': true,
},
},
},
}}
onSubmit={async (savedEntity) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
4 changes: 4 additions & 0 deletions front/src/applications/editor/tools/trackEdition/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export type TrackSectionEntity = EditorEntity<
track_name?: string;
track_number?: number;
};
source?: {
id: string;
name: string;
};
};
}
> & {
Expand Down
Loading