@@ -23,9 +23,9 @@ import {
23
23
} from 'common/IntervalsDataViz/data' ;
24
24
import { LinearMetadataItem } from 'common/IntervalsDataViz/types' ;
25
25
import { LinearMetadataDataviz } from 'common/IntervalsDataViz/dataviz' ;
26
- import { useModal } from '../../../../common/BootstrapSNCF/ModalSNCF' ;
26
+ import { useModal } from 'common/BootstrapSNCF/ModalSNCF' ;
27
+ import { tooltipPosition , notEmpty } from 'common/IntervalsDataViz/utils' ;
27
28
import HelpModal from './HelpModal' ;
28
- import { tooltipPosition , notEmpty } from '../../../../common/IntervalsDataViz/utils' ;
29
29
30
30
import { LinearMetadataTooltip } from './tooltip' ;
31
31
import { FormBeginEndWidget } from './FormBeginEndWidget' ;
@@ -66,12 +66,19 @@ export const FormComponent: React.FC<FieldProps> = (props) => {
66
66
return 0 ;
67
67
} , [ formContext ] ) ;
68
68
69
+ // Remove the 'valueField' required field because it is required by the backend. However,
70
+ // the segment with missing values is filtered in 'customOnChange' before being sent to the backend,
71
+ // and then re-added by 'fixLinearMetadataItems'.
72
+ const requiredFilter = ( requireds : string [ ] ) =>
73
+ requireds . filter ( ( r ) => [ 'end' , 'begin' ] . includes ( r ) ) ;
74
+
69
75
// Compute the JSON schema of the linear metadata item
70
76
const jsonSchema = useMemo (
71
77
( ) =>
72
78
getFieldJsonSchema (
73
79
schema ,
74
80
registry . rootSchema ,
81
+ requiredFilter ,
75
82
distance
76
83
? {
77
84
begin : {
@@ -108,6 +115,7 @@ export const FormComponent: React.FC<FieldProps> = (props) => {
108
115
109
116
const customOnChange = useCallback (
110
117
( newData : Array < LinearMetadataItem > ) => {
118
+ // Remove item without value, it will be recreated by the fixLinearMetadataItems function
111
119
onChange ( newData . filter ( ( e ) => ( valueField ? ! isNil ( e [ valueField ] ) : true ) ) ) ;
112
120
} ,
113
121
[ onChange , valueField ]
@@ -337,7 +345,7 @@ export const FormComponent: React.FC<FieldProps> = (props) => {
337
345
noHtml5Validate
338
346
tagName = "div"
339
347
schema = {
340
- ( getFieldJsonSchema ( schema , registry . rootSchema , {
348
+ ( getFieldJsonSchema ( schema , registry . rootSchema , requiredFilter , {
341
349
begin : {
342
350
minimum : 0 ,
343
351
maximum : fnMax ( [ selectedData . begin , selectedData . end - SEGMENT_MIN_SIZE ] ) ,
@@ -363,43 +371,41 @@ export const FormComponent: React.FC<FieldProps> = (props) => {
363
371
} }
364
372
formData = { selectedData }
365
373
onChange = { ( e ) => {
366
- if ( e . errors . length === 0 ) {
367
- const newItem = e . formData ;
368
- const oldItem = data [ selected ] ;
369
- let newData = [ ...data ] ;
370
- // we keep the old value for begin and end
371
- // they will be change in the resize function if needed
372
- newData [ selected ] = {
373
- ...oldItem ,
374
- ...omit ( newItem , [ 'begin' , 'end' ] ) ,
375
- } ;
374
+ const newItem = e . formData ;
375
+ const oldItem = data [ selected ] ;
376
+ let newData = [ ...data ] ;
377
+ // we keep the old value for begin and end
378
+ // they will be change in the resize function if needed
379
+ newData [ selected ] = {
380
+ ...oldItem ,
381
+ ...omit ( newItem , [ 'begin' , 'end' ] ) ,
382
+ } ;
376
383
377
- // Check if there is a resize
378
- try {
379
- if ( newItem . begin !== oldItem . begin ) {
380
- const resizeBegin = resizeSegment (
381
- [ ...newData ] ,
382
- selected ,
383
- newItem . begin - oldItem . begin ,
384
- 'begin'
385
- ) ;
386
- newData = resizeBegin . result ;
387
- }
388
- if ( oldItem . end !== newItem . end ) {
389
- const resizeEnd = resizeSegment (
390
- [ ...newData ] ,
391
- selected ,
392
- newItem . end - oldItem . end ,
393
- 'end'
394
- ) ;
395
- newData = resizeEnd . result ;
396
- }
397
- customOnChange ( newData ) ;
398
- } catch ( error ) {
399
- // TODO: Should we display the resize error ?
400
- } finally {
401
- setSelectedData ( newItem ) ;
384
+ // Check if there is a resize
385
+ try {
386
+ if ( newItem . begin !== oldItem . begin ) {
387
+ const resizeBegin = resizeSegment (
388
+ [ ...newData ] ,
389
+ selected ,
390
+ newItem . begin - oldItem . begin ,
391
+ 'begin'
392
+ ) ;
393
+ newData = resizeBegin . result ;
394
+ }
395
+ if ( oldItem . end !== newItem . end ) {
396
+ const resizeEnd = resizeSegment (
397
+ [ ...newData ] ,
398
+ selected ,
399
+ newItem . end - oldItem . end ,
400
+ 'end'
401
+ ) ;
402
+ newData = resizeEnd . result ;
402
403
}
404
+ customOnChange ( newData ) ;
405
+ } catch ( error ) {
406
+ // TODO: Should we display the resize error ?
407
+ } finally {
408
+ setSelectedData ( newItem ) ;
403
409
}
404
410
} }
405
411
>
0 commit comments