Skip to content

Commit 0c3db4e

Browse files
theocrsbSharglutDev
authored andcommitted
front: add suggestions using existing list of tags
1 parent 1178871 commit 0c3db4e

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

front/public/locales/en/translation.json

+1
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@
420420
"save-speed-section": "Save the speed limit"
421421
},
422422
"add-new-speed-limit": "Add new speed limit",
423+
"add-new-tag": "Add a new composition code",
423424
"add-sign": "Add a sign from {{signType}} type",
424425
"add-track-range": "Click to link to:",
425426
"additional-speed-limit": "Additional speed limits",

front/public/locales/fr/translation.json

+1
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@
403403
"save-speed-section": "Sauvegarder la vitesse limite"
404404
},
405405
"add-new-speed-limit": "Ajouter une nouvelle limitation",
406+
"add-new-tag": "Ajouter un nouveau code de composition",
406407
"add-sign": "Ajouter un panneau de type {{signType}}",
407408
"add-track-range": "Cliquer pour lier à :",
408409
"additional-speed-limit": "Limitation de vitesse additionnelles",

front/src/applications/editor/tools/rangeEdition/components/RangeEditionLeftPanel.tsx

+11-5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ const RangeEditionLeftPanel = () => {
9999
{ skip: !infraID }
100100
);
101101

102+
const { data: speedLimitTags } =
103+
osrdEditoastApi.endpoints.getInfraByInfraIdSpeedLimitTags.useQuery(
104+
{
105+
infraId: infraID as number,
106+
},
107+
{ skip: !infraID }
108+
);
109+
102110
const updateSpeedSectionExtensions = (
103111
extensions: SpeedSectionEntity['properties']['extensions']
104112
) => {
@@ -212,11 +220,9 @@ const RangeEditionLeftPanel = () => {
212220
<legend className="mb-4">
213221
{t(`Editor.obj-types.${isSpeedSection ? 'SpeedSection' : 'Electrification'}`)}
214222
</legend>
215-
{initialEntity.objType === 'SpeedSection' ? (
216-
<SpeedSectionMetadataForm />
217-
) : (
218-
voltages && <ElectrificationMetadataForm voltages={voltages} />
219-
)}
223+
{initialEntity.objType === 'SpeedSection'
224+
? speedLimitTags && <SpeedSectionMetadataForm speedLimitTags={speedLimitTags} />
225+
: voltages && <ElectrificationMetadataForm voltages={voltages} />}
220226
<hr />
221227
{initialEntity.objType === 'SpeedSection' && (
222228
<>

front/src/applications/editor/tools/rangeEdition/speedSection/SpeedSectionMetadataForm.tsx

+23-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { type FC, useContext, useEffect, useMemo, useState } from 'react';
1+
import React, { type FC, useContext, useEffect, useState } from 'react';
22

33
import { cloneDeep, isEmpty, isEqual, map, size } from 'lodash';
44
import { useTranslation } from 'react-i18next';
@@ -13,6 +13,7 @@ import type {
1313
SpeedSectionEntity,
1414
} from 'applications/editor/tools/rangeEdition/types';
1515
import type { ExtendedEditorContextType } from 'applications/editor/types';
16+
import SelectImprovedSNCF from 'common/BootstrapSNCF/SelectImprovedSNCF';
1617

1718
import SpeedInput from './SpeedInput';
1819

@@ -29,7 +30,9 @@ const getNewSpeedLimitTag = (
2930
return newSpeedLimitTag;
3031
};
3132

32-
const SpeedSectionMetadataForm: FC = () => {
33+
type SpeedSectionMetadataFormProps = { speedLimitTags: string[] };
34+
35+
const SpeedSectionMetadataForm: FC<SpeedSectionMetadataFormProps> = ({ speedLimitTags }) => {
3336
const { t } = useTranslation();
3437
const {
3538
state: { entity, error },
@@ -40,15 +43,6 @@ const SpeedSectionMetadataForm: FC = () => {
4043
{ id: string; tag: string; value?: number }[]
4144
>(map(entity.properties.speed_limit_by_tag, (value, tag) => ({ tag, value, id: nextId() })));
4245

43-
const tagCounts = useMemo(
44-
() =>
45-
tagSpeedLimits.reduce(
46-
(iter: Record<string, number>, { tag }) => ({ ...iter, [tag]: (iter[tag] || 0) + 1 }),
47-
{}
48-
),
49-
[tagSpeedLimits]
50-
);
51-
5246
useEffect(() => {
5347
const newState: Partial<RangeEditionState<SpeedSectionEntity>> = {};
5448
const newSpeedLimitByTags = tagSpeedLimits.reduce(
@@ -103,24 +97,24 @@ const SpeedSectionMetadataForm: FC = () => {
10397
)}
10498
{tagSpeedLimits.map(({ id, tag, value }, currentIndex) => (
10599
<div key={id} className="form-group field field-string">
106-
<div className="d-flex flex-row align-items-center">
107-
<input
108-
required
109-
className="form-control flex-grow-2 flex-shrink-1 mr-2 px-2"
110-
placeholder=""
111-
type="text"
112-
value={tag}
113-
pattern={
114-
// Insert an invalid pattern to force the error appearance when tag is redundant
115-
tagCounts[tag] && tagCounts[tag] > 1 ? `not ${tag}` : undefined
116-
}
117-
onChange={(e) => {
118-
const newKey = e.target.value;
119-
setTagSpeedLimits((state) =>
120-
state.map((pair, i) => (i === currentIndex ? { ...pair, tag: newKey } : pair))
121-
);
122-
}}
123-
/>
100+
<div className="d-flex flex-row align-items-center my-1">
101+
<div className="flex-grow-1 mr-2">
102+
<SelectImprovedSNCF
103+
options={speedLimitTags}
104+
value={tag}
105+
onChange={(tagSelected) => {
106+
setTagSpeedLimits((state) =>
107+
state.map((pair, i) =>
108+
i === currentIndex ? { ...pair, tag: tagSelected } : pair
109+
)
110+
);
111+
}}
112+
withSearch
113+
withNewValueInput
114+
addButtonTitle={t('Editor.tools.speed-edition.add-new-tag')}
115+
bgWhite
116+
/>
117+
</div>
124118
<SpeedInput
125119
className="form-control flex-shrink-0 px-2"
126120
style={{ width: '5em' }}

0 commit comments

Comments
 (0)