@@ -19,20 +19,20 @@ class TimelineTemplateField extends Component {
19
19
}
20
20
21
21
componentDidMount ( ) {
22
- const { challengeTimelines, timelineTemplates, challenge } = this . props
23
- this . checkData ( challengeTimelines , timelineTemplates , challenge )
22
+ const { challengeTimelines, timelineTemplates, challenge, currentTemplate } = this . props
23
+ this . checkData ( challengeTimelines , timelineTemplates , challenge , currentTemplate )
24
24
}
25
25
26
26
componentWillUnmount ( ) {
27
- this . props . onUpdateSelect ( this . state . selectedOption . value , false , 'timelineTemplateId' )
27
+ this . props . onUpdateSelect ( this . state . selectedOption . value )
28
28
}
29
29
30
30
loadSelectedOption ( validOptions , value ) {
31
- // if (!value) return
32
31
const { timelineTemplates, challenge } = this . props
33
32
const selectedOption = { }
34
33
const selectedTemplate = _ . find ( timelineTemplates , t => t . id === ( value ) )
35
- // if (!selectedTemplate) return
34
+ this . props . onUpdateSelect ( selectedTemplate )
35
+
36
36
selectedOption . label = selectedTemplate . name
37
37
selectedOption . value = selectedTemplate . id
38
38
this . setState ( {
@@ -42,16 +42,16 @@ class TimelineTemplateField extends Component {
42
42
} )
43
43
}
44
44
45
- checkData ( challengeTimelines , timelineTemplates , challenge ) {
45
+ checkData ( challengeTimelines , timelineTemplates , challenge , currentTemplate ) {
46
46
const availableTemplates = _ . filter ( challengeTimelines , ct => ct . typeId === challenge . typeId && ct . trackId === challenge . trackId )
47
47
const availableTemplateIds = availableTemplates . map ( tt => tt . timelineTemplateId )
48
48
const validOptions = _ . filter ( timelineTemplates , t => _ . includes ( availableTemplateIds , t . id ) )
49
49
const defaultValue = _ . get ( _ . find ( availableTemplates , t => t . isDefault ) , 'timelineTemplateId' )
50
- if ( challenge . timelineTemplateId ) {
51
- if ( ! _ . includes ( _ . map ( validOptions , o => o . id ) , challenge . timelineTemplateId ) ) {
50
+ if ( currentTemplate && currentTemplate . id ) {
51
+ if ( ! _ . includes ( _ . map ( validOptions , o => o . id ) , currentTemplate . id ) ) {
52
52
this . loadSelectedOption ( validOptions , defaultValue )
53
53
} else {
54
- this . loadSelectedOption ( validOptions , challenge . timelineTemplateId )
54
+ this . loadSelectedOption ( validOptions , currentTemplate . id )
55
55
}
56
56
} else if ( defaultValue ) {
57
57
return this . loadSelectedOption ( validOptions , defaultValue )
@@ -61,7 +61,7 @@ class TimelineTemplateField extends Component {
61
61
getErrorMessage ( ) {
62
62
if ( ! this . props . challenge . typeId || ! this . props . challenge . trackId ) {
63
63
return 'Please select a work type and format to enable this field'
64
- } else if ( this . props . challenge . submitTriggered && ! this . props . challenge . timelineTemplateId ) {
64
+ } else if ( this . props . challenge . submitTriggered && ! this . props . currentTemplate ) {
65
65
return 'Timeline template is required field'
66
66
} else if ( this . state . validOptions . length === 0 ) {
67
67
return 'Sorry, there are no available timeline templates for the options you have selected'
@@ -70,10 +70,10 @@ class TimelineTemplateField extends Component {
70
70
}
71
71
72
72
render ( ) {
73
- const { challengeTimelines, timelineTemplates, challenge } = this . props
73
+ const { challengeTimelines, timelineTemplates, challenge, currentTemplate } = this . props
74
74
const hasSelectedTypeAndTrack = ! _ . isEmpty ( challenge . typeId ) && ! _ . isEmpty ( challenge . trackId )
75
75
if ( ( hasSelectedTypeAndTrack && this . state . validOptions . length === 0 ) || this . state . matchString !== `${ challenge . typeId } -${ challenge . trackId } -${ this . state . selectedOption . value } ` ) {
76
- this . checkData ( challengeTimelines , timelineTemplates , challenge )
76
+ this . checkData ( challengeTimelines , timelineTemplates , challenge , currentTemplate )
77
77
}
78
78
const error = this . getErrorMessage ( )
79
79
return (
@@ -90,7 +90,6 @@ class TimelineTemplateField extends Component {
90
90
placeholder = 'Timeline Template'
91
91
isClearable = { false }
92
92
onChange = { ( e ) => {
93
- this . props . onUpdateSelect ( e . value , false , 'timelineTemplateId' )
94
93
this . loadSelectedOption ( this . state . validOptions , e . value )
95
94
} }
96
95
isDisabled = { this . state . validOptions . length === 0 || this . props . readOnly }
@@ -111,15 +110,17 @@ class TimelineTemplateField extends Component {
111
110
TimelineTemplateField . defaultProps = {
112
111
challengeTimelines : [ ] ,
113
112
timelineTemplates : [ ] ,
114
- readOnly : false
113
+ readOnly : false ,
114
+ currentTemplate : null
115
115
}
116
116
117
117
TimelineTemplateField . propTypes = {
118
118
challengeTimelines : PropTypes . arrayOf ( PropTypes . shape ( ) ) . isRequired ,
119
119
timelineTemplates : PropTypes . arrayOf ( PropTypes . shape ( ) ) . isRequired ,
120
120
challenge : PropTypes . shape ( ) . isRequired ,
121
121
onUpdateSelect : PropTypes . func . isRequired ,
122
- readOnly : PropTypes . bool
122
+ readOnly : PropTypes . bool ,
123
+ currentTemplate : PropTypes . shape ( )
123
124
}
124
125
125
126
export default TimelineTemplateField
0 commit comments