-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStdcmOperationalPoint.tsx
168 lines (150 loc) · 5.31 KB
/
StdcmOperationalPoint.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { useEffect, useMemo } from 'react';
import { Select, ComboBox } from '@osrd-project/ui-core';
import { useTranslation } from 'react-i18next';
import { type SearchResultItemOperationalPoint } from 'common/api/osrdEditoastApi';
import useSearchOperationalPoint from 'common/Map/Search/useSearchOperationalPoint';
import { useOsrdConfActions } from 'common/osrdContext';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { UicPathItemLocation } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { normalized } from 'utils/strings';
import { createFixedSelectOptions } from 'utils/uiCoreHelpers';
type StdcmOperationalPointProps = {
location?: UicPathItemLocation;
pathStepId: string;
disabled?: boolean;
};
type Option = { label: string; value: string; uic: number };
function formatChCode(chCode: string) {
return chCode === '' ? 'BV' : chCode;
}
const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperationalPointProps) => {
const { t } = useTranslation('stdcm');
const dispatch = useAppDispatch();
const { searchTerm, chCodeFilter, sortedSearchResults, setSearchTerm, setChCodeFilter } =
useSearchOperationalPoint({
initialSearchTerm: location?.name,
initialChCodeFilter: location?.secondary_code || undefined,
});
const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions;
const operationalPointsSuggestions = useMemo(
() =>
// Temporary filter added to show a more restrictive list of suggestions inside the stdcm app.
sortedSearchResults
.filter(
(op) =>
normalized(op.name).startsWith(normalized(searchTerm)) ||
op.trigram === searchTerm.toUpperCase()
)
.reduce((acc, p) => {
const newObject = {
label: [p.trigram, p.name].join(' '),
value: p.name,
uic: p.uic,
};
const isDuplicate = acc.some((pr) => pr.label === newObject.label);
if (!isDuplicate) acc.push(newObject);
return acc;
}, [] as Option[]),
[sortedSearchResults]
);
const sortedChOptions = useMemo(
() =>
sortedSearchResults
.filter((pr) => pr.name === searchTerm)
.reduce(
(acc, pr) => {
const newObject = {
label: formatChCode(pr.ch),
id: pr.ch,
};
const isDuplicate = acc.some((option) => option.label === newObject.label);
if (!isDuplicate) acc.push(newObject);
return acc;
},
[] as { label: string; id: string }[]
),
[location, sortedSearchResults]
);
const dispatchNewPoint = (p?: SearchResultItemOperationalPoint) => {
if (p && p.ch === location?.secondary_code && 'uic' in location && p.uic === location.uic)
return;
const newLocation = p
? {
name: p.name,
ch: p.ch,
uic: p.uic,
coordinates: p.geographic.coordinates as [number, number],
}
: undefined;
dispatch(updateStdcmPathStep({ id: pathStepId, updates: { location: newLocation } }));
};
const updateSelectedPoint = (
refList: SearchResultItemOperationalPoint[],
selectedUic: number,
selectedChCode?: string
) => {
const newPoint = refList.find(
(pr) => pr.uic === selectedUic && (selectedChCode ? pr.ch === selectedChCode : true)
);
dispatchNewPoint(newPoint);
};
const onSelectSuggestion = (selectedSuggestion?: Option) => {
if (!selectedSuggestion) {
setSearchTerm('');
return;
}
const { value: suggestionName, uic } = selectedSuggestion;
setSearchTerm(suggestionName);
updateSelectedPoint(sortedSearchResults, uic);
};
const onSelectChCodeFilter = (selectedChCode?: { id: string }) => {
setChCodeFilter(selectedChCode?.id);
if (location && 'uic' in location)
updateSelectedPoint(sortedSearchResults, location.uic, selectedChCode?.id);
};
const onInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchTerm(e.target.value);
if (e.target.value.trim().length === 0) {
dispatchNewPoint(undefined);
}
};
useEffect(() => {
if (location) {
setSearchTerm(location.name || '');
setChCodeFilter(location.secondary_code || '');
} else {
setSearchTerm('');
setChCodeFilter(undefined);
}
}, [location]);
return (
<div className="location-line">
<div className="col-9 ci-input">
<ComboBox
id={`${pathStepId}-ci`}
label={t('trainPath.ci')}
value={searchTerm}
onChange={onInputChange}
autoComplete="off"
suggestions={operationalPointsSuggestions}
disabled={disabled}
getSuggestionLabel={(option: Option) => option?.label}
onSelectSuggestion={onSelectSuggestion}
disableDefaultFilter
/>
</div>
<div className="col-3 p-0">
<Select
label={t('trainPath.ch')}
id={`${pathStepId}-ch`}
value={chCodeFilter ? { label: formatChCode(chCodeFilter), id: chCodeFilter } : undefined}
onChange={(e) => onSelectChCodeFilter(e)}
{...createFixedSelectOptions(sortedChOptions)}
disabled={disabled}
/>
</div>
</div>
);
};
export default StdcmOperationalPoint;