1
- import { useMemo , useState } from 'react' ;
1
+ import { useMemo } from 'react' ;
2
2
3
3
import { useTranslation } from 'react-i18next' ;
4
+ import { useSelector } from 'react-redux' ;
4
5
5
6
import DestinationIcon from 'assets/pictures/stdcm/destination.svg' ;
6
- import { useOsrdConfActions } from 'common/osrdContext' ;
7
+ import { useOsrdConfActions , useOsrdConfSelectors } from 'common/osrdContext' ;
7
8
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf' ;
8
- import type { PathStep } from 'reducers/osrdconf/types ' ;
9
+ import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors ' ;
9
10
import { useAppDispatch } from 'store' ;
10
11
import { extractDateAndTimefromISO , generateISODateFromDateTime } from 'utils/date' ;
11
12
12
13
import StdcmCard from './StdcmCard' ;
13
14
import StdcmOperationalPoint from './StdcmOperationalPoint' ;
14
15
import StdcmOpSchedule from './StdcmOpSchedule' ;
15
16
import { DEFAULT_TOLERANCE } from '../../consts' ;
16
- import { ArrivalTimeTypes , type ScheduleConstraint , type StdcmConfigCardProps } from '../../types' ;
17
+ import type { ArrivalTimeTypes , ScheduleConstraint , StdcmConfigCardProps } from '../../types' ;
17
18
18
- const StdcmDestination = ( {
19
- disabled = false ,
20
- destination,
21
- } : StdcmConfigCardProps & {
22
- destination : PathStep | null ;
23
- } ) => {
19
+ const StdcmDestination = ( { disabled = false } : StdcmConfigCardProps ) => {
24
20
const { t } = useTranslation ( 'stdcm' ) ;
25
21
const dispatch = useAppDispatch ( ) ;
26
22
27
- const [ arrivalScheduleConstraint , setArrivalScheduleConstraint ] = useState < ScheduleConstraint > ( ) ;
23
+ const { getStdcmDestination } = useOsrdConfSelectors ( ) as StdcmConfSelectors ;
28
24
29
- const {
30
- updateDestination,
31
- updateDestinationArrival,
32
- updateDestinationArrivalType,
33
- updateDestinationTolerances,
34
- } = useOsrdConfActions ( ) as StdcmConfSliceActions ;
25
+ const destination = useSelector ( getStdcmDestination ) ;
26
+
27
+ const { updateStdcmPathStep } = useOsrdConfActions ( ) as StdcmConfSliceActions ;
35
28
36
29
const { destinationArrival, destinationToleranceValues } = useMemo (
37
30
( ) => ( {
38
- destinationArrival : destination ? .arrival
31
+ destinationArrival : destination . arrival
39
32
? extractDateAndTimefromISO ( destination . arrival )
40
33
: undefined ,
41
34
destinationToleranceValues : {
42
- arrivalToleranceBefore : destination ?. arrivalToleranceBefore || DEFAULT_TOLERANCE ,
43
- arrivalToleranceAfter : destination ?. arrivalToleranceAfter || DEFAULT_TOLERANCE ,
35
+ arrivalToleranceBefore : destination . tolerances ?. before || DEFAULT_TOLERANCE ,
36
+ arrivalToleranceAfter : destination . tolerances ?. after || DEFAULT_TOLERANCE ,
44
37
} ,
45
38
} ) ,
46
39
[ destination ]
47
40
) ;
48
41
49
- const updateDestinationPoint = ( pathStep : PathStep | null ) => {
50
- if ( ! pathStep || ! arrivalScheduleConstraint ) {
51
- dispatch ( updateDestination ( pathStep ) ) ;
52
- } else {
53
- dispatch (
54
- updateDestination ( {
55
- ...pathStep ,
56
- arrival : generateISODateFromDateTime ( arrivalScheduleConstraint ) ,
57
- } )
58
- ) ;
59
- }
60
- } ;
61
-
62
- const onDestinationArrivalChange = ( schedule : ScheduleConstraint ) => {
63
- setArrivalScheduleConstraint ( schedule ) ;
64
-
65
- const newOpArrival = generateISODateFromDateTime ( schedule ) ;
66
- dispatch ( updateDestinationArrival ( newOpArrival ) ) ;
42
+ const onArrivalChange = ( schedule : ScheduleConstraint ) => {
43
+ const newPathStep = { ...destination , arrival : generateISODateFromDateTime ( schedule ) } ;
44
+ dispatch ( updateStdcmPathStep ( newPathStep ) ) ;
67
45
} ;
68
46
69
- const onDestinationArrivalTypeChange = ( arrivalType : ArrivalTimeTypes ) => {
70
- dispatch ( updateDestinationArrivalType ( arrivalType ) ) ;
47
+ const onArrivalTypeChange = ( arrivalType : ArrivalTimeTypes ) => {
48
+ dispatch ( updateStdcmPathStep ( { ... destination , arrivalType } ) ) ;
71
49
} ;
72
50
73
- const onDestinationToleranceChange = ( {
51
+ const onToleranceChange = ( {
74
52
toleranceBefore,
75
53
toleranceAfter,
76
54
} : {
77
55
toleranceBefore : number ;
78
56
toleranceAfter : number ;
79
57
} ) => {
80
- dispatch ( updateDestinationTolerances ( { toleranceBefore, toleranceAfter } ) ) ;
58
+ dispatch (
59
+ updateStdcmPathStep ( {
60
+ ...destination ,
61
+ tolerances : { before : toleranceBefore , after : toleranceAfter } ,
62
+ } )
63
+ ) ;
81
64
} ;
82
65
83
66
return (
@@ -87,26 +70,19 @@ const StdcmDestination = ({
87
70
disabled = { disabled }
88
71
className = "extremity"
89
72
>
90
- < div className = "stdcm-destination" >
91
- < StdcmOperationalPoint
92
- updatePoint = { updateDestinationPoint }
93
- point = { destination }
94
- opPointId = { destination ?. id || 'destination' }
95
- disabled = { disabled }
96
- />
97
- { destination && (
98
- < StdcmOpSchedule
99
- onArrivalChange = { onDestinationArrivalChange }
100
- onArrivalTypeChange = { onDestinationArrivalTypeChange }
101
- onArrivalToleranceChange = { onDestinationToleranceChange }
102
- opTimingData = { destinationArrival }
103
- opToleranceValues = { destinationToleranceValues }
104
- opScheduleTimeType = { destination ?. arrivalType || ArrivalTimeTypes . ASAP }
105
- disabled = { disabled }
106
- opId = "destination-arrival"
107
- />
108
- ) }
109
- </ div >
73
+ { 'uic' in destination && (
74
+ < StdcmOperationalPoint point = { destination } opPointId = { destination . id } disabled = { disabled } />
75
+ ) }
76
+ < StdcmOpSchedule
77
+ onArrivalChange = { onArrivalChange }
78
+ onArrivalTypeChange = { onArrivalTypeChange }
79
+ onArrivalToleranceChange = { onToleranceChange }
80
+ opTimingData = { destinationArrival }
81
+ opToleranceValues = { destinationToleranceValues }
82
+ opScheduleTimeType = { destination . arrivalType }
83
+ disabled = { disabled }
84
+ opId = "destination-arrival"
85
+ />
110
86
</ StdcmCard >
111
87
) ;
112
88
} ;
0 commit comments