@@ -24,13 +24,15 @@ import {
2424 ButtonVariantType ,
2525 ClusterCostModuleConfigPayload ,
2626 ClusterDetailListType ,
27+ ClusterProviderType ,
2728 DEFAULT_SECRET_PLACEHOLDER ,
2829 Icon ,
2930 ModalSidebarPanel ,
3031 ModuleNameMap ,
3132 ModuleStatus ,
3233 noop ,
3334 RemoteConnectionType ,
35+ SCHEMA_07_VALIDATOR ,
3436 SelectPickerOptionType ,
3537 showError ,
3638 ToastManager ,
@@ -83,6 +85,7 @@ const ClusterForm = ({
8385 category,
8486 clusterProvider,
8587 costModuleConfig,
88+ costModuleSchema,
8689} : ClusterFormProps ) => {
8790 const location = useLocation ( )
8891
@@ -93,12 +96,17 @@ const ClusterForm = ({
9396 )
9497
9598 const [ costModuleState , setCostModuleState ] = useState <
96- Pick < ClusterDetailListType [ 'costModuleConfig' ] , 'enabled' > & { config : string }
99+ Pick < ClusterDetailListType [ 'costModuleConfig' ] , 'enabled' > & {
100+ config : ClusterDetailListType [ 'costModuleConfig' ] [ 'config' ] & { provider : ClusterProviderType }
101+ }
97102 > ( {
98103 enabled : costModuleConfig ?. enabled || false ,
99- config : costModuleConfig ?. config ? JSON . stringify ( costModuleConfig . config ) : '' ,
104+ config : {
105+ ...costModuleConfig ?. config ,
106+ // Adding provider here to make the form work, it won't be sent to backend
107+ provider : clusterProvider ,
108+ } ,
100109 } )
101- const [ costModuleConfigErrorState , setCostModuleErrorState ] = useState < string > ( '' )
102110
103111 const [ isAppMetricsEnabled , setIsAppMetricsEnabled ] = useState ( ! ! prometheusUrl )
104112 const [ prometheusAuthenticationType , setPrometheusAuthenticationType ] = useState ( {
@@ -181,28 +189,20 @@ const ClusterForm = ({
181189 setIsConnectedViaSSHTunnelTemp ( false )
182190 }
183191
184- const validateCostModuleConfig = ( requiredConfig : string = costModuleState . config ) : string => {
185- try {
186- if ( requiredConfig ) {
187- JSON . parse ( requiredConfig )
188- }
189-
190- return ''
191- } catch ( e ) {
192- return e . message || 'Invalid JSON'
192+ const validateCostModuleConfig = ( ) : boolean => {
193+ if ( ! costModuleSchema || ! costModuleState . enabled ) {
194+ return true
193195 }
194- }
195196
196- const getParsedConfigValue = ( ) : ClusterCostModuleConfigPayload [ 'config' ] => {
197- if ( costModuleState . config ) {
198- try {
199- const parsedConfig = JSON . parse ( costModuleState . config )
200- return parsedConfig
201- } catch {
202- return { }
203- }
197+ try {
198+ const validationResult = SCHEMA_07_VALIDATOR . validateFormData (
199+ { ... costModuleState . config } ,
200+ costModuleSchema ,
201+ )
202+ return ! validationResult ?. errors ?. length
203+ } catch {
204+ return true
204205 }
205- return null
206206 }
207207
208208 const getCostModulePayload = ( ) : ClusterCostModuleConfigPayload | null => {
@@ -213,9 +213,12 @@ const ClusterForm = ({
213213 }
214214
215215 if ( costModuleState . config ) {
216+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
217+ const { provider, ...sanitizedConfig } = costModuleState . config
218+
216219 return {
217220 enabled : true ,
218- config : getParsedConfigValue ( ) ,
221+ config : sanitizedConfig ,
219222 }
220223 }
221224
@@ -250,18 +253,14 @@ const ClusterForm = ({
250253 } ,
251254 server_url : '' ,
252255 ...( getCategoryPayload ? getCategoryPayload ( selectedCategory ) : null ) ,
253- ...( clusterProvider ? { costModuleConfig : getCostModulePayload ( ) } : null ) ,
256+ ...( costModuleSchema ? { costModuleConfig : getCostModulePayload ( ) } : null ) ,
254257 } )
255258
256259 const additionalValidations = ( ) : boolean => {
257260 let hasError = false
258261
259- if ( costModuleState . enabled ) {
260- const costConfigError = validateCostModuleConfig ( )
261- if ( costConfigError ) {
262- setCostModuleErrorState ( costConfigError )
263- hasError = true
264- }
262+ if ( ! validateCostModuleConfig ( ) ) {
263+ hasError = true
265264 }
266265
267266 return hasError
@@ -512,14 +511,11 @@ const ClusterForm = ({
512511 } ) )
513512 }
514513
515- const handleCostConfigChange = ( newConfig : string ) => {
514+ const handleCostConfigChange = ( newConfig : typeof costModuleState . config ) => {
516515 setCostModuleState ( ( prev ) => ( {
517516 ...prev ,
518517 config : newConfig ,
519518 } ) )
520-
521- const error = validateCostModuleConfig ( newConfig )
522- setCostModuleErrorState ( error )
523519 }
524520
525521 const renderFooter = ( ) => (
@@ -612,10 +608,9 @@ const ClusterForm = ({
612608 costModuleEnabled = { costModuleState . enabled }
613609 toggleCostModule = { toggleCostModule }
614610 installationStatus = { costModuleConfig . installationStatus }
615- clusterProvider = { clusterProvider }
616611 handleCostConfigChange = { handleCostConfigChange }
617- config = { costModuleState . config || '' }
618- configError = { costModuleConfigErrorState }
612+ config = { costModuleState . config }
613+ costModuleSchema = { costModuleSchema || { } }
619614 />
620615 </ div >
621616 ) : null
0 commit comments