1
1
import PropTypes from 'prop-types' ;
2
2
import React , { useEffect , useState } from 'react' ;
3
- import { get , patch } from 'common/requests' ;
4
- import { setFailure , setSuccess } from 'reducers/main' ;
5
- import { updateAllowancesSettings , updateMustRedraw , updateSimulation } from 'reducers/osrdsimulation/actions' ;
6
- import { useDispatch , useSelector } from 'react-redux' ;
3
+ import { updateAllowancesSettings } from 'reducers/osrdsimulation/actions' ;
4
+ import { useSelector } from 'react-redux' ;
7
5
8
6
import DotsLoader from 'common/DotsLoader/DotsLoader' ;
9
7
import { FaTrash } from 'react-icons/fa' ;
@@ -16,19 +14,7 @@ import SelectSNCF from 'common/BootstrapSNCF/SelectSNCF';
16
14
import StandardAllowanceDefault from 'applications/osrd/components/Simulation/Allowances/StandardAllowanceDefault' ;
17
15
import nextId from 'react-id-generator' ;
18
16
import { useTranslation } from 'react-i18next' ;
19
- import { trainscheduleURI } from 'applications/osrd/components/Simulation/consts' ;
20
-
21
- const TYPEUNITS = {
22
- time : 's' ,
23
- percentage : '%' ,
24
- time_per_distance : 'min/100km' ,
25
- } ;
26
-
27
- const TYPES_UNITS = {
28
- time : 'seconds' ,
29
- percentage : 'percentage' ,
30
- time_per_distance : 'minutes' ,
31
- } ;
17
+ import { TYPES_UNITS , ALLOWANCE_UNITS_KEYS } from './consts' ;
32
18
33
19
function EmptyLine ( props ) {
34
20
const {
@@ -42,7 +28,7 @@ function EmptyLine(props) {
42
28
marecoEndPosition,
43
29
defaultDistributionId,
44
30
} = props ;
45
- // console.log("Display EmptyLine", allowances)
31
+
46
32
const { selectedTrain } = useSelector ( ( state ) => state . osrdsimulation ) ;
47
33
const simulation = useSelector ( ( state ) => state . osrdsimulation . simulation . present ) ;
48
34
const allowanceNewDatas =
@@ -220,10 +206,8 @@ function EmptyLine(props) {
220
206
}
221
207
222
208
function Allowance ( props ) {
223
- const { data, delAllowance, idx } = props ;
209
+ const { data, delAllowance, idx, selectedTrain , simulation } = props ;
224
210
const { t } = useTranslation ( [ 'allowances' ] ) ;
225
- const { selectedTrain } = useSelector ( ( state ) => state . osrdsimulation ) ;
226
- const simulation = useSelector ( ( state ) => state . osrdsimulation . simulation . present ) ;
227
211
228
212
const position2name = ( position ) => {
229
213
const place = simulation . trains [ selectedTrain ] . base . stops . find (
@@ -248,7 +232,7 @@ function Allowance(props) {
248
232
< div className = "col-md-3 text-left" >
249
233
{ t ( `allowanceTypes.${ data . value . value_type } ` ) } /
250
234
{ data . value [ TYPES_UNITS [ data . value . value_type ] ] }
251
- { TYPEUNITS [ data . value . value_type ] }
235
+ { ALLOWANCE_UNITS_KEYS [ data . value . value_type ] }
252
236
</ div >
253
237
< div className = "col-md-1 d-flex align-items-right" >
254
238
< button
@@ -265,34 +249,41 @@ function Allowance(props) {
265
249
}
266
250
267
251
export default function Allowances ( props ) {
268
- const { toggleAllowancesDisplay } = props ;
269
- const { allowancesSettings, selectedProjection, selectedTrain } = useSelector (
270
- ( state ) => state . osrdsimulation
271
- ) ;
272
- const simulation = useSelector ( ( state ) => state . osrdsimulation . simulation . present ) ;
273
- const [ trainDetail , setTrainDetail ] = useState ( undefined ) ;
252
+ const {
253
+ toggleAllowancesDisplay,
254
+ t,
255
+ dispatch,
256
+ simulation,
257
+ allowancesSettings,
258
+ selectedProjection,
259
+ selectedTrain,
260
+ persistentAllowances,
261
+ syncInProgress,
262
+ mutateAllowances,
263
+ getAllowances,
264
+ trainDetail,
265
+ } = props ;
266
+
274
267
const [ allowances , setAllowances ] = useState ( [ ] ) ;
275
268
const [ rawExtensions ] = useState ( [ ] ) ;
276
269
const [ updateAllowances , setUpdateAllowances ] = useState ( false ) ;
277
270
const [ isUpdating , setIsUpdating ] = useState ( false ) ;
278
- const dispatch = useDispatch ( ) ;
279
- const { t } = useTranslation ( [ 'allowances' ] ) ;
280
271
281
272
const allowanceTypes = [
282
273
{
283
274
id : 'time' ,
284
275
label : t ( 'allowanceTypes.time' ) ,
285
- unit : TYPEUNITS . time ,
276
+ unit : ALLOWANCE_UNITS_KEYS . time ,
286
277
} ,
287
278
{
288
279
id : 'percentage' ,
289
280
label : t ( 'allowanceTypes.percentage' ) ,
290
- unit : TYPEUNITS . percentage ,
281
+ unit : ALLOWANCE_UNITS_KEYS . percentage ,
291
282
} ,
292
283
{
293
284
id : 'time_per_distance' ,
294
285
label : t ( 'allowanceTypes.time_per_distance' ) ,
295
- unit : TYPEUNITS . time_per_distance ,
286
+ unit : ALLOWANCE_UNITS_KEYS . time_per_distance ,
296
287
} ,
297
288
] ;
298
289
@@ -308,61 +299,8 @@ export default function Allowances(props) {
308
299
} ,
309
300
] ;
310
301
311
- const getAllowances = async ( ) => {
312
- try {
313
- setIsUpdating ( true ) ;
314
- const result = await get ( `${ trainscheduleURI } ${ simulation . trains [ selectedTrain ] . id } /` ) ;
315
- setTrainDetail ( result ) ;
316
- setAllowances ( result . allowances ) ;
317
- setIsUpdating ( false ) ;
318
- } catch ( e ) {
319
- console . log ( 'ERROR' , e ) ;
320
- dispatch (
321
- setFailure ( {
322
- name : e . name ,
323
- message : e . message ,
324
- } )
325
- ) ;
326
- }
327
- } ;
328
-
329
- // Change this to adapt to MARECO SPEC
330
- const changeAllowances = async ( newAllowances ) => {
331
- try {
332
- setIsUpdating ( true ) ;
333
- await patch ( `${ trainscheduleURI } ${ simulation . trains [ selectedTrain ] . id } /` , {
334
- ...trainDetail ,
335
- allowances : newAllowances ,
336
- } ) ;
337
- const newSimulationTrains = Array . from ( simulation . trains ) ;
338
- newSimulationTrains [ selectedTrain ] = await get (
339
- `${ trainscheduleURI } ${ simulation . trains [ selectedTrain ] . id } /result/` ,
340
- {
341
- id : simulation . trains [ selectedTrain ] . id ,
342
- path : selectedProjection . path ,
343
- }
344
- ) ;
345
-
346
- getAllowances ( ) ;
347
- dispatch ( updateSimulation ( { ...simulation , trains : newSimulationTrains } ) ) ;
348
- dispatch ( updateMustRedraw ( true ) ) ;
349
- dispatch (
350
- setSuccess ( {
351
- title : t ( 'allowanceModified.anyAllowanceModified' ) ,
352
- text : '' ,
353
- } )
354
- ) ;
355
- setIsUpdating ( false ) ;
356
- } catch ( e ) {
357
- setIsUpdating ( false ) ;
358
- console . log ( 'ERROR' , e ) ;
359
- dispatch (
360
- setFailure ( {
361
- name : e . name ,
362
- message : t ( 'allowanceModified.anyAllowanceModificationError' ) ,
363
- } )
364
- ) ;
365
- }
302
+ const handleChangeAllowances = ( newAllowances ) => {
303
+ mutateAllowances ( newAllowances ) ;
366
304
} ;
367
305
368
306
const delAllowance = ( idx , allowanceType ) => {
@@ -394,18 +332,22 @@ export default function Allowances(props) {
394
332
395
333
useEffect ( ( ) => {
396
334
if ( updateAllowances ) {
397
- changeAllowances ( allowances ) ;
335
+ handleChangeAllowances ( allowances ) ;
398
336
setUpdateAllowances ( false ) ;
399
337
}
400
338
} , [ allowances ] ) ;
401
339
402
340
useEffect ( ( ) => {
403
- getAllowances ( ) ;
404
- } , [ selectedTrain ] ) ;
341
+ setAllowances ( persistentAllowances ) ;
342
+ } , [ persistentAllowances ] ) ;
405
343
406
- const standardAllowance = allowances . find (
407
- ( allowance ) => allowance . allowance_type === 'standard' && allowance . ranges
408
- ) ;
344
+ useEffect ( ( ) => {
345
+ setIsUpdating ( syncInProgress ) ;
346
+ } , [ syncInProgress ] ) ;
347
+
348
+ const standardAllowance =
349
+ allowances &&
350
+ allowances . find ( ( allowance ) => allowance . allowance_type === 'standard' && allowance . ranges ) ;
409
351
410
352
// Engineergin can be defined alone, yet its default distribution depends on eventuel defined standard margin
411
353
@@ -419,7 +361,7 @@ export default function Allowances(props) {
419
361
< DotsLoader />
420
362
</ div >
421
363
) }
422
- { trainDetail && trainDetail . allowances && (
364
+ { allowances && (
423
365
< >
424
366
< div className = "h2 d-flex" >
425
367
< StandardAllowanceDefault
@@ -429,6 +371,11 @@ export default function Allowances(props) {
429
371
setIsUpdating = { setIsUpdating }
430
372
trainDetail = { trainDetail }
431
373
TYPES_UNITS = { TYPES_UNITS }
374
+ selectedTrain = { selectedTrain }
375
+ selectedProjection = { selectedProjection }
376
+ simulation = { simulation }
377
+ t = { t }
378
+ dispatch = { dispatch }
432
379
/>
433
380
< button
434
381
type = "button"
@@ -438,14 +385,21 @@ export default function Allowances(props) {
438
385
< i className = "icons-arrow-up" />
439
386
</ button >
440
387
</ div >
441
- { trainDetail . allowances . find ( ( a ) => a . ranges ) && (
388
+ { allowances . find ( ( a ) => a . ranges ) && (
442
389
< div className = "text-normal" > { t ( 'specificValuesOnIntervals' ) } </ div >
443
390
) }
444
391
445
- { trainDetail . allowances
392
+ { allowances
446
393
. find ( ( a ) => a . ranges )
447
394
?. ranges ?. map ( ( allowance , idx ) => (
448
- < Allowance data = { allowance } delAllowance = { delAllowance } idx = { idx } key = { nextId ( ) } />
395
+ < Allowance
396
+ data = { allowance }
397
+ delAllowance = { delAllowance }
398
+ idx = { idx }
399
+ key = { nextId ( ) }
400
+ selectedTrain = { selectedTrain }
401
+ simulation = { simulation }
402
+ />
449
403
) ) }
450
404
451
405
{ trainDetail . allowances . find ( ( a ) => a . ranges ) && (
@@ -472,7 +426,14 @@ export default function Allowances(props) {
472
426
{ trainDetail . allowances . map ( ( allowance , idx ) => {
473
427
if ( allowance . allowance_type === 'engineering' ) {
474
428
return (
475
- < Allowance data = { allowance } delAllowance = { delAllowance } idx = { idx } key = { nextId ( ) } />
429
+ < Allowance
430
+ data = { allowance }
431
+ delAllowance = { delAllowance }
432
+ idx = { idx }
433
+ key = { nextId ( ) }
434
+ selectedTrain = { selectedTrain }
435
+ simulation = { simulation }
436
+ />
476
437
) ;
477
438
}
478
439
return null ;
@@ -504,14 +465,14 @@ export default function Allowances(props) {
504
465
) ;
505
466
}
506
467
507
- Allowances . propTypes = {
508
- toggleAllowancesDisplay : PropTypes . func . isRequired ,
509
- } ;
510
-
511
468
Allowance . propTypes = {
512
469
data : PropTypes . object . isRequired ,
513
470
delAllowance : PropTypes . func . isRequired ,
514
471
idx : PropTypes . number . isRequired ,
472
+ t : PropTypes . func ,
473
+ dispatch : PropTypes . func ,
474
+ mutateAllowances : PropTypes . func ,
475
+ getAllowances : PropTypes . func ,
515
476
} ;
516
477
517
478
EmptyLine . propTypes = {
@@ -531,3 +492,10 @@ EmptyLine.defaultProps = {
531
492
allowanceType : 'construction' ,
532
493
marecoBeginPosition : 0 ,
533
494
} ;
495
+
496
+ Allowance . defaultProps = {
497
+ t : ( key ) => key ,
498
+ dispatch : ( ) => { } ,
499
+ getAllowances : ( ) => { } ,
500
+ mutateAllowances : ( ) => { } ,
501
+ } ;
0 commit comments