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: fix update coordinates when change ch code #10346

Merged
merged 1 commit into from
Jan 17, 2025
Merged
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
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] };
SarahBellaha marked this conversation as resolved.
Show resolved Hide resolved

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
Loading