Skip to content

Commit

Permalink
front: remove flag for the edition source, call dtoimport on relevant…
Browse files Browse the repository at this point in the history
… files
  • Loading branch information
RomainValls committed Dec 9, 2024
1 parent 4c19f93 commit b2b3224
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useCallback } from 'react';
import { useState, useCallback } from 'react';

import { ChevronRight } from '@osrd-project/ui-icons';
import cx from 'classnames';
Expand Down Expand Up @@ -60,33 +60,26 @@ const ScenarioContent = ({
removeTrains,
} = useScenarioData(scenario, timetable, infra);

const [editFromTimetable, setEditFromTimetable] = useState(false);
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);
}
if (isMacroMode) {
dtoImport();
}
},
[setIsMacro, collapsedTimetable]
);

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

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

if (isMacro || editFromTimetable) {
dtoImport();
}
}, [scenario, isMacro, editFromTimetable]);
const handleNGEOperation = (event: NGEEvent, netzgrafikDto: NetzgrafikDto) => {
handleOperation({
event,
Expand Down Expand Up @@ -131,7 +124,7 @@ const ScenarioContent = ({
trainIdToEdit={trainIdToEdit}
setTrainIdToEdit={setTrainIdToEdit}
infraState={infra.state}
setEditFromTimetable={setEditFromTimetable}
dtoImport={dtoImport}
/>
)}
<Timetable
Expand All @@ -145,6 +138,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,7 +18,7 @@ type TimetableManageTrainScheduleProps = {
upsertTrainSchedules: (trainSchedules: TrainScheduleResult[]) => void;
infraState?: InfraState;
setTrainIdToEdit: (trainIdToEdit?: number) => void;
setEditFromTimetable: (editFromTimetable: boolean) => void;
dtoImport: () => void;
};

const TimetableManageTrainSchedule = ({
Expand All @@ -28,23 +28,23 @@ const TimetableManageTrainSchedule = ({
infraState,
trainIdToEdit,
setTrainIdToEdit,
setEditFromTimetable,
dtoImport,
}: TimetableManageTrainScheduleProps) => {
const { t } = useTranslation('operationalStudies/manageTrainSchedule');
const [isWorking, setIsWorking] = useState(false);

const leaveManageTrainSchedule = () => {
setDisplayTrainScheduleManagement(MANAGE_TRAIN_SCHEDULE_TYPES.none);
setTrainIdToEdit(undefined);
setEditFromTimetable(false);
dtoImport();
};

const updateTrainSchedule = useUpdateTrainSchedule(
setIsWorking,
setDisplayTrainScheduleManagement,
upsertTrainSchedules,
setTrainIdToEdit,
setEditFromTimetable,
dtoImport,
trainIdToEdit
);
return (
Expand Down Expand Up @@ -80,6 +80,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,7 +19,7 @@ const useUpdateTrainSchedule = (
setDisplayTrainScheduleManagement: (type: string) => void,
upsertTrainSchedules: (trainSchedules: TrainScheduleResult[]) => void,
setTrainIdToEdit: (trainIdToEdit?: number) => void,
setEditFromTimetable: (editFromTimetable: boolean) => void,
dtoImport: () => void,
trainIdToEdit?: number
) => {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
Expand Down Expand Up @@ -51,8 +51,8 @@ const useUpdateTrainSchedule = (
id: trainIdToEdit,
trainScheduleForm: trainSchedule,
}).unwrap();
setEditFromTimetable(true);
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 b2b3224

Please sign in to comment.