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 psl announcement first value #5939

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
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 @@ -219,7 +219,7 @@ export const RangeEditionLeftPanel: FC = () => {
const { t } = useTranslation();
const {
setState,
state: { entity, initialEntity },
state: { entity, initialEntity, trackSectionsCache },
} = useContext(EditorContext) as ExtendedEditorContextType<
RangeEditionState<SpeedSectionEntity | CatenaryEntity>
>;
Expand Down Expand Up @@ -361,6 +361,7 @@ export const RangeEditionLeftPanel: FC = () => {
stateOrReducer: PartialOrReducer<RangeEditionState<SpeedSectionEntity>>
) => void
}
trackSectionsCache={trackSectionsCache}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React from 'react';
import { cloneDeep } from 'lodash';
import { PSLExtension, PSLSign, SpeedSectionEntity, SpeedSectionPslEntity } from 'types';
import React, { useMemo } from 'react';
import { cloneDeep, compact } from 'lodash';
import {
PSLExtension,
PSLSign,
SpeedSectionEntity,
SpeedSectionPslEntity,
TrackSectionEntity,
} from 'types';
import { useTranslation } from 'react-i18next';
import { PslSignInformation, PSL_SIGN_TYPES, RangeEditionState } from '../types';
import { removeDuplicates } from 'utils/array';
import { PslSignInformation, PSL_SIGN_TYPES, RangeEditionState, TrackState } from '../types';
import PslSignCard from './PslSignCard';
import PslSignSubSection from './PslSignSubSection';
import { msToKmh, selectPslSign } from '../utils';
Expand All @@ -18,8 +25,8 @@ const getNewAnnouncementSign = (
return {
angle_geo: 0,
angle_sch: 0,
position: firstRange.end,
side: 'RIGHT',
position: firstRange.begin,
side: 'LEFT',
SharglutDev marked this conversation as resolved.
Show resolved Hide resolved
track: firstRange.track,
type: 'TIV_D',
value: `${speedMultipleOfFive}`,
Expand All @@ -43,16 +50,48 @@ const getNewRSign = (
} as PSLSign;
};

const validateSignPosition = (sign: PSLSign, tracks: TrackSectionEntity[]) => {
const validSign = cloneDeep(sign);
const signTrack = tracks.find((track) => track.properties.id === sign.track);
SharglutDev marked this conversation as resolved.
Show resolved Hide resolved
if (signTrack && sign.position > signTrack.properties.length) {
return {
...validSign,
position: signTrack.properties.length,
};
}
if (sign.position < 0) {
return {
...validSign,
position: 0,
};
}
return validSign;
};

const EditPSLSection = ({
entity,
trackSectionsCache,
setState,
}: {
entity: SpeedSectionPslEntity;
trackSectionsCache: Record<string, TrackState>;
setState: (stateOrReducer: PartialOrReducer<RangeEditionState<SpeedSectionEntity>>) => void;
}) => {
const { t } = useTranslation();
const pslExtension = entity.properties.extensions.psl_sncf;

const tracks = useMemo(() => {
const { psl_sncf: pslExtensionSigns } = entity.properties.extensions;
const signs = [pslExtensionSigns.z, ...pslExtensionSigns.r, ...pslExtensionSigns.announcement];
const signTrackIds = removeDuplicates(signs.map((sign) => sign.track));
return compact(
signTrackIds.map((trackId) => {
const track = trackSectionsCache[trackId];
return track?.type !== 'success' ? null : track.track;
})
);
}, [entity.properties.extensions.psl_sncf]);

const selectSign = (signInformation: PslSignInformation) => {
selectPslSign(signInformation, setState);
};
Expand All @@ -67,7 +106,7 @@ const EditPSLSection = ({
const newPslExtension = cloneDeep(pslExtension);
const trackRanges = entity.properties.track_ranges || [];
if (signType === PSL_SIGN_TYPES.ANNOUNCEMENT) {
const speedLimit = entity.properties.speed_limit || 30;
const speedLimit = entity.properties.speed_limit || 30; // We should not have the value 30, 0 seems more accurate but we can't display it.
newPslExtension.announcement = [
...pslExtension.announcement,
getNewAnnouncementSign(trackRanges, speedLimit),
Expand All @@ -80,15 +119,17 @@ const EditPSLSection = ({

const updateSign = (signInfo: PslSignInformation, sign: PSLSign) => {
const newPslExtension = cloneDeep(pslExtension);
const newSign = validateSignPosition(sign, tracks);

const { signType } = signInfo;
if (signType === PSL_SIGN_TYPES.Z) {
newPslExtension.z = sign;
newPslExtension.z = newSign;
} else {
const { signIndex } = signInfo;
if (signType === PSL_SIGN_TYPES.ANNOUNCEMENT) {
newPslExtension.announcement[signIndex] = sign;
newPslExtension.announcement[signIndex] = newSign;
} else {
newPslExtension.r[signIndex] = sign;
newPslExtension.r[signIndex] = newSign;
}
}
updateEntity(newPslExtension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const PslSignCard = ({
position: Number(e.target.value !== '' ? e.target.value : ''),
});
}}
min={0}
sm
/>
</div>
Expand Down
2 changes: 2 additions & 0 deletions front/src/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ export const replaceElementAtIndex = <T>(array: T[], indexToRemove: number, newE
result.splice(indexToRemove, 1, newElement);
return result;
};

export const removeDuplicates = <T>(array: T[]) => [...new Set(array)];