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: rename and refacto StdcmInputVia #10218

Merged
merged 2 commits into from
Jan 10, 2025
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

This file was deleted.

20 changes: 4 additions & 16 deletions front/src/applications/stdcm/components/StdcmForm/StdcmVias.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { useAppDispatch } from 'store';

import StdcmCard from './StdcmCard';
import StdcmDefaultCard from './StdcmDefaultCard';
import StdcmInputVia from './StdcmInputVia';
import StdcmOperationalPoint from './StdcmOperationalPoint';
import StdcmStopType from './StdcmStopType';
import StopDurationInput from './StopDurationInput';
import { StdcmStopTypes } from '../../types';
import type { StdcmConfigCardProps } from '../../types';

Expand Down Expand Up @@ -84,16 +84,6 @@ const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
};
}, [newIntermediateOpIndex]);

const updateStopDuration = (stopTime: string, pathStep: StdcmPathStep) => {
const stopFor = stopTime ? Number(stopTime) : undefined;
dispatch(
updateStdcmPathStep({
id: pathStep.id,
updates: { stopFor },
})
);
};

const deleteViaOnClick = (pathStepId: string) => {
dispatch(deleteStdcmVia(pathStepId));
};
Expand Down Expand Up @@ -147,11 +137,9 @@ const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
stopTypes={pathStep.stopType}
updatePathStepStopType={(newStopType) => updateStopType(newStopType, pathStep)}
/>
<StdcmInputVia
stopType={pathStep.stopType}
stopDuration={pathStep.stopFor}
updatePathStepStopTime={(e) => updateStopDuration(e, pathStep)}
/>
{pathStep.stopType !== StdcmStopTypes.PASSAGE_TIME && (
<StopDurationInput pathStep={pathStep} />
)}
</StdcmCard>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { useMemo, useEffect, useState } from 'react';

import { Input } from '@osrd-project/ui-core';
import type { Status } from '@osrd-project/ui-core/dist/components/inputs/StatusMessage';
import { useTranslation } from 'react-i18next';

import { useOsrdConfActions } from 'common/osrdContext';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { StdcmPathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { useDebounce } from 'utils/helpers';
import { parseNumber } from 'utils/strings';

import { StdcmStopTypes } from '../../types';

type StopDurationInputProps = {
pathStep: Extract<StdcmPathStep, { isVia: true }>;
};

const StopDurationInput = ({ pathStep }: StopDurationInputProps) => {
const dispatch = useAppDispatch();
const { t } = useTranslation('stdcm');

const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions;

const [stopDuration, setStopDuration] = useState(
pathStep.stopFor !== undefined ? `${pathStep.stopFor}` : ''
);
const debouncedStopDuration = useDebounce(stopDuration, 300);

const stopWarning = useMemo(
() =>
pathStep.stopType === StdcmStopTypes.DRIVER_SWITCH &&
pathStep.stopFor !== undefined &&
pathStep.stopFor < 3
? {
status: 'warning' as Status,
message: t('trainPath.warningMinStopTime'),
}
: undefined,
[pathStep.stopType, pathStep.stopFor]
);

useEffect(() => {
setStopDuration(pathStep.stopFor !== undefined ? `${pathStep.stopFor}` : '');
}, [pathStep.stopFor]);

useEffect(() => {
const parsedNumber = parseNumber(debouncedStopDuration);
const newStopDuration = parsedNumber !== undefined ? Math.round(parsedNumber) : undefined;
if (newStopDuration !== pathStep.stopFor) {
dispatch(
updateStdcmPathStep({
id: pathStep.id,
updates: { stopFor: newStopDuration },
})
);
}
}, [debouncedStopDuration]);

return (
<div className="stop-time">
<Input
id="stdcm-via-stop-time"
type="text"
label={t('trainPath.stopFor')}
onChange={(e) => {
setStopDuration(e.target.value);
}}
value={stopDuration}
trailingContent="minutes"
statusWithMessage={stopWarning}
/>
</div>
);
};

export default StopDurationInput;
3 changes: 1 addition & 2 deletions front/src/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export function geti18nKeyForNull(str: string | null): string {

/** Given a string, return a number or undefined */
export function parseNumber(str: string) {
const number = Number(str);
return !Number.isNaN(number) ? number : undefined;
return str && !Number.isNaN(Number(str)) ? Number(str) : undefined;
}

/**
Expand Down
Loading