Skip to content

Commit

Permalink
front: fix stdcm empty ch list in new query
Browse files Browse the repository at this point in the history
Signed-off-by: SharglutDev <[email protected]>
  • Loading branch information
SharglutDev committed Jan 20, 2025
1 parent 8affe7b commit 601f66f
Showing 1 changed file with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useMemo, useState } 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';
Expand All @@ -23,6 +24,20 @@ function formatChCode(chCode: string) {
return chCode === '' ? 'BV' : chCode;
}

const extractChCodes = (searchResults: SearchResultItemOperationalPoint[], selectedCI: CIOption) =>
searchResults
.filter((pr) => pr.name === selectedCI.name)
.reduce((acc, pr) => {
const newObject = {
label: formatChCode(pr.ch),
id: pr.ch,
coordinates: pr.geographic.coordinates as [number, number],
};
const isDuplicate = acc.some((option) => option.label === newObject.label);
if (!isDuplicate) acc.push(newObject);
return acc;
}, [] as CHOption[]);

const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperationalPointProps) => {
const { t } = useTranslation('stdcm');
const dispatch = useAppDispatch();
Expand Down Expand Up @@ -93,18 +108,7 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio

const handleCiSelect = (selectedSuggestion?: CIOption) => {
if (selectedSuggestion) {
const newChSuggestions = searchResults
.filter((pr) => pr.name === selectedSuggestion.name)
.reduce((acc, pr) => {
const newObject = {
label: formatChCode(pr.ch),
id: pr.ch,
coordinates: pr.geographic.coordinates as [number, number],
};
const isDuplicate = acc.some((option) => option.label === newObject.label);
if (!isDuplicate) acc.push(newObject);
return acc;
}, [] as CHOption[]);
const newChSuggestions = extractChCodes(searchResults, selectedSuggestion);
setChSuggestions(newChSuggestions);
} else {
setChSuggestions([]);
Expand Down Expand Up @@ -147,6 +151,15 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
}
}, [location]);

useEffect(() => {
// If we start a new query with inputs (ch suggestions will be empty at load),
// fetch the ch list again for the corresponding CI
if (chSuggestions.length === 0 && selectedCi && searchResults.length > 0) {
const updatedChSuggestions = extractChCodes(searchResults, selectedCi);
setChSuggestions(updatedChSuggestions);
}
}, [searchResults, selectedCi, chSuggestions]);

return (
<div className="location-line">
<div className="col-9 ci-input">
Expand Down

0 comments on commit 601f66f

Please sign in to comment.