1
+ import { useState } from 'react' ;
2
+
1
3
import { useSelector } from 'react-redux' ;
2
4
5
+ import type { LightRollingStockWithLiveries , TowedRollingStock } from 'common/api/osrdEditoastApi' ;
3
6
import { useOsrdConfActions , useOsrdConfSelectors } from 'common/osrdContext' ;
4
7
import { type StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf' ;
5
8
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors' ;
6
9
import { useAppDispatch } from 'store' ;
10
+ import { kgToT } from 'utils/physics' ;
7
11
8
12
const useStdcmConsist = ( ) => {
9
13
const dispatch = useAppDispatch ( ) ;
14
+
15
+ const [ totalMassChanged , setTotalMassChanged ] = useState ( false ) ;
16
+ const [ totalLengthChanged , setTotalLengthChanged ] = useState ( false ) ;
17
+ const [ maxSpeedChanged , setMaxSpeedChanged ] = useState ( false ) ;
18
+
10
19
const { getTotalMass, getTotalLength, getMaxSpeed } =
11
20
useOsrdConfSelectors ( ) as StdcmConfSelectors ;
12
21
const { updateTotalMass, updateTotalLength, updateMaxSpeed } =
@@ -15,28 +24,51 @@ const useStdcmConsist = () => {
15
24
const totalMass = useSelector ( getTotalMass ) ;
16
25
const onTotalMassChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
17
26
const totalMassValue = Number ( e . target . value ) ;
27
+ setTotalMassChanged ( true ) ;
18
28
dispatch ( updateTotalMass ( totalMassValue === 0 ? undefined : totalMassValue ) ) ;
19
29
} ;
20
30
21
31
const totalLength = useSelector ( getTotalLength ) ;
22
32
const onTotalLengthChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
23
33
const totalLengthValue = Number ( e . target . value ) ;
34
+ setTotalLengthChanged ( true ) ;
24
35
dispatch ( updateTotalLength ( totalLengthValue === 0 ? undefined : totalLengthValue ) ) ;
25
36
} ;
26
37
27
38
const maxSpeed = useSelector ( getMaxSpeed ) ;
28
39
const onMaxSpeedChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
29
40
const totalMaxSpeed = Number ( e . target . value ) ;
41
+ setMaxSpeedChanged ( true ) ;
30
42
dispatch ( updateMaxSpeed ( totalMaxSpeed === 0 ? undefined : totalMaxSpeed ) ) ;
31
43
} ;
32
44
45
+ const prefillConsist = (
46
+ rollingStock ?: LightRollingStockWithLiveries ,
47
+ towed ?: TowedRollingStock
48
+ ) => {
49
+ if ( ! totalMassChanged ) {
50
+ dispatch ( updateTotalMass ( Math . floor ( kgToT ( ( rollingStock ?. mass ?? 0 ) + ( towed ?. mass ?? 0 ) ) ) ) ) ;
51
+ }
52
+
53
+ if ( ! totalLengthChanged ) {
54
+ dispatch ( updateTotalLength ( Math . floor ( ( rollingStock ?. length ?? 0 ) + ( towed ?. length ?? 0 ) ) ) ) ;
55
+ }
56
+
57
+ if ( ! maxSpeedChanged ) {
58
+ dispatch (
59
+ updateMaxSpeed ( rollingStock ?. max_speed ? Math . floor ( rollingStock . max_speed ) : undefined )
60
+ ) ;
61
+ }
62
+ } ;
63
+
33
64
return {
34
65
totalMass,
35
66
onTotalMassChange,
36
67
totalLength,
37
68
onTotalLengthChange,
38
69
maxSpeed,
39
70
onMaxSpeedChange,
71
+ prefillConsist,
40
72
} ;
41
73
} ;
42
74
0 commit comments