-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
front/src/modules/conflict/components/ConflictsListV2.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters