Skip to content

Commit 1f0c61f

Browse files
committed
working, just need ot write test
1 parent dee3bcf commit 1f0c61f

File tree

3 files changed

+150
-114
lines changed

3 files changed

+150
-114
lines changed

app/controllers/ops_controller/settings/cap_and_u.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ def cu_collection_update
77
return unless load_edit("cu_edit__collection", "replace_cell__explorer")
88

99
cu_collection_get_form_vars
10-
# require 'byebug'
11-
# byebug
10+
1211
if params[:button] == "save"
1312
# C & U collection settings
1413
if @edit[:new][:all_clusters] != @edit[:current][:all_clusters]

app/javascript/components/settings-cu-collection/index.jsx

Lines changed: 144 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* eslint-disable no-restricted-syntax */
22
import React, { useState, useEffect } from 'react';
3+
import { FormSpy } from '@data-driven-forms/react-form-renderer';
34
import MiqFormRenderer, { useFormApi } from '@@ddf';
45
import {
56
Db2Database20, BareMetalServer20,
67
} 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';
911

1012
let idCounter = 0;
11-
let hostsOrDatastoresIds = new Set();
13+
let modified = false;
1214

1315
const SettingsCUCollectionTab = ({
1416
url, fetchURL, clusterTree, datastoreTree, allClusters, allDatastores,
@@ -20,8 +22,25 @@ const SettingsCUCollectionTab = ({
2022
datastoresNodes: [],
2123
hostsCheckedWithId: [],
2224
datastoresCheckedWithId: [],
25+
callNumber: 0,
2326
});
2427

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+
2544
const generateId = () => idCounter++;
2645

2746
const parseLabel = (text) => {
@@ -53,11 +72,11 @@ const SettingsCUCollectionTab = ({
5372
let icon;
5473
switch (node.icon) {
5574
case 'fa fa-database':
56-
icon = Db2Database20 ? <Db2Database20 color="black" /> : <span>Icon DATABSEnot available</span>;
75+
icon = <Db2Database20 color="black" />;
5776
break;
5877

5978
case 'pficon pficon-cluster':
60-
icon = BareMetalServer20 ? <BareMetalServer20 color="black" /> : <span>Icon SERvERASDSADASDSADSADASDa not available</span>;
79+
icon = <BareMetalServer20 color="black" />;
6180
break;
6281
default:
6382
break;
@@ -76,23 +95,10 @@ const SettingsCUCollectionTab = ({
7695
return nodeObject;
7796
};
7897

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 = () => {
9499
let clustersNodes = [];
95100
let datastoresNodes = [];
101+
idCounter = 0;
96102
if (clusterTree) {
97103
const clustersBsTree = JSON.parse(clusterTree.bs_tree);
98104
clustersNodes = clustersBsTree.map((node) => transformTree(node, false, 0));
@@ -131,25 +137,46 @@ const SettingsCUCollectionTab = ({
131137

132138
for (const node of datastoresNodes) {
133139
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) {
135144
for (const host of node.children) {
136145
datastoresCheckedWithId.push(host.value);
137146
}
138147
}
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);
143150
}
144151
}
152+
145153
setData({
146154
...data,
147155
clustersNodes,
148156
datastoresNodes,
149157
hostsCheckedWithId,
150158
datastoresCheckedWithId,
159+
callNumber: data.callNumber + 1,
151160
});
152161
});
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();
153180
}, []);
154181

155182
const handleSubmit = (values) => {
@@ -201,36 +228,38 @@ const SettingsCUCollectionTab = ({
201228
}
202229
}
203230
}
231+
} else if (clustersSplitValues.includes(node.key)) {
232+
params.clusters_checked.push({ id: node.key, capture: true });
204233
} 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 });
210235
}
211236
}
212237

213-
for (const node of datastoreTree.tree_nodes) {
238+
for (const node of data.datastoresNodes) {
214239
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);
219244
}
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+
}
223254
} else {
224-
params.datastores_checked.push({ id: node.key, capture: false });
255+
params.datastores_checked.push({ id: node.value.split('#')[0], capture: false });
225256
}
226257
}
227258
}
259+
} else if (datastoresSplitValues.includes(node.value.split('#')[0])) {
260+
params.datastores_checked.push({ id: node.value.split('#')[0], capture: true });
228261
} 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 });
234263
}
235264
}
236265

@@ -242,82 +271,90 @@ const SettingsCUCollectionTab = ({
242271
};
243272

244273
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();
305275
};
306276

307277
return (
308278
<MiqFormRenderer
309279
className="toggle"
310280
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,
313283
)}
314284
onSubmit={handleSubmit}
315285
canSubmit
316286
canReset
317287
onReset={handleReset}
318-
// FormTemplate={(props) => <FormTemplate {...props} />}
288+
validate={(values) => customValidation(values)}
289+
FormTemplate={(props) => <FormTemplate {...props} />}
319290
/>
320291
);
321292
};
322293

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+
323360
export default SettingsCUCollectionTab;

app/javascript/components/settings-cu-collection/settings-cu-collection-tab.schema.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { componentTypes } from '@@ddf';
22

33
const createSchema = (
4-
clustersTree, datastoresTree, clustersNodes, datastoresNodes, hostsChecked, datastoresChecked, allClusters, allDatastores,
4+
clustersTree, datastoresTree, clustersNodes, datastoresNodes, hostsChecked, datastoresChecked, allClusters, allDatastores, callNumber,
55
) => {
66
const fields = [
77
{
@@ -69,7 +69,7 @@ const createSchema = (
6969
component: 'checkbox-tree',
7070
id: 'clusters-tree',
7171
name: 'clusters_tree',
72-
key: `${clustersNodes.length}`,
72+
key: `${clustersNodes.length}${callNumber}`,
7373
className: 'clusters-tree',
7474
nodes: clustersNodes,
7575
checked: hostsChecked,
@@ -84,8 +84,8 @@ const createSchema = (
8484
className: 'vm-data',
8585
condition: { and: [{ when: 'all_clusters', is: false }] },
8686
label: __(
87-
'VM data will be collected for VMs under selected Hosts only. ' +
88-
'Data is collected for a Cluster and all of its Hosts when at least one Host is selected.'
87+
'VM data will be collected for VMs under selected Hosts only. '
88+
+ 'Data is collected for a Cluster and all of its Hosts when at least one Host is selected.'
8989
),
9090
},
9191
...(clustersTree != null
@@ -150,7 +150,7 @@ const createSchema = (
150150
id: 'datastores-tree',
151151
name: 'datastores_tree',
152152
className: 'datastores-tree',
153-
key: `${datastoresNodes.length}`,
153+
key: `${datastoresNodes.length}${callNumber}`,
154154
nodes: datastoresNodes,
155155
checked: datastoresChecked,
156156
condition: { and: [{ when: 'all_datastores', is: false }] },

0 commit comments

Comments
 (0)