@@ -64,11 +64,13 @@ function DeploymentFormBody({
64
64
{ } as YAML . Document . Parsed < YAML . ParsedNode > ,
65
65
) ;
66
66
const [ restoreModalIsOpen , setRestoreModalOpen ] = useState ( false ) ;
67
- const [ isLoaded , setIsloaded ] = useState ( false ) ;
68
- const [ isLoading , setIsloading ] = useState ( true ) ;
67
+ const [ isLoaded , setIsLoaded ] = useState ( false ) ;
68
+ const [ isLoading , setIsLoading ] = useState ( true ) ;
69
69
const [ unsavedChangesMap ] = useState ( new Map < string , any > ( ) ) ;
70
+ const [ shouldSubmitForm , setShouldSubmitForm ] = useState ( false ) ;
70
71
71
- // setBasicFormParameters when basicFormParameters changes
72
+ // whenever the parsed values change (for instance, when a new pkg version is selected),
73
+ // we need to force a new extraction of the params from the schema
72
74
useEffect ( ( ) => {
73
75
if ( ! isLoaded && schemaFromTheAvailablePackage && ! isEmpty ( valuesFromTheParentContainerNodes ) ) {
74
76
const initialParamsFromContainer = retrieveBasicFormParams (
@@ -79,8 +81,8 @@ function DeploymentFormBody({
79
81
valuesFromTheDeployedPackageNodes ,
80
82
) ;
81
83
setParamsFromComponentState ( initialParamsFromContainer ) ;
82
- setIsloaded ( true ) ;
83
- setIsloading ( false ) ;
84
+ setIsLoaded ( true ) ;
85
+ setIsLoading ( false ) ;
84
86
}
85
87
} , [
86
88
deploymentEvent ,
@@ -92,42 +94,48 @@ function DeploymentFormBody({
92
94
valuesFromTheParentContainerNodes ,
93
95
] ) ;
94
96
95
- // setDefaultValues when defaultValues changes
97
+ // parse and store in the component state the values from the available package
96
98
useEffect ( ( ) => {
97
99
if ( valuesFromTheAvailablePackage ) {
98
100
setValuesFromTheAvailablePackageNodes ( parseToYamlNode ( valuesFromTheAvailablePackage ) ) ;
99
101
}
100
102
} , [ isLoaded , valuesFromTheAvailablePackage ] ) ;
101
103
104
+ // parse and store in the component state the current values (which come from the parent container)
102
105
useEffect ( ( ) => {
103
106
if ( valuesFromTheParentContainer ) {
104
107
setValuesFromTheParentContainerNodes ( parseToYamlNode ( valuesFromTheParentContainer ) ) ;
105
108
}
106
109
} , [ isLoaded , valuesFromTheParentContainer ] ) ;
107
110
111
+ // parse and store in the component state the values from the deployed package
108
112
useEffect ( ( ) => {
109
113
if ( valuesFromTheDeployedPackage ) {
110
114
setValuesFromTheDeployedPackageNodes ( parseToYamlNode ( valuesFromTheDeployedPackage ) ) ;
111
115
}
112
116
} , [ isLoaded , valuesFromTheDeployedPackage , valuesFromTheParentContainer ] ) ;
113
117
114
- const handleYAMLEditorChange = ( value : string ) => {
115
- setValuesFromTheParentContainer ( value ) ;
116
- setValuesModified ( ) ;
117
- } ;
118
-
119
- const forceSubmit = useCallback ( ( ) => {
120
- // the API was added recently, but should replace the manual dispatch of a submit event with bubbles:true (react>=17)
121
- formRef ?. current ?. requestSubmit ( ) ;
122
- } , [ formRef ] ) ;
118
+ // when the shouldSubmitForm flag is enabled, the form will be submitted, but using a native
119
+ // form submit event (to trigger the browser validations) instead of just calling its handler function
120
+ useEffect ( ( ) => {
121
+ if ( shouldSubmitForm ) {
122
+ // the requestSubmit API was added recently,
123
+ // but should replace the manual dispatch of a submit event with "bubbles:true" (react>=17)
124
+ formRef ?. current ?. requestSubmit ( ) ;
125
+ setShouldSubmitForm ( false ) ;
126
+ }
127
+ } , [ formRef , shouldSubmitForm ] ) ;
123
128
129
+ // for each unsaved change in the component state, we need to update the values,
130
+ // so that both the table and the yaml editor get the updated values
124
131
const saveAllChanges = ( ) => {
125
132
let newValuesFromTheParentContainer , newParamsFromComponentState ;
126
133
unsavedChangesMap . forEach ( ( value , key ) => {
127
- setIsloading ( true ) ;
134
+ setIsLoading ( true ) ;
128
135
setValuesModified ( ) ;
129
- const aa = updateCurrentConfigByKey ( paramsFromComponentState , key , value ) ;
130
- newParamsFromComponentState = [ ...aa ] ;
136
+ newParamsFromComponentState = [
137
+ ...updateCurrentConfigByKey ( paramsFromComponentState , key , value ) ,
138
+ ] ;
131
139
setParamsFromComponentState ( newParamsFromComponentState ) ;
132
140
133
141
newValuesFromTheParentContainer = toStringYamlNode (
@@ -136,13 +144,19 @@ function DeploymentFormBody({
136
144
setValuesFromTheParentContainer ( newValuesFromTheParentContainer ) ;
137
145
} ) ;
138
146
unsavedChangesMap . clear ( ) ;
139
- setIsloading ( false ) ;
147
+ setIsLoading ( false ) ;
140
148
} ;
141
149
142
- // save the pending changes and fire the submit event (via useEffect, to actually get the saved changes)
150
+ // save the pending changes and fire the submit event
151
+ // via an useEffect, to actually get the most recent saved changes
143
152
const handleDeployClick = ( ) => {
144
153
saveAllChanges ( ) ;
145
- forceSubmit ( ) ;
154
+ setShouldSubmitForm ( true ) ;
155
+ } ;
156
+
157
+ const handleYAMLEditorChange = ( value : string ) => {
158
+ setValuesFromTheParentContainer ( value ) ;
159
+ setValuesModified ( ) ;
146
160
} ;
147
161
148
162
// re-build the table based on the new YAML
0 commit comments