Skip to content

Commit

Permalink
front: adapt conflicts to V2
Browse files Browse the repository at this point in the history
  • Loading branch information
Akctarus committed May 29, 2024
1 parent 4bdd9b5 commit 579ca20
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,10 @@ const ScenarioV2 = () => {
}
);

// const { refetch: refetchConflicts } =
// osrdEditoastApi.endpoints.getV2TimetableByIdConflicts.useQuery(
// { id: timetable!.id, infraId: infraId! },
// { skip: !timetable || !infraId }
// );
const { data: conflicts } = osrdEditoastApi.endpoints.getV2TimetableByIdConflicts.useQuery(
{ id: timetable?.id as number, infraId: infraId! },
{ skip: !timetable || !infraId }
);

useEffect(() => {
if (timetable && infra?.state === 'CACHED' && timetable.train_ids.length > 0) {
Expand Down Expand Up @@ -287,7 +286,6 @@ const ScenarioV2 = () => {
setDisplayTrainScheduleManagement={setDisplayTrainScheduleManagement}
setTrainResultsToFetch={setTrainResultsToFetch}
infraState={infra.state}
// refetchConflicts={() => {}}
/>
)}
{infra && (
Expand All @@ -297,7 +295,7 @@ const ScenarioV2 = () => {
infraState={infra.state}
trainIds={timetable.train_ids}
selectedTrainId={selectedTrainId}
conflicts={[]}
conflicts={conflicts}
setTrainResultsToFetch={setTrainResultsToFetch}
/>
)}
Expand Down
113 changes: 113 additions & 0 deletions front/src/modules/conflict/components/ConflictsListV2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import React from 'react';

import cx from 'classnames';
import { useTranslation } from 'react-i18next';
import { BsLightningFill } from 'react-icons/bs';

import type { ConflictV2 } from 'common/api/osrdEditoastApi';
import type { TrainScheduleWithDetails } from 'modules/trainschedule/components/TimetableV2/types';

type ConflictTableProps = {
conflicts: ConflictV2[];
expanded: boolean;
trainSchedulesDetails: TrainScheduleWithDetails[];
toggleConflictsList: () => void;
onClick: (conflict: ConflictWithTrainNames) => void;
};

type ConflictWithTrainNames = ConflictV2 & {
train_names: string[];
};

function addTrainNamesToConflicts(
conflicts: ConflictV2[],
trainSchedulesDetails: TrainScheduleWithDetails[]
): ConflictWithTrainNames[] {
const trainNameMap: { [id: number]: string } = {};

trainSchedulesDetails.forEach(({ id, trainName }) => {
trainNameMap[id] = trainName;
});

return conflicts.map((conflict) => ({
...conflict,
train_names: conflict.train_ids.map((id) => trainNameMap[id] || ''),
}));
}

function string2time(sec: string) {
const secNum = parseInt(sec, 10);
return new Date(secNum * 1000).toISOString().substr(11, 8);
}

function ConflictCard({
conflict,
onClick,
}: {
conflict: ConflictWithTrainNames;
onClick: (conflict: ConflictWithTrainNames) => void;
}) {
const { t } = useTranslation(['operationalStudies/scenario']);

return (
<div className="conflict-card" onClick={() => onClick(conflict)} role="button" tabIndex={0}>
<BsLightningFill color="red" />
<div className="conflict-trains">
{conflict.train_names.map((train_name, idx) => (
<div className="card-text" key={`train-${idx}-${train_name}`}>
{train_name}
</div>
))}
</div>
<div className="conflict-type">
<p>{t(`${conflict.conflict_type}`)}</p>
</div>
<div className="conflict-times">
<div className="start-time">{string2time(conflict.start_time)}</div>
<div className="end-time">{string2time(conflict.end_time)}</div>
</div>
</div>
);
}

export default function ConflictsList({
conflicts,
expanded,
trainSchedulesDetails,
toggleConflictsList,
onClick,
}: ConflictTableProps) {
const { t } = useTranslation(['operationalStudies/scenario']);
const enrichedConflicts = addTrainNamesToConflicts(conflicts, trainSchedulesDetails);

if (conflicts.length === 0) {
return null;
}
return (
<div className="conflicts-list">
<div
className="conflicts-list-header"
role="button"
tabIndex={0}
onClick={toggleConflictsList}
>
<h2>
{t('conflictsCount', {
count: conflicts.length,
})}
</h2>
<i className={cx('icons-arrow-up', expanded && 'expanded')} />
</div>

<div className={cx('conflicts-container', expanded && 'expanded')}>
{enrichedConflicts.map((conflict, index) => (
<ConflictCard
key={`${conflict.train_ids.join(', ')}-${conflict.conflict_type}-${index}`}
conflict={conflict}
onClick={onClick}
/>
))}
</div>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ import formatTrainSchedulePayload from './helpers/formatTrainSchedulePayload';
type SubmitConfAddTrainScheduleProps = {
infraState?: InfraState;
// refetchTimetable: () => void;
// refetchConflicts: () => void;
setIsWorking: (isWorking: boolean) => void;
// setTrainResultsToFetch: (trainScheduleIds?: number[]) => void;
};

const AddTrainScheduleV2Button = ({
infraState,
// refetchTimetable,
// refetchConflicts,
setIsWorking,
// setTrainResultsToFetch,
}: SubmitConfAddTrainScheduleProps) => {
Expand Down Expand Up @@ -87,7 +85,6 @@ const AddTrainScheduleV2Button = ({
setIsWorking(false);
// setTrainResultsToFetch(newTrainIds);
// refetchTimetable();
// refetchConflicts();
} catch (e) {
setIsWorking(false);
dispatch(setFailure(castErrorToFailure(e)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type TimetableManageTrainScheduleProps = {
setTrainResultsToFetch: (trainScheduleIds?: number[]) => void;
infraState?: InfraState;
// refetchTimetable: () => void;
// refetchConflicts: () => void;
};

const TimetableManageTrainScheduleV2 = ({
Expand All @@ -28,7 +27,6 @@ const TimetableManageTrainScheduleV2 = ({
setTrainResultsToFetch,
infraState,
// refetchTimetable,
// refetchConflicts,
}: TimetableManageTrainScheduleProps) => {
const { t } = useTranslation('operationalStudies/manageTrainSchedule');
const dispatch = useDispatch();
Expand Down Expand Up @@ -67,7 +65,6 @@ const TimetableManageTrainScheduleV2 = ({
infraState={infraState}
// refetchTimetable={refetchTimetable}
setIsWorking={setIsWorking}
// refetchConflicts={refetchConflicts}
// setTrainResultsToFetch={setTrainResultsToFetch}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

import { MANAGE_TRAIN_SCHEDULE_TYPES } from 'applications/operationalStudies/consts';
import type { Conflict, InfraState } from 'common/api/osrdEditoastApi';
import type { ConflictV2, InfraState } from 'common/api/osrdEditoastApi';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import ConflictsList from 'modules/conflict/components/ConflictsList';
import ConflictsListV2 from 'modules/conflict/components/ConflictsListV2';
import { updateSelectedTrainId } from 'reducers/osrdsimulation/actions';
import { getTrainIdUsedForProjection } from 'reducers/osrdsimulation/selectors';
import { useAppDispatch } from 'store';
Expand All @@ -26,7 +26,7 @@ type TimetableV2Props = {
infraState: InfraState;
trainIds: number[];
selectedTrainId?: number;
conflicts?: Conflict[];
conflicts?: ConflictV2[];
setTrainResultsToFetch: (trainScheduleIds?: number[]) => void;
};

Expand Down Expand Up @@ -86,7 +86,7 @@ const TimetableV2 = ({
setSelectedTrainIds(currentSelectedTrainIds);
};

const handleConflictClick = (conflict: Conflict) => {
const handleConflictClick = (conflict: ConflictV2) => {
if (conflict.train_ids.length > 0) {
const firstTrainId = conflict.train_ids[0];
dispatch(updateSelectedTrainId(firstTrainId));
Expand Down Expand Up @@ -176,10 +176,11 @@ const TimetableV2 = ({
</div>
)}
{conflicts && (
<ConflictsList
<ConflictsListV2
conflicts={conflicts}
expanded={conflictsListExpanded}
toggleConflictsList={toggleConflictsListExpanded}
trainSchedulesDetails={trainSchedulesDetails}
onClick={handleConflictClick}
/>
)}
Expand Down

0 comments on commit 579ca20

Please sign in to comment.