@@ -20,6 +20,7 @@ import {
20
20
useUpdateMigrationWaveMutation ,
21
21
} from "@app/queries/migration-waves" ;
22
22
import dayjs from "dayjs" ;
23
+
23
24
import {
24
25
Stakeholder ,
25
26
StakeholderGroup ,
@@ -268,10 +269,16 @@ export const WaveForm: React.FC<WaveFormProps> = ({
268
269
} ;
269
270
270
271
const endDateRangeValidator = ( date : Date ) => {
271
- const sDate = startDate || new Date ( ) ;
272
- if ( sDate >= date ) {
273
- return "Date is before allowable range." ;
272
+ const selectedEndDate = dayjs ( date ) ;
273
+ const selectedStartDate = startDate ? dayjs ( startDate ) : null ;
274
+
275
+ if (
276
+ ! selectedStartDate ||
277
+ selectedEndDate . isSameOrBefore ( selectedStartDate , "day" )
278
+ ) {
279
+ return "End date must be at least one day after the start date." ;
274
280
}
281
+
275
282
return "" ;
276
283
} ;
277
284
@@ -281,7 +288,8 @@ export const WaveForm: React.FC<WaveFormProps> = ({
281
288
const stakeholderGroupsToRefs = ( names : string [ ] | undefined | null ) =>
282
289
matchItemsToRefs ( stakeholderGroups , ( i ) => i . name , names ) ;
283
290
284
- const dateRef = useRef < HTMLDivElement | null > ( null ) ;
291
+ const startDateRef = useRef < HTMLDivElement | null > ( null ) ;
292
+ const endDateRef = useRef < HTMLDivElement | null > ( null ) ;
285
293
286
294
return (
287
295
< Form onSubmit = { handleSubmit ( onSubmit ) } >
@@ -295,7 +303,7 @@ export const WaveForm: React.FC<WaveFormProps> = ({
295
303
/>
296
304
</ GridItem >
297
305
< GridItem span = { 3 } >
298
- < div ref = { dateRef } >
306
+ < div ref = { startDateRef } >
299
307
< HookFormPFGroupController
300
308
control = { control }
301
309
name = "startDateStr"
@@ -307,14 +315,14 @@ export const WaveForm: React.FC<WaveFormProps> = ({
307
315
aria-label = { name }
308
316
onChange = { ( e , val ) => {
309
317
onChange ( val ) ;
310
- trigger ( "endDateStr" ) ; // Validation of endDateStr depends on startDateStr
318
+ trigger ( "endDateStr" ) ;
311
319
} }
312
320
placeholder = "MM/DD/YYYY"
313
321
value = { value }
314
322
dateFormat = { ( val ) => dayjs ( val ) . format ( "MM/DD/YYYY" ) }
315
323
dateParse = { ( val ) => dayjs ( val ) . toDate ( ) }
316
324
validators = { [ startDateRangeValidator ] }
317
- appendTo = { ( ) => dateRef . current || document . body }
325
+ appendTo = { ( ) => startDateRef . current || document . body }
318
326
/>
319
327
) }
320
328
/>
@@ -330,30 +338,31 @@ export const WaveForm: React.FC<WaveFormProps> = ({
330
338
>
331
339
to
332
340
</ GridItem >
333
-
334
341
< GridItem span = { 8 } >
335
- < HookFormPFGroupController
336
- control = { control }
337
- name = "endDateStr"
338
- label = "Potential End Date"
339
- fieldId = "endDateStr"
340
- isRequired
341
- renderInput = { ( { field : { value, name, onChange } } ) => (
342
- < DatePicker
343
- aria-label = { name }
344
- onChange = { ( e , val ) => {
345
- onChange ( val ) ;
346
- } }
347
- placeholder = "MM/DD/YYYY"
348
- value = { value }
349
- dateFormat = { ( val ) => dayjs ( val ) . format ( "MM/DD/YYYY" ) }
350
- dateParse = { ( val ) => dayjs ( val ) . toDate ( ) }
351
- validators = { [ endDateRangeValidator ] }
352
- appendTo = { ( ) => document . body }
353
- />
354
- ) }
355
- />
342
+ < div ref = { endDateRef } >
343
+ < HookFormPFGroupController
344
+ control = { control }
345
+ name = "endDateStr"
346
+ label = "Potential End Date"
347
+ fieldId = "endDateStr"
348
+ isRequired
349
+ renderInput = { ( { field : { value, name, onChange } } ) => (
350
+ < DatePicker
351
+ isDisabled = { ! startDate }
352
+ aria-label = { name }
353
+ onChange = { ( e , val ) => onChange ( val ) }
354
+ placeholder = "MM/DD/YYYY"
355
+ value = { value && startDate ? value : "" }
356
+ dateFormat = { ( val ) => dayjs ( val ) . format ( "MM/DD/YYYY" ) }
357
+ dateParse = { ( val ) => dayjs ( val ) . toDate ( ) }
358
+ validators = { [ endDateRangeValidator ] }
359
+ appendTo = { ( ) => endDateRef . current || document . body }
360
+ />
361
+ ) }
362
+ />
363
+ </ div >
356
364
</ GridItem >
365
+
357
366
< GridItem span = { 12 } >
358
367
< HookFormPFGroupController
359
368
control = { control }
0 commit comments