-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathModalSuggeredVias.tsx
140 lines (132 loc) · 4.89 KB
/
ModalSuggeredVias.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { useContext } from 'react';
import { getSuggeredVias, getVias } from 'reducers/osrdconf/selectors';
import { replaceVias } from 'reducers/osrdconf';
import { useSelector, useDispatch } from 'react-redux';
import { useTranslation } from 'react-i18next';
import ModalHeaderSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalHeaderSNCF';
import ModalBodySNCF from 'common/BootstrapSNCF/ModalSNCF/ModalBodySNCF';
import ModalFooterSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalFooterSNCF';
import { GoDash, GoPlus, GoTrash } from 'react-icons/go';
import { ModalContext } from 'common/BootstrapSNCF/ModalSNCF/ModalProvider';
import { Spinner } from 'common/Loader';
import type { ArrayElement } from 'utils/types';
import type { PathResponse, PathWaypoint } from 'common/api/osrdEditoastApi';
import { formatUicToCi } from 'utils/strings';
import cx from 'classnames';
type Props = {
removeAllVias: () => void;
pathfindingInProgress?: boolean;
};
function LoaderPathfindingInProgress() {
return <Spinner className="loaderPathfindingInProgress" />;
}
export default function ModalSugerredVias({ removeAllVias, pathfindingInProgress }: Props) {
const dispatch = useDispatch();
const suggeredVias = useSelector(getSuggeredVias) as PathWaypoint[];
const vias = useSelector(getVias);
const { t } = useTranslation('operationalStudies/manageTrainSchedule');
const nbVias = suggeredVias ? suggeredVias.length - 1 : 0;
const selectedViasTracks = vias.map((via) => via.id);
const { closeModal } = useContext(ModalContext);
const removeViaFromPath = (step: ArrayElement<PathResponse['steps']>) => {
dispatch(
replaceVias(
vias.filter(
(via) =>
via.location?.track_section !== step.location.track_section ||
via.path_offset !== step.path_offset
)
)
);
};
const convertPathfindingVias = (steps: PathResponse['steps'], idxToAdd: number) => {
if (steps) {
const newVias = steps.slice(1, -1).flatMap((step, idx) => {
if (!step.suggestion || idxToAdd === idx) {
const viaCoordinates = step.geo?.coordinates;
return [
{
...step,
coordinates: viaCoordinates,
id: step.id || undefined,
name: step.name || undefined,
},
];
}
return [];
});
dispatch(replaceVias(newVias));
}
};
const formatVia = (via: ArrayElement<PathResponse['steps']>, idx: number, idxTrueVia: number) => (
<div
key={`suggested-via-modal-${via.id}-${idx}`}
className={cx('d-flex align-items-center p-1', via.suggestion && 'suggested-via-clickable')}
title={via.name!}
>
{!via.suggestion && <small className="pr-2">{idxTrueVia}</small>}
<i className={`${via.suggestion ? 'text-muted' : 'text-info'} icons-itinerary-bullet mr-2`} />
<span className="suggested-via-name">{via.name || ''}</span>
<span>{via.ch}</span>
{via.uic && <small className="text-muted ml-3">{formatUicToCi(via.uic)}</small>}
<div className="ml-auto">
{via.path_offset && (
<small className="mr-2">{`KM ${Math.round(via.path_offset) / 1000}`}</small>
)}
{via.suggestion && via.id && !selectedViasTracks.includes(via.id) ? (
<button
className="btn btn-sm btn-only-icon"
type="button"
onClick={() => convertPathfindingVias(suggeredVias, idx - 1)}
>
<GoPlus />
</button>
) : (
<button
className="btn btn-sm btn-only-icon bg-dark"
type="button"
onClick={() => removeViaFromPath(via)}
>
<GoDash color="white" />
</button>
)}
</div>
</div>
);
let idxTrueVia = 0;
return (
<div className="manage-vias-modal">
<ModalHeaderSNCF>
<h1>{`${t('manageVias')} ${vias.length > 0 ? `(${vias.length})` : ''}`}</h1>
<button className="btn btn-only-icon close" type="button" onClick={closeModal}>
<i className="icons-close" />
</button>
</ModalHeaderSNCF>
<ModalBodySNCF>
<div className="suggested-vias">
{pathfindingInProgress && <LoaderPathfindingInProgress />}
{suggeredVias &&
suggeredVias.map((via, idx) => {
if (idx !== 0 && idx !== nbVias) {
if (!via.suggestion) idxTrueVia += 1;
return formatVia(via, idx, idxTrueVia);
}
return null;
})}
</div>
</ModalBodySNCF>
<ModalFooterSNCF>
<div className="w-100 mt-2 mb-n2">
<button
className="btn btn-danger btn-sm btn-block mb-1"
type="button"
onClick={removeAllVias}
>
<GoTrash />
<span className="ml-2">{t('deleteVias')}</span>
</button>
</div>
</ModalFooterSNCF>
</div>
);
}