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: add, get and delete paced trains #10966

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

SharglutDev
Copy link
Contributor

@SharglutDev SharglutDev commented Feb 26, 2025

See commits (will be merged all together)

part of #10615

Warning

PR not testable yet (depend on #11022)

E2E Tests :

@SharglutDev SharglutDev requested a review from a team as a code owner February 26, 2025 15:13
@github-actions github-actions bot added the area:front Work on Standard OSRD Interface modules label Feb 26, 2025
@SharglutDev SharglutDev marked this pull request as draft February 26, 2025 15:14
@codecov-commenter
Copy link

codecov-commenter commented Feb 26, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.58%. Comparing base (28261be) to head (7e8ff62).
Report is 6 commits behind head on dev.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##              dev   #10966       +/-   ##
===========================================
+ Coverage   80.63%   87.58%    +6.94%     
===========================================
  Files        1101       31     -1070     
  Lines      112360     1538   -110822     
  Branches      747        0      -747     
===========================================
- Hits        90602     1347    -89255     
+ Misses      21715      191    -21524     
+ Partials       43        0       -43     
Flag Coverage Δ
editoast ?
front ?
gateway ?
osrdyne ?
railjson_generator 87.58% <ø> (ø)
tests ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@SharglutDev SharglutDev self-assigned this Feb 26, 2025
@SharglutDev SharglutDev changed the title front: handle add paced trains add, get and delete paced trains Feb 26, 2025
@SharglutDev SharglutDev force-pushed the pfn/front/add-get-delete-paced-trains branch 9 times, most recently from 4cb8276 to 5cf88dd Compare March 6, 2025 17:28
@SharglutDev SharglutDev force-pushed the pfn/front/add-get-delete-paced-trains branch from 5cf88dd to 77ebbd0 Compare March 7, 2025 09:21
@emersion emersion self-requested a review March 7, 2025 10:25
@emersion emersion changed the title add, get and delete paced trains front: add, get and delete paced trains Mar 7, 2025
Diff for the 2 new files :
- useLadyLoadTimetableItems : copy of useLadyLoadTrains. Changes are to get item ids (reduce l.72) and merge paced train and train schedule summaries in one map.
- formatTimetableItemSummaries : as typescript don't get that the mapping of items work combines properly ids and items with details, had to first create 2 arrays for each type of item and then merge them in a map.

Signed-off-by: SharglutDev <[email protected]>
@SharglutDev SharglutDev force-pushed the pfn/front/add-get-delete-paced-trains branch from 77ebbd0 to 9d5a8e6 Compare March 7, 2025 10:37
@SharglutDev SharglutDev requested a review from RomainValls March 7, 2025 10:38
Copy link
Member

@emersion emersion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked at the first two commits! Will continue the review later.

Comment on lines +6 to +9
export default function formatTimetableItemPayload<T extends boolean>(
validConfig: ValidConfig,
{ isPacedTrainMode }: { isPacedTrainMode: T }
): T extends true ? PacedTrainBase : TrainScheduleBase {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, we do really need generics here? Could we instead have a function to format the common fields, and then separate functions for each type which call the common function?

{ isPacedTrainMode }: { isPacedTrainMode: T }
): T extends true ? PacedTrainBase : TrainScheduleBase {
const {
baseTrainName,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me like destructuring isn't buying us much here… The function would be half shorter without it, WDYT?

@@ -0,0 +1,52 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to disable this one? Which variables are unused?

upsertTrainSchedules([formattedNewTrainSchedule]);
}
} catch (e) {
setIsWorking(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid repetition, this could be moved to a finally block.

})
);
setIsWorking(false);
dtoImport();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid repetition, dtoImport() could be moved outside of the if/else.

if (showPacedTrains) {
// Prevent to block the train creation if a paced train field is invalid but we want to add a train schedule
if (isPacedTrainMode) {
if (cadence.total('minute') < 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: adding .total('minute') belongs to the previous commit, where the type of cadence is changed. (TypeScript missed this because Duration has a valueOf() method, which makes comparison operators work between Duration values… maybe it wasn't such a bright idea to implement this method…)

@emersion emersion self-requested a review March 7, 2025 16:05
@@ -49,7 +58,15 @@ const useScenarioData = (scenario: ScenarioResponse, infra: InfraWithState) => {
osrdEditoastApi.endpoints.getAllTimetableByIdTrainSchedules.useQuery(
{ timetableId: scenario?.timetable_id },
{
skip: !scenario,
skip: !scenario || showPacedTrains,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum I think you should fetch the paced trains even if we need to show the paced trains no ?

Suggested change
skip: !scenario || showPacedTrains,
skip: !scenario,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was I thinking 🙃

@@ -58,6 +75,9 @@ const useScenarioData = (scenario: ScenarioResponse, infra: InfraWithState) => {
[fetchedTrainSchedulesResults]
);

const pacedTrainsResults = useMemo(() => fetchedPacedTrains || [], [fetchedPacedTrains]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm you can replace pacedTrainResults by these formatted trains, and this way reuse it below, for instance in useEffects (same for trainScheduleResults)

Suggested change
const pacedTrainsResults = useMemo(() => fetchedPacedTrains || [], [fetchedPacedTrains]);
const pacedTrains: PacedTrainResultWithPacedTrainId[] = useMemo(
() =>
showPacedTrains
? (fetchedPacedTrains || []).map((trainSchedule) => ({
...trainSchedule,
id: formatEditoastTrainIdToPacedTrainId(trainSchedule.id),
}))
: [],
[showPacedTrains, fetchedPacedTrains]
);

Comment on lines +187 to +204
if (showPacedTrains) {
const formattedRawTrainSchedules: TrainScheduleResultWithTrainId[] =
trainSchedulesResults.map((trainSchedule) => ({
...trainSchedule,
id: formatEditoastTrainIdToTrainScheduleId(trainSchedule.id),
}));
const formattedRawPacedTrains: PacedTrainResultWithPacedTrainId[] = pacedTrainsResults.map(
(trainSchedule) => ({
...trainSchedule,
id: formatEditoastTrainIdToPacedTrainId(trainSchedule.id),
})
);
const sortedTimatableItems = sortBy(
[...formattedRawTrainSchedules, ...formattedRawPacedTrains],
'start_time'
);
setTimetableItems(sortedTimatableItems);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have this above:

  const trainSchedules: TrainScheduleResultWithTrainId[] = useMemo(
    () =>
      (fetchedTrainSchedulesResults || []).map((trainSchedule) => ({
        ...trainSchedule,
        id: formatEditoastTrainIdToTrainScheduleId(trainSchedule.id),
      })),
    [fetchedTrainSchedulesResults]
  );

then, you can simply do (pacedTrains will be empty if !showPacedTrains):

Suggested change
if (showPacedTrains) {
const formattedRawTrainSchedules: TrainScheduleResultWithTrainId[] =
trainSchedulesResults.map((trainSchedule) => ({
...trainSchedule,
id: formatEditoastTrainIdToTrainScheduleId(trainSchedule.id),
}));
const formattedRawPacedTrains: PacedTrainResultWithPacedTrainId[] = pacedTrainsResults.map(
(trainSchedule) => ({
...trainSchedule,
id: formatEditoastTrainIdToPacedTrainId(trainSchedule.id),
})
);
const sortedTimatableItems = sortBy(
[...formattedRawTrainSchedules, ...formattedRawPacedTrains],
'start_time'
);
setTimetableItems(sortedTimatableItems);
}
const sortedTimetableItems = sortBy([...trainSchedules, ...pacedTrains], 'start_time');
setTimetableItems(sortedTimetableItems);
}

Then, you can remove the previous useEffect

Comment on lines 42 to +44
const [trainSchedules, setTrainSchedules] = useState<TrainScheduleResultWithTrainId[]>();
const [trainIdsToFetch, setTrainIdsToFetch] = useState<TrainId[]>();
const [trainIdsToProject, setTrainIdsToProject] = useState<Set<TrainId>>(new Set());
const [trainIdsToFetch, setTrainIdsToFetch] = useState<TimetableItemId[]>();
const [trainIdsToProject, setTrainIdsToProject] = useState<Set<TimetableItemId>>(new Set());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove trainSchedules and trainIdsToFetch and directly use timetableItems

Suggested change
const [trainSchedules, setTrainSchedules] = useState<TrainScheduleResultWithTrainId[]>();
const [trainIdsToFetch, setTrainIdsToFetch] = useState<TrainId[]>();
const [trainIdsToProject, setTrainIdsToProject] = useState<Set<TrainId>>(new Set());
const [trainIdsToFetch, setTrainIdsToFetch] = useState<TimetableItemId[]>();
const [trainIdsToProject, setTrainIdsToProject] = useState<Set<TimetableItemId>>(new Set());
const [trainIdsToProject, setTrainIdsToProject] = useState<Set<TimetableItemId>>(new Set());

@SharglutDev SharglutDev force-pushed the pfn/front/add-get-delete-paced-trains branch from 9d5a8e6 to 42c86a8 Compare March 7, 2025 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:front Work on Standard OSRD Interface modules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants