Skip to content

Commit

Permalink
front: fix update coordinates when change ch code
Browse files Browse the repository at this point in the history
Signed-off-by: nncluzu <[email protected]>
  • Loading branch information
kmer2016 committed Jan 13, 2025
1 parent 673dc44 commit e5b65fc
Showing 1 changed file with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type StdcmOperationalPointProps = {
disabled?: boolean;
};

type Option = StdcmPathStep['location'] & { label: string };
type CIOption = StdcmPathStep['location'] & { label: string };
type CHOption = { label: string; id: string; coordinates: [number, number] };

function formatChCode(chCode: string) {
return chCode === '' ? 'BV' : chCode;
Expand All @@ -37,7 +38,7 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
isStdcm: true,
});

const [chSuggestions, setChSuggestions] = useState<{ label: string; id: string }[]>([]);
const [chSuggestions, setChSuggestions] = useState<CHOption[]>([]);

const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions;

Expand All @@ -55,12 +56,16 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
const selectedCh = useMemo(
() =>
location
? { label: formatChCode(location.secondary_code), id: location.secondary_code }
? {
label: formatChCode(location.secondary_code),
id: location.secondary_code,
coordinates: location.coordinates,
}
: undefined,
[location]
);

const ciSuggestions: Option[] = useMemo(
const ciSuggestions: CIOption[] = useMemo(
() =>
// Temporary filter added to show a more restrictive list of suggestions inside the stdcm app.
searchResults
Expand All @@ -70,7 +75,7 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
normalized(op.name).includes(normalized(searchTerm)) ||
op.trigram === searchTerm.toUpperCase()
)
.reduce<Option[]>((acc, p) => {
.reduce<CIOption[]>((acc, p) => {
const newObject = {
label: [p.trigram, p.name].join(' '),
trigram: p.trigram,
Expand All @@ -86,35 +91,39 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
[searchResults]
);

const handleCiSelect = (selectedSuggestion?: Option) => {
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,
};
const isDuplicate = acc.some((option) => option.label === newObject.label);
if (!isDuplicate) acc.push(newObject);
return acc;
},
[] as { label: string; id: string }[]
);
.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[]);
setChSuggestions(newChSuggestions);
} else {
setChSuggestions([]);
}
dispatch(updateStdcmPathStep({ id: pathStepId, updates: { location: selectedSuggestion } }));
};

const handleChSelect = (selectedChCode?: { id: string }) => {
const handleChSelect = (selectedChCode?: CHOption) => {
if (location && selectedChCode) {
dispatch(
updateStdcmPathStep({
id: pathStepId,
updates: { location: { ...location, secondary_code: selectedChCode?.id } },
updates: {
location: {
...location,
secondary_code: selectedChCode.id,
coordinates: selectedChCode.coordinates,
},
},
})
);
}
Expand Down Expand Up @@ -147,7 +156,7 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
value={selectedCi}
suggestions={ciSuggestions}
onChange={handleCiInputChange}
getSuggestionLabel={(option: Option) => option.label}
getSuggestionLabel={(option: CIOption) => option.label}
onSelectSuggestion={handleCiSelect}
resetSuggestions={resetSuggestions}
disabled={disabled}
Expand Down

0 comments on commit e5b65fc

Please sign in to comment.