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
+ dispatch (
44
+ updateStdcmPathStep ( {
45
+ id : destination . id ,
46
+ updates : { arrival : generateISODateFromDateTime ( schedule ) } ,
47
+ } )
48
+ ) ;
67
49
} ;
68
50
69
- const onDestinationArrivalTypeChange = ( arrivalType : ArrivalTimeTypes ) => {
70
- dispatch ( updateDestinationArrivalType ( arrivalType ) ) ;
51
+ const onArrivalTypeChange = ( arrivalType : ArrivalTimeTypes ) => {
52
+ dispatch ( updateStdcmPathStep ( { id : destination . id , updates : { arrivalType } } ) ) ;
71
53
} ;
72
54
73
- const onDestinationToleranceChange = ( {
55
+ const onToleranceChange = ( {
74
56
toleranceBefore,
75
57
toleranceAfter,
76
58
} : {
77
59
toleranceBefore : number ;
78
60
toleranceAfter : number ;
79
61
} ) => {
80
- dispatch ( updateDestinationTolerances ( { toleranceBefore, toleranceAfter } ) ) ;
62
+ dispatch (
63
+ updateStdcmPathStep ( {
64
+ id : destination . id ,
65
+ updates : { tolerances : { before : toleranceBefore , after : toleranceAfter } } ,
66
+ } )
67
+ ) ;
81
68
} ;
82
69
83
70
return (
@@ -87,26 +74,19 @@ const StdcmDestination = ({
87
74
disabled = { disabled }
88
75
className = "extremity"
89
76
>
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 >
77
+ { 'uic' in destination && (
78
+ < StdcmOperationalPoint point = { destination } opPointId = { destination . id } disabled = { disabled } />
79
+ ) }
80
+ < StdcmOpSchedule
81
+ onArrivalChange = { onArrivalChange }
82
+ onArrivalTypeChange = { onArrivalTypeChange }
83
+ onArrivalToleranceChange = { onToleranceChange }
84
+ opTimingData = { destinationArrival }
85
+ opToleranceValues = { destinationToleranceValues }
86
+ opScheduleTimeType = { destination . arrivalType }
87
+ disabled = { disabled }
88
+ opId = "destination-arrival"
89
+ />
110
90
</ StdcmCard >
111
91
) ;
112
92
} ;
0 commit comments