@@ -11,6 +11,7 @@ import {
11
11
import { defaultCommonConf , buildCommonConfReducers } from 'reducers/osrdconf/osrdConfCommon' ;
12
12
import type { OsrdStdcmConfState , StdcmPathStep } from 'reducers/osrdconf/types' ;
13
13
import { addElementAtIndex } from 'utils/array' ;
14
+ import { isArrivalDateInSearchTimeWindow } from 'utils/date' ;
14
15
import type { ArrayElement } from 'utils/types' ;
15
16
16
17
const DEFAULT_TOLERANCE = 1800 ; // 30min
@@ -134,28 +135,48 @@ export const stdcmConfSlice = createSlice({
134
135
} ,
135
136
updateStdcmEnvironment (
136
137
state : Draft < OsrdStdcmConfState > ,
137
- action : PayloadAction <
138
- Pick <
139
- OsrdStdcmConfState ,
140
- | 'infraID'
141
- | 'timetableID'
142
- | 'electricalProfileSetId'
143
- | 'workScheduleGroupId'
144
- | 'temporarySpeedLimitGroupId'
145
- | 'searchDatetimeWindow'
146
- >
147
- >
138
+ action : PayloadAction < {
139
+ infraID : number ;
140
+ timetableID : number ;
141
+ searchDatetimeWindow ?: {
142
+ begin : Date ;
143
+ end : Date ;
144
+ } ;
145
+ electricalProfileSetId ?: number ;
146
+ temporarySpeedLimitGroupId ?: number ;
147
+ workScheduleGroupId ?: number ;
148
+ } >
148
149
) {
150
+ const { searchDatetimeWindow } = action . payload ;
149
151
state . infraID = action . payload . infraID ;
150
152
state . timetableID = action . payload . timetableID ;
151
153
state . electricalProfileSetId = action . payload . electricalProfileSetId ;
152
- state . searchDatetimeWindow = action . payload . searchDatetimeWindow ;
153
- if ( action . payload . workScheduleGroupId ) {
154
- state . workScheduleGroupId = action . payload . workScheduleGroupId ;
155
- }
156
- if ( action . payload . temporarySpeedLimitGroupId ) {
157
- state . temporarySpeedLimitGroupId = action . payload . temporarySpeedLimitGroupId ;
154
+ state . searchDatetimeWindow = searchDatetimeWindow ;
155
+ state . workScheduleGroupId = action . payload . workScheduleGroupId ;
156
+ state . temporarySpeedLimitGroupId = action . payload . temporarySpeedLimitGroupId ;
157
+
158
+ // check that the arrival dates are in the search time window
159
+ const origin = state . stdcmPathSteps . at ( 0 ) as Extract < StdcmPathStep , { isVia : false } > ;
160
+ const destination = state . stdcmPathSteps . at ( - 1 ) as Extract < StdcmPathStep , { isVia : false } > ;
161
+ let newOrigin = origin ;
162
+ let newDestination = destination ;
163
+
164
+ if ( searchDatetimeWindow ) {
165
+ if (
166
+ ! origin . arrival ||
167
+ ! isArrivalDateInSearchTimeWindow ( origin . arrival , searchDatetimeWindow )
168
+ ) {
169
+ newOrigin = { ...origin , arrival : searchDatetimeWindow . begin } ;
170
+ }
171
+ if (
172
+ ! destination . arrival ||
173
+ ! isArrivalDateInSearchTimeWindow ( destination . arrival , searchDatetimeWindow )
174
+ ) {
175
+ newDestination = { ...destination , arrival : searchDatetimeWindow . begin } ;
176
+ }
158
177
}
178
+
179
+ state . stdcmPathSteps = [ newOrigin , ...state . stdcmPathSteps . slice ( 1 , - 1 ) , newDestination ] ;
159
180
} ,
160
181
updateStdcmPathSteps (
161
182
state : Draft < OsrdStdcmConfState > ,
0 commit comments