1
1
/* eslint-disable no-restricted-syntax */
2
2
import React , { useState , useEffect } from 'react' ;
3
+ import { FormSpy } from '@data-driven-forms/react-form-renderer' ;
3
4
import MiqFormRenderer , { useFormApi } from '@@ddf' ;
4
5
import {
5
6
Db2Database20 , BareMetalServer20 ,
6
7
} from '@carbon/icons-react' ;
7
- import createSchema from './settings-cu-collection-tab.schema.js' ;
8
- import miqRedirectBack from '../../helpers/miq-redirect-back.js' ;
8
+ import PropTypes from 'prop-types' ;
9
+ import { Button } from 'carbon-components-react' ;
10
+ import createSchema from './settings-cu-collection-tab.schema' ;
9
11
10
12
let idCounter = 0 ;
11
- let hostsOrDatastoresIds = new Set ( ) ;
13
+ let modified = false ;
12
14
13
15
const SettingsCUCollectionTab = ( {
14
16
url, fetchURL, clusterTree, datastoreTree, allClusters, allDatastores,
@@ -20,8 +22,25 @@ const SettingsCUCollectionTab = ({
20
22
datastoresNodes : [ ] ,
21
23
hostsCheckedWithId : [ ] ,
22
24
datastoresCheckedWithId : [ ] ,
25
+ callNumber : 0 ,
23
26
} ) ;
24
27
28
+ const customValidation = ( values ) => {
29
+ if ( values . tree_dropdown === undefined ) {
30
+ values . tree_dropdown = data . checked || [ ] ;
31
+ }
32
+ if ( values ) {
33
+ if ( JSON . stringify ( values . clusters_tree ) === JSON . stringify ( data . hostsCheckedWithId )
34
+ && JSON . stringify ( values . datastores_tree ) === JSON . stringify ( data . datastoresCheckedWithId )
35
+ && values . all_clusters === allClusters
36
+ && values . all_datastores === allDatastores ) {
37
+ modified = false ;
38
+ } else {
39
+ modified = true ;
40
+ }
41
+ }
42
+ } ;
43
+
25
44
const generateId = ( ) => idCounter ++ ;
26
45
27
46
const parseLabel = ( text ) => {
@@ -53,11 +72,11 @@ const SettingsCUCollectionTab = ({
53
72
let icon ;
54
73
switch ( node . icon ) {
55
74
case 'fa fa-database' :
56
- icon = Db2Database20 ? < Db2Database20 color = "black" /> : < span > Icon DATABSEnot available </ span > ;
75
+ icon = < Db2Database20 color = "black" /> ;
57
76
break ;
58
77
59
78
case 'pficon pficon-cluster' :
60
- icon = BareMetalServer20 ? < BareMetalServer20 color = "black" /> : < span > Icon SERvERASDSADASDSADSADASDa not available </ span > ;
79
+ icon = < BareMetalServer20 color = "black" /> ;
61
80
break ;
62
81
default :
63
82
break ;
@@ -76,23 +95,10 @@ const SettingsCUCollectionTab = ({
76
95
return nodeObject ;
77
96
} ;
78
97
79
- useEffect ( ( ) => {
80
- if ( data . isLoading ) {
81
- http . post ( url , data . tempData )
82
- . then ( ( ) => {
83
- setData ( {
84
- ...data ,
85
- isLoading : false ,
86
- } ) ;
87
- window . location . reload ( ) ;
88
- } )
89
- . catch ( ( error ) => console . log ( 'error: ' , error ) ) ;
90
- }
91
- } , [ data . isLoading ] ) ;
92
-
93
- useEffect ( ( ) => {
98
+ const getNodeValues = ( ) => {
94
99
let clustersNodes = [ ] ;
95
100
let datastoresNodes = [ ] ;
101
+ idCounter = 0 ;
96
102
if ( clusterTree ) {
97
103
const clustersBsTree = JSON . parse ( clusterTree . bs_tree ) ;
98
104
clustersNodes = clustersBsTree . map ( ( node ) => transformTree ( node , false , 0 ) ) ;
@@ -131,25 +137,46 @@ const SettingsCUCollectionTab = ({
131
137
132
138
for ( const node of datastoresNodes ) {
133
139
if ( node . children ) {
134
- if ( datastoresChecked . includes ( node . value . split ( '#' ) [ 0 ] ) ) {
140
+ const dsValue = node . value . split ( '#' ) [ 0 ] ;
141
+ const dsId = dsValue . split ( '-' ) [ 1 ] ;
142
+ const foundObject = datastores . find ( ( item ) => item . id === Number ( dsId ) ) ;
143
+ if ( datastoresChecked . includes ( dsValue ) && foundObject . capture === true ) {
135
144
for ( const host of node . children ) {
136
145
datastoresCheckedWithId . push ( host . value ) ;
137
146
}
138
147
}
139
- } else {
140
- if ( datastoresChecked . includes ( node . value . split ( '#' ) [ 0 ] ) ) {
141
- datastoresCheckedWithId . push ( node . value ) ;
142
- }
148
+ } else if ( datastoresChecked . includes ( node . value . split ( '#' ) [ 0 ] ) ) {
149
+ datastoresCheckedWithId . push ( node . value ) ;
143
150
}
144
151
}
152
+
145
153
setData ( {
146
154
...data ,
147
155
clustersNodes,
148
156
datastoresNodes,
149
157
hostsCheckedWithId,
150
158
datastoresCheckedWithId,
159
+ callNumber : data . callNumber + 1 ,
151
160
} ) ;
152
161
} ) ;
162
+ } ;
163
+
164
+ useEffect ( ( ) => {
165
+ if ( data . isLoading ) {
166
+ http . post ( url , data . tempData )
167
+ . then ( ( ) => {
168
+ setData ( {
169
+ ...data ,
170
+ isLoading : false ,
171
+ } ) ;
172
+ window . location . reload ( ) ;
173
+ } )
174
+ . catch ( ( error ) => console . log ( 'error: ' , error ) ) ;
175
+ }
176
+ } , [ data . isLoading ] ) ;
177
+
178
+ useEffect ( ( ) => {
179
+ getNodeValues ( ) ;
153
180
} , [ ] ) ;
154
181
155
182
const handleSubmit = ( values ) => {
@@ -201,36 +228,38 @@ const SettingsCUCollectionTab = ({
201
228
}
202
229
}
203
230
}
231
+ } else if ( clustersSplitValues . includes ( node . key ) ) {
232
+ params . clusters_checked . push ( { id : node . key , capture : true } ) ;
204
233
} else {
205
- if ( clustersSplitValues . includes ( node . key ) ) {
206
- params . clusters_checked . push ( { id : node . key , capture : true } ) ;
207
- } else {
208
- params . clusters_checked . push ( { id : node . key , capture : false } ) ;
209
- }
234
+ params . clusters_checked . push ( { id : node . key , capture : false } ) ;
210
235
}
211
236
}
212
237
213
- for ( const node of datastoreTree . tree_nodes ) {
238
+ for ( const node of data . datastoresNodes ) {
214
239
const curr = [ ] ;
215
- if ( node . nodes ) {
216
- for ( const hostNode of node . nodes ) {
217
- if ( datastoresSplitValues . includes ( hostNode . key ) ) {
218
- curr . push ( hostNode . key ) ;
240
+ if ( node . children ) {
241
+ for ( const hostNode of node . children ) {
242
+ if ( values . datastores_tree && values . datastores_tree . includes ( hostNode . value ) ) {
243
+ curr . push ( hostNode . value ) ;
219
244
}
220
- if ( hostNode === node . nodes [ node . nodes . length - 1 ] ) {
221
- if ( curr . length === node . nodes . length ) {
222
- params . datastores_checked . push ( { id : node . key , capture : true } ) ;
245
+ if ( hostNode === node . children [ node . children . length - 1 ] ) {
246
+ if ( curr . length === node . children . length ) {
247
+ params . datastores_checked . push ( { id : node . value . split ( '#' ) [ 0 ] , capture : true } ) ;
248
+ for ( const val of curr ) {
249
+ const index = values . datastores_tree . findIndex ( ( host ) => host . id === val ) ;
250
+ if ( index > - 1 ) {
251
+ values . datastores_tree . splice ( index , 1 ) ;
252
+ }
253
+ }
223
254
} else {
224
- params . datastores_checked . push ( { id : node . key , capture : false } ) ;
255
+ params . datastores_checked . push ( { id : node . value . split ( '#' ) [ 0 ] , capture : false } ) ;
225
256
}
226
257
}
227
258
}
259
+ } else if ( datastoresSplitValues . includes ( node . value . split ( '#' ) [ 0 ] ) ) {
260
+ params . datastores_checked . push ( { id : node . value . split ( '#' ) [ 0 ] , capture : true } ) ;
228
261
} else {
229
- if ( datastoresSplitValues . includes ( node . key ) ) {
230
- params . datastores_checked . push ( { id : node . key , capture : true } ) ;
231
- } else {
232
- params . datastores_checked . push ( { id : node . key , capture : false } ) ;
233
- }
262
+ params . datastores_checked . push ( { id : node . value . split ( '#' ) [ 0 ] , capture : false } ) ;
234
263
}
235
264
}
236
265
@@ -242,82 +271,90 @@ const SettingsCUCollectionTab = ({
242
271
} ;
243
272
244
273
const handleReset = ( ) => {
245
- let clustersNodes = [ ] ;
246
- let datastoresNodes = [ ] ;
247
- if ( clusterTree ) {
248
- const clustersBsTree = JSON . parse ( clusterTree . bs_tree ) ;
249
- clustersNodes = clustersBsTree . map ( ( node ) => transformTree ( node , false , 0 ) ) ;
250
- }
251
- if ( datastoreTree ) {
252
- const datastoresBsTree = JSON . parse ( datastoreTree . bs_tree ) ;
253
- datastoresNodes = datastoresBsTree . map ( ( node ) => transformTree ( node , true , 0 ) ) ;
254
- }
255
-
256
- const hostsChecked = [ ] ;
257
- const hostsCheckedWithId = [ ] ;
258
- const datastoresChecked = [ ] ;
259
- const datastoresCheckedWithId = [ ] ;
260
- http . get ( fetchURL ) . then ( ( result ) => {
261
- const { hosts, datastores } = result ;
262
- console . log ( "results: " , result ) ;
263
- for ( const host of hosts ) {
264
- if ( host . capture === true ) {
265
- hostsChecked . push ( `h-${ host . id } ` ) ;
266
- }
267
- }
268
- for ( const datastore of datastores ) {
269
- if ( datastore . capture === true ) {
270
- datastoresChecked . push ( `ds-${ datastore . id } ` ) ;
271
- }
272
- }
273
-
274
- for ( const node of clustersNodes ) {
275
- if ( node . children ) {
276
- for ( const host of node . children ) {
277
- if ( hostsChecked . includes ( host . value . split ( '#' ) [ 0 ] ) ) {
278
- hostsCheckedWithId . push ( host . value ) ;
279
- }
280
- }
281
- }
282
- }
283
-
284
- for ( const node of datastoresNodes ) {
285
- if ( node . children ) {
286
- if ( datastoresChecked . includes ( node . value . split ( '#' ) [ 0 ] ) ) {
287
- for ( const host of node . children ) {
288
- datastoresCheckedWithId . push ( host . value ) ;
289
- }
290
- }
291
- } else {
292
- if ( datastoresChecked . includes ( node . value . split ( '#' ) [ 0 ] ) ) {
293
- datastoresCheckedWithId . push ( node . value ) ;
294
- }
295
- }
296
- }
297
- setData ( {
298
- ...data ,
299
- clustersNodes,
300
- datastoresNodes,
301
- hostsCheckedWithId,
302
- datastoresCheckedWithId,
303
- } ) ;
304
- } ) ;
274
+ getNodeValues ( ) ;
305
275
} ;
306
276
307
277
return (
308
278
< MiqFormRenderer
309
279
className = "toggle"
310
280
schema = { createSchema (
311
- clusterTree , datastoreTree , data . clustersNodes , data . datastoresNodes ,
312
- data . hostsCheckedWithId , data . datastoresCheckedWithId , allClusters , allDatastores ,
281
+ clusterTree , datastoreTree , data . clustersNodes , data . datastoresNodes , data . hostsCheckedWithId ,
282
+ data . datastoresCheckedWithId , allClusters , allDatastores , data . callNumber ,
313
283
) }
314
284
onSubmit = { handleSubmit }
315
285
canSubmit
316
286
canReset
317
287
onReset = { handleReset }
318
- // FormTemplate={(props) => <FormTemplate {...props} />}
288
+ validate = { ( values ) => customValidation ( values ) }
289
+ FormTemplate = { ( props ) => < FormTemplate { ...props } /> }
319
290
/>
320
291
) ;
321
292
} ;
322
293
294
+ const FormTemplate = ( {
295
+ formFields,
296
+ } ) => {
297
+ const {
298
+ handleSubmit, onReset, getState,
299
+ } = useFormApi ( ) ;
300
+ const { valid } = getState ( ) ;
301
+ return (
302
+ < form onSubmit = { handleSubmit } >
303
+ { formFields }
304
+ < FormSpy >
305
+ { ( ) => (
306
+ < div className = "custom-button-wrapper" >
307
+ < Button
308
+ disabled = { ! valid || ! modified }
309
+ kind = "primary"
310
+ className = "btnRight"
311
+ type = "submit"
312
+ variant = "contained"
313
+ >
314
+ { __ ( 'Save' ) }
315
+ </ Button >
316
+ < Button
317
+ disabled = { ! modified }
318
+ kind = "secondary"
319
+ className = "btnRight"
320
+ variant = "contained"
321
+ onClick = { onReset }
322
+ type = "button"
323
+ >
324
+ { __ ( 'Reset' ) }
325
+ </ Button >
326
+ </ div >
327
+ ) }
328
+ </ FormSpy >
329
+ </ form >
330
+ ) ;
331
+ } ;
332
+
333
+ SettingsCUCollectionTab . propTypes = {
334
+ url : PropTypes . string ,
335
+ fetchURL : PropTypes . string ,
336
+ clusterTree : PropTypes . object ,
337
+ datastoreTree : PropTypes . object ,
338
+ allClusters : PropTypes . bool ,
339
+ allDatastores : PropTypes . bool ,
340
+
341
+ } ;
342
+
343
+ SettingsCUCollectionTab . defaultProps = {
344
+ url : '' ,
345
+ fetchURL : '' ,
346
+ clusterTree : undefined ,
347
+ datastoreTree : undefined ,
348
+ allClusters : false ,
349
+ allDatastores : false ,
350
+ } ;
351
+
352
+ FormTemplate . propTypes = {
353
+ formFields : PropTypes . object ,
354
+ } ;
355
+
356
+ FormTemplate . defaultProps = {
357
+ formFields : undefined ,
358
+ } ;
359
+
323
360
export default SettingsCUCollectionTab ;
0 commit comments