1
1
import React , { useEffect , useMemo , useState } from 'react' ;
2
2
import { useTranslation } from 'react-i18next' ;
3
3
import { useDispatch , useSelector } from 'react-redux' ;
4
- import { getAllowances , getPathfindingID } from 'reducers/osrdconf/selectors' ;
5
- import { StandardAllowance , osrdEditoastApi , Allowance } from 'common/api/osrdEditoastApi' ;
6
4
import { AiOutlineDash } from 'react-icons/ai' ;
5
+
6
+ import { StandardAllowance , osrdEditoastApi , Allowance } from 'common/api/osrdEditoastApi' ;
7
7
import { updateAllowances } from 'reducers/osrdconf' ;
8
- import cx from 'classnames ' ;
8
+ import { getAllowances , getPathfindingID } from 'reducers/osrdconf/selectors ' ;
9
9
import AllowancesStandardSettings from './AllowancesStandardSettings' ;
10
10
import AllowancesActions from './AllowancesActions' ;
11
11
import AllowancesList from './AllowancesList' ;
12
12
import {
13
13
AllowanceValueForm ,
14
14
AllowancesTypes ,
15
15
EngineeringAllowanceForm ,
16
- ManageAllowancesType ,
17
16
OverlapAllowancesIndexesType ,
17
+ RangeAllowanceForm ,
18
18
StandardAllowanceForm ,
19
19
} from './types' ;
20
- import AllowancesLinearView from './AllowancesLinearView' ;
21
20
import { initialStandardAllowance } from './consts' ;
22
- import getAllowanceValue from './helpers' ;
21
+ import getAllowanceValue , { fillAllowancesWithDefaultRanges } from './helpers' ;
22
+ import AllowancesLinearView from './AllowancesLinearView' ;
23
23
24
24
const MissingPathFindingMessage = ( ) => {
25
25
const { t } = useTranslation ( 'operationalStudies/allowances' ) ;
@@ -43,16 +43,26 @@ export default function Allowances() {
43
43
{ id : pathFindingID as number } ,
44
44
{ skip : ! pathFindingID }
45
45
) ;
46
- const pathLength = pathFinding ?. length ? Math . round ( pathFinding . length ) : 0 ;
46
+
47
+ const pathLength = pathFinding ?. length ? pathFinding . length : 0 ;
47
48
const allowances = useSelector ( getAllowances ) ;
48
- const [ collapsedStandardAllowanceRanges , setCollapsedStandardAllowanceRanges ] = useState ( true ) ;
49
- const [ standardAllowance , setStandardAllowance ] = useState (
50
- ( allowances &&
51
- ( allowances . find (
49
+ const [ standardAllowance , setStandardAllowance ] = useState ( ( ) => {
50
+ if ( allowances ) {
51
+ const tmpStandardAllowance = allowances . find (
52
52
( allowance ) => allowance . allowance_type === 'standard'
53
- ) as StandardAllowanceForm ) ) ||
54
- initialStandardAllowance
55
- ) ;
53
+ ) as StandardAllowanceForm ;
54
+ if ( tmpStandardAllowance )
55
+ return {
56
+ ...tmpStandardAllowance ,
57
+ ranges : fillAllowancesWithDefaultRanges (
58
+ tmpStandardAllowance . ranges ,
59
+ tmpStandardAllowance . default_value ,
60
+ pathLength
61
+ ) ,
62
+ } ;
63
+ }
64
+ return initialStandardAllowance ;
65
+ } ) ;
56
66
const [ engineeringAllowances , setEngineeringAllowances ] = useState (
57
67
( allowances &&
58
68
( allowances . filter (
@@ -69,6 +79,9 @@ export default function Allowances() {
69
79
const [ overlapAllowancesIndexes , setOverlapAllowancesIndexes ] =
70
80
useState < OverlapAllowancesIndexesType > ( [ false , false ] ) ;
71
81
82
+ const [ engineeringOverlapAllowancesIndexes , setEngineeringOverlapAllowancesIndexes ] =
83
+ useState < OverlapAllowancesIndexesType > ( [ false , false ] ) ;
84
+
72
85
const standardAllowanceValue = useMemo (
73
86
( ) => getAllowanceValue ( standardAllowance . default_value ) ,
74
87
[ standardAllowance ]
@@ -78,10 +91,6 @@ export default function Allowances() {
78
91
setStandardAllowance ( { ...standardAllowance , distribution } ) ;
79
92
} ;
80
93
81
- const setStandardValueAndUnit = ( valueAndUnit : AllowanceValueForm ) => {
82
- setStandardAllowance ( { ...standardAllowance , default_value : valueAndUnit } ) ;
83
- } ;
84
-
85
94
const toggleStandardAllowanceSelectedIndex = ( AllowanceIndex ?: number ) => {
86
95
setStandardAllowanceSelectedIndex (
87
96
AllowanceIndex !== standardAllowanceSelectedIndex ? AllowanceIndex : undefined
@@ -96,46 +105,38 @@ export default function Allowances() {
96
105
const resetFunction = ( type : AllowancesTypes ) => {
97
106
if ( type === AllowancesTypes . standard ) {
98
107
setStandardAllowance ( initialStandardAllowance ) ;
108
+ setStandardAllowanceSelectedIndex ( undefined ) ;
99
109
}
100
110
if ( type === AllowancesTypes . standard ) {
101
111
setEngineeringAllowances ( [ ] ) ;
112
+ setEngineeringAllowanceSelectedIndex ( undefined ) ;
102
113
}
103
114
} ;
104
115
105
- // This function manage "add" and "delete" allowance, "update" is "delete" followed by "add"
106
- const manageAllowance = ( {
107
- type,
108
- newAllowance,
109
- allowanceIndexToDelete,
110
- } : ManageAllowancesType ) => {
111
- if ( type === AllowancesTypes . standard ) {
112
- const newRanges =
113
- allowanceIndexToDelete !== undefined
114
- ? standardAllowance . ranges . filter ( ( _ , idx ) => allowanceIndexToDelete !== idx )
115
- : [ ...standardAllowance . ranges ] ;
116
- setStandardAllowance ( {
117
- ...standardAllowance ,
118
- ranges : ( newAllowance ? [ ...newRanges , newAllowance ] : newRanges ) . sort (
119
- ( a , b ) => a . begin_position - b . begin_position
120
- ) ,
121
- } ) ;
122
- setStandardAllowanceSelectedIndex ( undefined ) ;
123
- }
124
- if ( type === AllowancesTypes . engineering ) {
125
- const newEngineeringAllowances =
126
- allowanceIndexToDelete !== undefined
127
- ? engineeringAllowances . filter ( ( _ , index ) => index !== allowanceIndexToDelete )
128
- : [ ...engineeringAllowances ] ;
129
- setEngineeringAllowances (
130
- ( newAllowance
131
- ? ( [ ...newEngineeringAllowances , newAllowance ] as EngineeringAllowanceForm [ ] )
132
- : newEngineeringAllowances
133
- ) . sort ( ( a , b ) => a . begin_position - b . begin_position )
134
- ) ;
135
- setEngineeringAllowanceSelectedIndex ( undefined ) ;
136
- }
116
+ const updateStandardAllowanceDefaultValue = ( newDefaultValue : AllowanceValueForm ) => {
117
+ setStandardAllowance ( { ...standardAllowance , default_value : newDefaultValue } ) ;
137
118
} ;
138
119
120
+ const updateStandardAllowances = ( newStandardAllowanceRanges : RangeAllowanceForm [ ] ) => {
121
+ setStandardAllowance ( {
122
+ ...standardAllowance ,
123
+ ranges : newStandardAllowanceRanges ,
124
+ } ) ;
125
+ setStandardAllowanceSelectedIndex ( undefined ) ;
126
+ } ;
127
+
128
+ const updateEngineeringAllowances = ( newStandardAllowanceRanges : EngineeringAllowanceForm [ ] ) => {
129
+ setEngineeringAllowances ( newStandardAllowanceRanges ) ;
130
+ setEngineeringAllowanceSelectedIndex ( undefined ) ;
131
+ } ;
132
+
133
+ useEffect ( ( ) => {
134
+ const newRanges = standardAllowance . ranges . map ( ( allowance ) =>
135
+ allowance . isDefault ? { ...allowance , value : standardAllowance . default_value } : allowance
136
+ ) ;
137
+ setStandardAllowance ( { ...standardAllowance , ranges : newRanges } ) ;
138
+ } , [ standardAllowance . default_value ] ) ;
139
+
139
140
// dispatch only the valid allowances in the store
140
141
useEffect ( ( ) => {
141
142
const standardAllowanceDefaultValue = getAllowanceValue ( standardAllowance . default_value ) ;
@@ -161,55 +162,40 @@ export default function Allowances() {
161
162
distribution = { standardAllowance . distribution }
162
163
valueAndUnit = { standardAllowance . default_value }
163
164
setDistribution = { setStandardDistribution }
164
- setValueAndUnit = { setStandardValueAndUnit }
165
+ updateStandardAllowanceDefaultValue = { updateStandardAllowanceDefaultValue }
165
166
/>
166
167
{ standardAllowanceValue !== undefined && standardAllowanceValue > 0 && (
167
168
< >
168
- < button
169
- className = "subtitle mb-1 mt-2"
170
- type = "button"
171
- onClick = { ( ) =>
172
- setCollapsedStandardAllowanceRanges ( ! collapsedStandardAllowanceRanges )
173
- }
174
- >
169
+ < div className = "subtitle mb-2 mt-2" >
175
170
< AiOutlineDash />
176
171
< span className = "ml-1" > { t ( 'standardAllowanceIntervals' ) } </ span >
177
- < span className = { cx ( 'ml-auto' , standardAllowance . ranges . length > 0 && 'd-none' ) } >
178
- { collapsedStandardAllowanceRanges ? (
179
- < i className = "icons-arrow-down" />
180
- ) : (
181
- < i className = "icons-arrow-up" />
182
- ) }
183
- </ span >
184
- </ button >
185
- { ( ! collapsedStandardAllowanceRanges || standardAllowance . ranges . length > 0 ) && (
186
- < >
187
- < AllowancesActions
188
- allowances = { standardAllowance . ranges }
189
- pathLength = { pathLength }
190
- manageAllowance = { manageAllowance }
191
- type = { AllowancesTypes . standard }
192
- allowanceSelectedIndex = { standardAllowanceSelectedIndex }
193
- setAllowanceSelectedIndex = { setStandardAllowanceSelectedIndex }
194
- setOverlapAllowancesIndexes = { setOverlapAllowancesIndexes }
195
- pathFindingSteps = { pathFinding ?. steps }
196
- />
197
- < AllowancesLinearView
198
- allowances = { standardAllowance . ranges }
199
- pathLength = { pathLength }
200
- allowanceSelectedIndex = { standardAllowanceSelectedIndex }
201
- setAllowanceSelectedIndex = { toggleStandardAllowanceSelectedIndex }
202
- globalDistribution = { standardAllowance . distribution }
203
- />
204
- < AllowancesList
205
- allowances = { standardAllowance . ranges }
206
- type = { AllowancesTypes . standard }
207
- allowanceSelectedIndex = { standardAllowanceSelectedIndex }
208
- setAllowanceSelectedIndex = { toggleStandardAllowanceSelectedIndex }
209
- overlapAllowancesIndexes = { overlapAllowancesIndexes }
210
- />
211
- </ >
212
- ) }
172
+ </ div >
173
+ < AllowancesActions
174
+ allowances = { standardAllowance . ranges }
175
+ pathLength = { pathLength }
176
+ type = { AllowancesTypes . standard }
177
+ allowanceSelectedIndex = { standardAllowanceSelectedIndex }
178
+ setAllowanceSelectedIndex = { setStandardAllowanceSelectedIndex }
179
+ setOverlapAllowancesIndexes = { setOverlapAllowancesIndexes }
180
+ pathFindingSteps = { pathFinding ?. steps }
181
+ updateAllowances = { updateStandardAllowances }
182
+ defaultAllowance = { standardAllowance . default_value }
183
+ overlapAllowancesIndexes = { overlapAllowancesIndexes }
184
+ />
185
+ < AllowancesLinearView
186
+ allowances = { standardAllowance . ranges }
187
+ defaultAllowance = { standardAllowance . default_value }
188
+ pathLength = { pathLength }
189
+ allowanceSelectedIndex = { standardAllowanceSelectedIndex }
190
+ setAllowanceSelectedIndex = { toggleStandardAllowanceSelectedIndex }
191
+ globalDistribution = { standardAllowance . distribution }
192
+ />
193
+ < AllowancesList
194
+ allowances = { standardAllowance . ranges }
195
+ allowanceSelectedIndex = { standardAllowanceSelectedIndex }
196
+ setAllowanceSelectedIndex = { toggleStandardAllowanceSelectedIndex }
197
+ overlapAllowancesIndexes = { overlapAllowancesIndexes }
198
+ />
213
199
</ >
214
200
) }
215
201
</ div >
@@ -232,11 +218,13 @@ export default function Allowances() {
232
218
< AllowancesActions
233
219
allowances = { engineeringAllowances }
234
220
pathLength = { pathLength }
235
- manageAllowance = { manageAllowance }
221
+ updateAllowances = { updateEngineeringAllowances }
236
222
type = { AllowancesTypes . engineering }
237
223
allowanceSelectedIndex = { EngineeringAllowanceSelectedIndex }
238
224
setAllowanceSelectedIndex = { setEngineeringAllowanceSelectedIndex }
239
225
pathFindingSteps = { pathFinding ?. steps }
226
+ overlapAllowancesIndexes = { engineeringOverlapAllowancesIndexes }
227
+ setOverlapAllowancesIndexes = { setEngineeringOverlapAllowancesIndexes }
240
228
/>
241
229
{ /*
242
230
* Temporary desactivated until new version with overlap
@@ -250,9 +238,9 @@ export default function Allowances() {
250
238
*/ }
251
239
< AllowancesList
252
240
allowances = { engineeringAllowances }
253
- type = { AllowancesTypes . engineering }
254
241
allowanceSelectedIndex = { EngineeringAllowanceSelectedIndex }
255
242
setAllowanceSelectedIndex = { toggleEngineeringAllowanceSelectedIndex }
243
+ overlapAllowancesIndexes = { engineeringOverlapAllowancesIndexes }
256
244
/>
257
245
</ div >
258
246
</ >
0 commit comments