Skip to content

Commit

Permalink
front: edit timetable trains in macro mode
Browse files Browse the repository at this point in the history
Signed-off-by: romainvalls <[email protected]>
  • Loading branch information
RomainValls committed Dec 10, 2024
1 parent c608ccd commit f354aff
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useCallback } from 'react';
import { useState, useCallback, useEffect } from 'react';

import { ChevronRight } from '@osrd-project/ui-icons';
import cx from 'classnames';
Expand All @@ -24,7 +24,6 @@ import ScenarioLoaderMessage from 'modules/scenario/components/ScenarioLoaderMes
import TimetableManageTrainSchedule from 'modules/trainschedule/components/ManageTrainSchedule/TimetableManageTrainSchedule';
import Timetable from 'modules/trainschedule/components/Timetable/Timetable';
import { useAppDispatch } from 'store';
import { concatMap, mapBy } from 'utils/types';

import ScenarioDescription from './ScenarioDescription';

Expand Down Expand Up @@ -61,62 +60,44 @@ const ScenarioContent = ({
removeTrains,
} = useScenarioData(scenario, timetable, infra);

const [ngeDto, setNgeDto] = useState<NetzgrafikDto>();

const dtoImport = async () => {
const dto = await importTimetableToNGE(scenario.infra_id, scenario.timetable_id, dispatch);
setNgeDto(dto);
};

const toggleMicroMacroButton = useCallback(
(isMacroMode: boolean) => {
setIsMacro(isMacroMode);
if (!isMacroMode && collapsedTimetable) {
setCollapsedTimetable(false);
}
},
[setIsMacro, setCollapsedTimetable, collapsedTimetable]
[setIsMacro, collapsedTimetable]
);

const [ngeDto, setNgeDto] = useState<NetzgrafikDto>();
const [ngeUpsertedTrainSchedules, setNgeUpsertedTrainSchedules] = useState<
Map<number, TrainScheduleResult>
>(new Map());
const [ngeDeletedTrainIds, setNgeDeletedTrainIds] = useState<number[]>([]);

useEffect(() => {
if (!isMacro || (isMacro && ngeDto)) {
return;
}

const doImport = async () => {
const dto = await importTimetableToNGE(scenario.infra_id, scenario.timetable_id, dispatch);
setNgeDto(dto);
};
doImport();
}, [scenario, isMacro]);

useEffect(() => {
if (isMacro) {
return;
}

if (ngeDto) {
setNgeDto(undefined);
dtoImport();
}
upsertTrainSchedules(Array.from(ngeUpsertedTrainSchedules.values()));
removeTrains(ngeDeletedTrainIds);
}, [isMacro]);

const handleNGEOperation = (event: NGEEvent, netzgrafikDto: NetzgrafikDto) =>
const handleNGEOperation = (event: NGEEvent, netzgrafikDto: NetzgrafikDto) => {
handleOperation({
event,
dispatch,
infraId: infra.id,
timeTableId: scenario.timetable_id,
netzgrafikDto,
addUpsertedTrainSchedules: (upsertedTrainSchedules: TrainScheduleResult[]) => {
setNgeUpsertedTrainSchedules((prev) =>
concatMap(prev, mapBy(upsertedTrainSchedules, 'id'))
);
upsertTrainSchedules(upsertedTrainSchedules);
},
addDeletedTrainIds: (trainIds: number[]) => {
setNgeDeletedTrainIds((prev) => [...prev, ...trainIds]);
removeTrains(trainIds);
},
});
};

return (
<main className="mastcontainer mastcontainer-no-mastnav">
Expand Down Expand Up @@ -146,6 +127,7 @@ const ScenarioContent = ({
trainIdToEdit={trainIdToEdit}
setTrainIdToEdit={setTrainIdToEdit}
infraState={infra.state}
dtoImport={dtoImport}
/>
)}
<Timetable
Expand All @@ -159,6 +141,7 @@ const ScenarioContent = ({
trainIdToEdit={trainIdToEdit}
trainSchedules={trainSchedules}
trainSchedulesWithDetails={trainScheduleSummaries}
dtoImport={dtoImport}
/>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ type AddTrainScheduleButtonProps = {
infraState?: InfraState;
setIsWorking: (isWorking: boolean) => void;
upsertTrainSchedules: (trainSchedules: TrainScheduleResult[]) => void;
dtoImport: () => void;
};

const AddTrainScheduleButton = ({
infraState,
setIsWorking,
upsertTrainSchedules,
dtoImport,
}: AddTrainScheduleButtonProps) => {
const [postTrainSchedule] =
osrdEditoastApi.endpoints.postTimetableByIdTrainSchedule.useMutation();
Expand Down Expand Up @@ -83,6 +85,7 @@ const AddTrainScheduleButton = ({
})
);
setIsWorking(false);
dtoImport();
upsertTrainSchedules(newTrainSchedules);
} catch (e) {
setIsWorking(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type TimetableManageTrainScheduleProps = {
upsertTrainSchedules: (trainSchedules: TrainScheduleResult[]) => void;
infraState?: InfraState;
setTrainIdToEdit: (trainIdToEdit?: number) => void;
dtoImport: () => void;
};

const TimetableManageTrainSchedule = ({
Expand All @@ -27,6 +28,7 @@ const TimetableManageTrainSchedule = ({
infraState,
trainIdToEdit,
setTrainIdToEdit,
dtoImport,
}: TimetableManageTrainScheduleProps) => {
const { t } = useTranslation('operationalStudies/manageTrainSchedule');
const [isWorking, setIsWorking] = useState(false);
Expand All @@ -41,9 +43,9 @@ const TimetableManageTrainSchedule = ({
setDisplayTrainScheduleManagement,
upsertTrainSchedules,
setTrainIdToEdit,
dtoImport,
trainIdToEdit
);

return (
<div className="scenario-timetable-managetrainschedule">
<div className="scenario-timetable-managetrainschedule-header">
Expand Down Expand Up @@ -77,6 +79,7 @@ const TimetableManageTrainSchedule = ({
infraState={infraState}
setIsWorking={setIsWorking}
upsertTrainSchedules={upsertTrainSchedules}
dtoImport={dtoImport}
/>
)}
<TrainAddingSettings />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const useUpdateTrainSchedule = (
setDisplayTrainScheduleManagement: (type: string) => void,
upsertTrainSchedules: (trainSchedules: TrainScheduleResult[]) => void,
setTrainIdToEdit: (trainIdToEdit?: number) => void,
dtoImport: () => void,
trainIdToEdit?: number
) => {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
Expand Down Expand Up @@ -50,8 +51,8 @@ const useUpdateTrainSchedule = (
id: trainIdToEdit,
trainScheduleForm: trainSchedule,
}).unwrap();

upsertTrainSchedules([trainScheduleResult]);
dtoImport();
dispatch(
setSuccess({
title: t('trainUpdated'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type TimetableProps = {
trainIdToEdit?: number;
trainSchedules?: TrainScheduleResult[];
trainSchedulesWithDetails: TrainScheduleWithDetails[];
dtoImport: () => void;
};

const formatDepartureDate = (d: Date) => dayjs(d).locale(i18n.language).format('dddd D MMMM YYYY');
Expand All @@ -43,6 +44,7 @@ const Timetable = ({
trainIdToEdit,
trainSchedules = [],
trainSchedulesWithDetails,
dtoImport,
}: TimetableProps) => {
const { t } = useTranslation(['operationalStudies/scenario', 'common/itemTypes']);

Expand Down Expand Up @@ -160,6 +162,7 @@ const Timetable = ({
projectionPathIsUsed={
infraState === 'CACHED' && trainIdUsedForProjection === train.id
}
dtoImport={dtoImport}
/>
</div>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type TimetableTrainCardProps = {
setTrainIdToEdit: (trainIdToEdit?: number) => void;
removeTrains: (trainIds: number[]) => void;
projectionPathIsUsed: boolean;
dtoImport: () => void;
};

const formatFullDate = (d: Date) => dayjs(d).format('D/MM/YYYY HH:mm:ss');
Expand All @@ -50,6 +51,7 @@ const TimetableTrainCard = ({
setTrainIdToEdit,
removeTrains,
projectionPathIsUsed,
dtoImport,
}: TimetableTrainCardProps) => {
const { t } = useTranslation(['operationalStudies/scenario']);
const dispatch = useAppDispatch();
Expand Down Expand Up @@ -81,6 +83,7 @@ const TimetableTrainCard = ({
.unwrap()
.then(() => {
removeTrains([train.id]);
dtoImport();
dispatch(
setSuccess({
title: t('timetable.trainDeleted', { name: train.trainName }),
Expand Down Expand Up @@ -125,6 +128,7 @@ const TimetableTrainCard = ({
body: [newTrain],
}).unwrap();
upsertTrainSchedules([trainScheduleResult]);
dtoImport();
dispatch(
setSuccess({
title: t('timetable.trainAdded'),
Expand Down

0 comments on commit f354aff

Please sign in to comment.