@@ -14,6 +14,7 @@ import ConsentComponent from 'components/Settings/ConsentComponent';
14
14
import { PrimaryButton } from 'topcoder-react-ui-kit' ;
15
15
import DatePicker from 'components/challenge-listing/Filters/DatePicker' ;
16
16
import ErrorMessage from 'components/Settings/ErrorMessage' ;
17
+ import { validateStartDate , validateEndDate } from 'utils/settings' ;
17
18
import ConfirmationModal from '../../CofirmationModal' ;
18
19
import EducationList from './List' ;
19
20
@@ -39,6 +40,10 @@ export default class Education extends ConsentComponent {
39
40
const { userTraits } = props ;
40
41
this . state = {
41
42
formInvalid : false ,
43
+ startDateInvalid : false ,
44
+ startDateInvalidMsg : '' ,
45
+ endDateInvalid : false ,
46
+ endDateInvalidMsg : '' ,
42
47
educationTrait : this . loadEducationTrait ( userTraits ) ,
43
48
personalizationTrait : this . loadPersonalizationTrait ( userTraits ) ,
44
49
newEducation : {
@@ -48,7 +53,7 @@ export default class Education extends ConsentComponent {
48
53
timePeriodTo : '' ,
49
54
graduated : false ,
50
55
} ,
51
- inputChanged : false ,
56
+ isSubmit : false ,
52
57
isMobileView : false ,
53
58
screenSM : 767 ,
54
59
isEdit : false ,
@@ -69,7 +74,11 @@ export default class Education extends ConsentComponent {
69
74
educationTrait,
70
75
personalizationTrait,
71
76
formInvalid : false ,
72
- inputChanged : false ,
77
+ startDateInvalid : false ,
78
+ startDateInvalidMsg : '' ,
79
+ endDateInvalid : false ,
80
+ endDateInvalidMsg : '' ,
81
+ isSubmit : false ,
73
82
newEducation : {
74
83
schoolCollegeName : '' ,
75
84
major : '' ,
@@ -91,30 +100,24 @@ export default class Education extends ConsentComponent {
91
100
*/
92
101
onCheckFormValue ( newEducation ) {
93
102
let invalid = false ;
94
- const currentDate = new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ;
95
103
96
104
if ( ! _ . trim ( newEducation . schoolCollegeName ) . length ) {
97
105
invalid = true ;
98
106
}
99
107
100
- if ( ! _ . isEmpty ( newEducation . timePeriodFrom ) && _ . isEmpty ( newEducation . timePeriodTo ) ) {
101
- const fromDate = new Date ( newEducation . timePeriodFrom ) . setHours ( 0 , 0 , 0 , 0 ) ;
102
-
103
- if ( fromDate > currentDate ) {
104
- invalid = true ; // Start Date should be in past or current
105
- }
106
- } else if ( ! _ . isEmpty ( newEducation . timePeriodFrom ) && ! _ . isEmpty ( newEducation . timePeriodTo ) ) {
107
- const fromDate = new Date ( newEducation . timePeriodFrom ) . setHours ( 0 , 0 , 0 , 0 ) ;
108
- const toDate = new Date ( newEducation . timePeriodTo ) . setHours ( 0 , 0 , 0 , 0 ) ;
109
-
110
- if ( fromDate > currentDate // Start Date is in past or current
111
- || fromDate >= toDate // Start Date is before End Date
112
- || ( newEducation . graduated && toDate > currentDate ) ) { // End Date is in past or current
113
- invalid = true ;
114
- }
115
- }
108
+ const fromDateValidResult = validateStartDate ( newEducation . graduated ,
109
+ newEducation . timePeriodFrom , newEducation . timePeriodTo ) ;
110
+ const endDateValidResult = validateEndDate ( newEducation . graduated ,
111
+ newEducation . timePeriodFrom , newEducation . timePeriodTo ) ;
112
+ const formInvalid = invalid || fromDateValidResult . invalid || endDateValidResult . invalid ;
116
113
117
- this . setState ( { formInvalid : invalid } ) ;
114
+ this . setState ( {
115
+ formInvalid,
116
+ startDateInvalid : fromDateValidResult . invalid ,
117
+ startDateInvalidMsg : fromDateValidResult . message ,
118
+ endDateInvalidMsg : endDateValidResult . message ,
119
+ endDateInvalid : endDateValidResult . invalid ,
120
+ } ) ;
118
121
return invalid ;
119
122
}
120
123
@@ -167,8 +170,7 @@ export default class Education extends ConsentComponent {
167
170
result . message = 'End Date should be in past or current' ;
168
171
}
169
172
}
170
-
171
- return result ;
173
+ return formInvalid ;
172
174
}
173
175
174
176
onHandleDeleteEducation ( indexNo ) {
@@ -183,7 +185,7 @@ export default class Education extends ConsentComponent {
183
185
const { newEducation : oldEducation } = this . state ;
184
186
const newEducation = { ...oldEducation } ;
185
187
newEducation [ timePeriod ] = date ;
186
- this . setState ( { newEducation, inputChanged : true } ) ;
188
+ this . setState ( { newEducation, isSubmit : false } ) ;
187
189
}
188
190
}
189
191
@@ -215,8 +217,13 @@ export default class Education extends ConsentComponent {
215
217
this . setState ( {
216
218
showConfirmation : false ,
217
219
indexNo : null ,
218
- inputChanged : false ,
220
+ isSubmit : false ,
219
221
isEdit : false ,
222
+ formInvalid : false ,
223
+ startDateInvalid : false ,
224
+ startDateInvalidMsg : '' ,
225
+ endDateInvalid : false ,
226
+ endDateInvalidMsg : '' ,
220
227
} ) ;
221
228
}
222
229
@@ -234,8 +241,14 @@ export default class Education extends ConsentComponent {
234
241
timePeriodTo : _ . isEmpty ( educationTrait . traits . data [ indexNo ] . timePeriodTo ) ? '' : educationTrait . traits . data [ indexNo ] . timePeriodTo ,
235
242
graduated : educationTrait . traits . data [ indexNo ] . graduated ,
236
243
} ,
244
+ isSubmit : false ,
237
245
isEdit : true ,
238
246
indexNo,
247
+ formInvalid : false ,
248
+ startDateInvalid : false ,
249
+ startDateInvalidMsg : '' ,
250
+ endDateInvalid : false ,
251
+ endDateInvalidMsg : '' ,
239
252
} ) ;
240
253
}
241
254
@@ -299,7 +312,7 @@ export default class Education extends ConsentComponent {
299
312
newEducation : empty ,
300
313
isEdit : false ,
301
314
indexNo : null ,
302
- inputChanged : false ,
315
+ isSubmit : false ,
303
316
} ) ;
304
317
// save personalization
305
318
if ( _ . isEmpty ( personalizationTrait ) ) {
@@ -334,7 +347,7 @@ export default class Education extends ConsentComponent {
334
347
}
335
348
}
336
349
337
- this . setState ( { newEducation, inputChanged : true } ) ;
350
+ this . setState ( { newEducation, isSubmit : false } ) ;
338
351
}
339
352
340
353
/**
@@ -346,7 +359,7 @@ export default class Education extends ConsentComponent {
346
359
const { newEducation : oldEducation } = this . state ;
347
360
const newEducation = { ...oldEducation } ;
348
361
newEducation [ option . key ] = option . name ;
349
- this . setState ( { newEducation, inputChanged : true } ) ;
362
+ this . setState ( { newEducation, isSubmit : false } ) ;
350
363
}
351
364
}
352
365
@@ -357,7 +370,7 @@ export default class Education extends ConsentComponent {
357
370
onHandleAddEducation ( e ) {
358
371
e . preventDefault ( ) ;
359
372
const { newEducation } = this . state ;
360
- this . setState ( { inputChanged : true } ) ;
373
+ this . setState ( { isSubmit : true } ) ;
361
374
if ( this . onCheckFormValue ( newEducation ) ) {
362
375
return ;
363
376
}
@@ -394,7 +407,7 @@ export default class Education extends ConsentComponent {
394
407
if ( isEdit ) {
395
408
this . setState ( {
396
409
isEdit : false ,
397
- inputChanged : false ,
410
+ isSubmit : false ,
398
411
indexNo : null ,
399
412
newEducation : {
400
413
schoolCollegeName : '' ,
@@ -403,6 +416,11 @@ export default class Education extends ConsentComponent {
403
416
timePeriodTo : '' ,
404
417
graduated : false ,
405
418
} ,
419
+ formInvalid : false ,
420
+ startDateInvalid : false ,
421
+ startDateInvalidMsg : '' ,
422
+ endDateInvalid : false ,
423
+ endDateInvalidMsg : '' ,
406
424
} ) ;
407
425
}
408
426
}
@@ -417,13 +435,19 @@ export default class Education extends ConsentComponent {
417
435
isEdit,
418
436
showConfirmation,
419
437
indexNo,
438
+ formInvalid,
439
+ startDateInvalid,
440
+ startDateInvalidMsg,
441
+ endDateInvalid,
442
+ endDateInvalidMsg,
443
+ isSubmit,
420
444
} = this . state ;
421
445
const tabs = settingsUI . TABS . PROFILE ;
422
446
const currentTab = settingsUI . currentProfileTab ;
423
447
const containerStyle = currentTab === tabs . EDUCATION ? '' : 'hide' ;
424
448
const educationItems = educationTrait . traits
425
449
? educationTrait . traits . data . slice ( ) : [ ] ;
426
- const { newEducation, inputChanged } = this . state ;
450
+ const { newEducation } = this . state ;
427
451
428
452
429
453
return (
@@ -478,7 +502,11 @@ export default class Education extends ConsentComponent {
478
502
< div styleName = "field col-2" >
479
503
< span styleName = "text-required" > * Required</ span >
480
504
< input id = "schoolCollegeName" name = "schoolCollegeName" type = "text" placeholder = "Name" onChange = { this . onUpdateInput } value = { newEducation . schoolCollegeName } maxLength = "64" required />
481
- < ErrorMessage invalid = { _ . isEmpty ( newEducation . schoolCollegeName ) && inputChanged } message = "Name cannot be empty" />
505
+ {
506
+ isSubmit && formInvalid && (
507
+ < ErrorMessage invalid = { _ . isEmpty ( newEducation . schoolCollegeName ) } message = "Name cannot be empty" />
508
+ )
509
+ }
482
510
</ div >
483
511
</ div >
484
512
< div styleName = "row" >
@@ -509,10 +537,14 @@ export default class Education extends ConsentComponent {
509
537
onDateChange = { date => this . onUpdateDate ( date , 'timePeriodFrom' ) }
510
538
placeholder = "dd/mm/yyyy"
511
539
/>
512
- < ErrorMessage
513
- invalid = { this . onCheckStartDate ( newEducation ) . invalid && inputChanged }
514
- message = { this . onCheckStartDate ( newEducation ) . message }
515
- />
540
+ {
541
+ isSubmit && (
542
+ < ErrorMessage
543
+ invalid = { startDateInvalid }
544
+ message = { startDateInvalidMsg }
545
+ />
546
+ )
547
+ }
516
548
</ div >
517
549
</ div >
518
550
< div styleName = "row" >
@@ -546,10 +578,14 @@ export default class Education extends ConsentComponent {
546
578
/>
547
579
)
548
580
}
549
- < ErrorMessage
550
- invalid = { this . onCheckEndDate ( newEducation ) . invalid && inputChanged }
551
- message = { this . onCheckEndDate ( newEducation ) . message }
552
- />
581
+ {
582
+ isSubmit && (
583
+ < ErrorMessage
584
+ invalid = { endDateInvalid }
585
+ message = { endDateInvalidMsg }
586
+ />
587
+ )
588
+ }
553
589
</ div >
554
590
</ div >
555
591
< div styleName = "row" >
@@ -622,7 +658,11 @@ export default class Education extends ConsentComponent {
622
658
< input type = "hidden" />
623
659
</ label >
624
660
< input id = "schoolCollegeName" name = "schoolCollegeName" type = "text" placeholder = "Name" onChange = { this . onUpdateInput } value = { newEducation . schoolCollegeName } maxLength = "64" required />
625
- < ErrorMessage invalid = { _ . isEmpty ( newEducation . schoolCollegeName ) && inputChanged } message = "Name cannot be empty" />
661
+ {
662
+ isSubmit && formInvalid && (
663
+ < ErrorMessage invalid = { _ . isEmpty ( newEducation . schoolCollegeName ) } message = "Name cannot be empty" />
664
+ )
665
+ }
626
666
</ div >
627
667
< div styleName = "field col-3" >
628
668
< label htmlFor = "major" >
@@ -652,10 +692,14 @@ export default class Education extends ConsentComponent {
652
692
onDateChange = { date => this . onUpdateDate ( date , 'timePeriodFrom' ) }
653
693
placeholder = "dd/mm/yyyy"
654
694
/>
655
- < ErrorMessage
656
- invalid = { this . onCheckStartDate ( newEducation ) . invalid && inputChanged }
657
- message = { this . onCheckStartDate ( newEducation ) . message }
658
- />
695
+ {
696
+ isSubmit && (
697
+ < ErrorMessage
698
+ invalid = { startDateInvalid }
699
+ message = { startDateInvalidMsg }
700
+ />
701
+ )
702
+ }
659
703
</ div >
660
704
< div styleName = "field col-date" >
661
705
< label htmlFor = "timePeriodTo" >
@@ -685,10 +729,14 @@ export default class Education extends ConsentComponent {
685
729
/>
686
730
)
687
731
}
688
- < ErrorMessage
689
- invalid = { this . onCheckEndDate ( newEducation ) . invalid && inputChanged }
690
- message = { this . onCheckEndDate ( newEducation ) . message }
691
- />
732
+ {
733
+ isSubmit && (
734
+ < ErrorMessage
735
+ invalid = { endDateInvalid }
736
+ message = { endDateInvalidMsg }
737
+ />
738
+ )
739
+ }
692
740
</ div >
693
741
< div styleName = "field col-checkbox" >
694
742
< div styleName = "tc-checkbox" >
0 commit comments