-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathRollingStockCardDetail.tsx
210 lines (201 loc) · 7.37 KB
/
RollingStockCardDetail.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, { useEffect } from 'react';
import cx from 'classnames';
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import { STANDARD_COMFORT_LEVEL } from 'modules/rollingStock/consts';
import { floor, isEmpty, uniq } from 'lodash';
import { setFailure } from 'reducers/main';
import { useDispatch } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { Loader } from 'common/Loaders/Loader';
import RollingStockCurves from 'modules/rollingStock/components/RollingStockCurve';
import RollingStock2Img from 'modules/rollingStock/components/RollingStock2Img';
import type { EffortCurveForms } from 'modules/rollingStock/types';
import type { RollingStockComfortType } from 'common/api/osrdEditoastApi';
type RollingStockCardDetailProps = {
id: number;
hideCurves?: boolean;
form?: string;
curvesComfortList: RollingStockComfortType[];
setCurvesComfortList: (curvesComfortList: RollingStockComfortType[]) => void;
};
export const getCurvesComforts = (curvesData: EffortCurveForms) => {
const modes = Object.keys(curvesData);
return modes.length
? modes.reduce<RollingStockComfortType[]>((list, mode) => {
const modeComfortList = curvesData[mode].curves.reduce(
(acc, curve) => {
const { comfort } = curve.cond;
if (comfort && !acc.includes(comfort)) acc.push(comfort);
return acc;
},
[STANDARD_COMFORT_LEVEL]
);
return uniq([...list, ...modeComfortList]);
}, [])
: [STANDARD_COMFORT_LEVEL];
};
export default function RollingStockCardDetail({
id,
hideCurves,
form,
curvesComfortList,
setCurvesComfortList,
}: RollingStockCardDetailProps) {
const dispatch = useDispatch();
const { t } = useTranslation(['rollingstock']);
// we only fetch the whole rollingStock here, when we open the card and display its details
const { data: rollingStock, error } = osrdEditoastApi.useGetRollingStockByRollingStockIdQuery(
{ rollingStockId: id },
{
skip: !id,
}
);
useEffect(() => {
if (rollingStock) setCurvesComfortList(getCurvesComforts(rollingStock.effort_curves.modes));
}, [rollingStock]);
useEffect(() => {
if (error) {
dispatch(
setFailure({
name: t('errorMessages.unableToRetrieveRollingStockMessage'),
message: t('errorMessages.unableToRetrieveRollingStock'),
})
);
}
}, [error]);
return rollingStock && !isEmpty(curvesComfortList) ? (
<div className={form ? 'px-4' : 'rollingstock-card-body'}>
<div className={`row pt-2 ${form}`}>
<div className="col-sm-6">
<table className="rollingstock-details-table">
<tbody>
<tr>
<td className="text-primary">{t('startupTime')}</td>
<td>
{rollingStock.startup_time}
<span className="small ml-1 text-muted">s</span>
</td>
</tr>
<tr>
<td className="text-primary">{t('startupAcceleration')}</td>
<td>
{rollingStock.startup_acceleration}
<span className="small ml-1 text-muted">m/s²</span>
</td>
</tr>
<tr>
<td className="text-primary">{t('comfortAcceleration')}</td>
<td>
{rollingStock.comfort_acceleration}
<span className="small ml-1 text-muted">m/s²</span>
</td>
</tr>
<tr>
<td className="text-primary">{t('inertiaCoefficient')}</td>
<td>{rollingStock.inertia_coefficient}</td>
</tr>
<tr>
<td className="text-primary">{t('gammaValue')}</td>
<td>
{rollingStock.gamma.value * -1}
<span className="small ml-1 text-muted">m/s²</span>
</td>
</tr>
<tr>
<td className="text-primary">{t('loadingGauge')}</td>
<td>{rollingStock.loading_gauge}</td>
</tr>
<tr>
<td className="text-primary">{t('basePowerClass')}</td>
<td>{rollingStock.base_power_class}</td>
</tr>
</tbody>
</table>
</div>
<div className="col-sm-6">
{rollingStock.power_restrictions &&
Object.keys(rollingStock.power_restrictions).length !== 0 && (
<table className="rollingstock-details-table mb-1">
<tbody>
<tr>
<td className="text-primary text-nowrap pr-1">
{t('powerRestrictionsInfos', {
count: Object.keys(rollingStock.power_restrictions).length,
})}
</td>
<td>
{rollingStock.power_restrictions !== null &&
Object.keys(rollingStock.power_restrictions).join(' ')}
</td>
</tr>
</tbody>
</table>
)}
<table className="rollingstock-details-table ml-2">
<tbody>
<tr>
<td colSpan={2} className={cx({ 'formResistance ml-2': form })}>
{t('rollingResistance')}
</td>
</tr>
<tr>
<td
colSpan={2}
className={cx('text-primary text-muted', { 'formResistance ml-4': form })}
>
{t('rollingResistanceFormula')}
</td>
</tr>
<tr>
<td>
{Math.floor(
rollingStock.rolling_resistance?.A
? (rollingStock.rolling_resistance.A * 10000) / 10000
: 0
)}
<span className="small ml-1 text-muted">N</span>
</td>
<td className="text-primary">{t('rollingResistanceA')}</td>
</tr>
<tr>
<td>
{Math.floor(
rollingStock.rolling_resistance?.B
? (rollingStock.rolling_resistance.B * 10000) / 10000
: 0
)}
<span className="small ml-1 text-muted">N/(m/s)</span>
</td>
<td className="text-primary">{t('rollingResistanceB')}</td>
</tr>
<tr>
<td title={rollingStock.rolling_resistance?.C.toString()}>
{floor(rollingStock.rolling_resistance?.C ?? 0, 2)}
<span className="small ml-1 text-muted">N/(m/s)²</span>
</td>
<td className="text-primary">{t('rollingResistanceC')}</td>
</tr>
</tbody>
</table>
</div>
</div>
{!hideCurves && (
<>
<RollingStockCurves
data={rollingStock.effort_curves.modes}
curvesComfortList={curvesComfortList}
/>
<div className="rollingstock-detail-container-img">
<div className="rollingstock-detail-img">
<RollingStock2Img rollingStock={rollingStock} />
</div>
</div>
</>
)}
</div>
) : (
<div className="rollingstock-card-body">
<Loader />
</div>
);
}