Skip to content

Commit 12a8ce4

Browse files
Update automate entry point
1 parent 8bf325a commit 12a8ce4

File tree

2 files changed

+128
-167
lines changed

2 files changed

+128
-167
lines changed

_history

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
simulate_queue_worker
2+
simulate_queue_worker
3+
simulate_queue_worker
4+
simulate_queue_worker
5+
simulate_queue_worker
6+
exit

app/javascript/components/automate-entry-points/index.jsx

Lines changed: 122 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,15 @@
1-
import React, { useEffect, useRef, useState } from 'react';
2-
import { Loading } from 'carbon-components-react';
3-
import PropTypes from 'prop-types';
1+
import React, { useEffect, useState } from 'react';
2+
import { Loading, Modal, ModalBody } from 'carbon-components-react';
3+
import PropTypes, { number } from 'prop-types';
44
import {
5-
CheckboxCheckedFilled16,
65
Document16,
76
Folder16,
87
FolderOpen16,
9-
CheckboxIndeterminateFilled16,
10-
Checkbox16,
11-
CaretDown16,
128
} from '@carbon/icons-react';
139
import TreeView from 'react-accessible-treeview';
14-
import cx from 'classnames';
1510
import './styles.css';
1611

17-
let initialData = [
18-
{
19-
name: '',
20-
id: 0,
21-
children: [1, 2, 3],
22-
parent: null,
23-
},
24-
{
25-
name: 'Fruits',
26-
children: [],
27-
id: 1,
28-
parent: 0,
29-
isBranch: true,
30-
},
31-
{
32-
name: 'Drinks',
33-
children: [4, 5],
34-
id: 2,
35-
parent: 0,
36-
isBranch: true,
37-
},
38-
{
39-
name: 'Vegetables',
40-
children: [],
41-
id: 3,
42-
parent: 0,
43-
isBranch: true,
44-
},
45-
{
46-
name: 'Pine colada',
47-
children: [],
48-
id: 4,
49-
parent: 2,
50-
},
51-
{
52-
name: 'Water',
53-
children: [],
54-
id: 5,
55-
parent: 2,
56-
},
57-
];
58-
59-
initialData = [
12+
const initialData = [
6013
{
6114
name: 'Datastore',
6215
id: 'datastore_folder',
@@ -66,14 +19,13 @@ initialData = [
6619
];
6720

6821
const MultiSelectCheckboxAsync = () => {
69-
const loadedAlertElement = useRef(null);
7022
const [data, setData] = useState(initialData);
7123
const [nodesAlreadyLoaded, setNodesAlreadyLoaded] = useState([]);
24+
const [open, setOpen] = useState(true);
25+
const [selectedValue, setSelectedValue] = useState();
26+
const [disableSubmit, setDisableSubmit] = useState(true);
7227

7328
const updateTreeData = (list, id, children) => {
74-
console.log(list);
75-
console.log(id);
76-
console.log(children);
7729
const data = list.map((node) => {
7830
if (node.id === id) {
7931
node.children = children.map((el) => el.id);
@@ -110,78 +62,73 @@ const MultiSelectCheckboxAsync = () => {
11062
}, []);
11163

11264
const onLoadData = ({ element }) => {
113-
console.log(element);
114-
return new Promise((resolve) => {
115-
if (element.children.length > 0) {
116-
resolve();
117-
return;
118-
}
119-
setTimeout(() => {
120-
setData((value) =>
121-
updateTreeData(value, element.id, [
122-
{
123-
name: `Child Node ${value.length}`,
65+
let path = element.name;
66+
if (element.metadata && element.metadata.fqname) {
67+
path = element.metadata.fqname;
68+
}
69+
API.get(`/api/automate/${path}?depth=1`).then((newNodes) => {
70+
const newChildren = [];
71+
newNodes.resources.forEach((newNode) => {
72+
if (element.id !== newNode.id) {
73+
if (newNode.klass !== 'MiqAeClass') {
74+
newChildren.push({
75+
id: newNode.id,
76+
name: newNode.name,
12477
children: [],
125-
id: value.length,
126-
parent: element.id,
12778
isBranch: true,
128-
},
129-
{
130-
name: 'Another child Node',
79+
parent: element.id,
80+
metadata: { fqname: newNode.fqname },
81+
});
82+
} else {
83+
newChildren.push({
84+
id: newNode.id,
85+
name: newNode.name,
13186
children: [],
132-
id: value.length + 1,
13387
parent: element.id,
134-
},
135-
]));
136-
resolve();
137-
}, 1000);
88+
metadata: { fqname: newNode.fqname },
89+
});
90+
}
91+
}
92+
});
93+
return new Promise((resolve) => {
94+
if (element.children.length > 0) {
95+
resolve();
96+
return;
97+
}
98+
setTimeout(() => {
99+
setData((value) =>
100+
updateTreeData(value, element.id, newChildren));
101+
resolve();
102+
}, 1000);
103+
});
138104
});
139105
};
140106

141-
const wrappedOnLoadData = async(props) => {
142-
const nodeHasNoChildData = props.element.children.length === 0;
107+
const wrappedOnLoadData = async({ element }) => {
108+
const nodeHasNoChildData = element.children.length === 0;
143109
const nodeHasAlreadyBeenLoaded = nodesAlreadyLoaded.find(
144-
(e) => e.id === props.element.id
110+
(e) => e.id === element.id
145111
);
146112

147-
await onLoadData(props);
113+
await onLoadData({ element });
148114

149115
if (nodeHasNoChildData && !nodeHasAlreadyBeenLoaded) {
150-
setNodesAlreadyLoaded([...nodesAlreadyLoaded, props.element]);
116+
setNodesAlreadyLoaded([...nodesAlreadyLoaded, element]);
151117

152-
// Clearing aria-live region so loaded node alerts no longer appear in DOM
153118
setTimeout(() => {
154-
}, 5000);
119+
}, 1000);
155120
}
156121
};
157-
158-
const onSelect = (value) => {
159-
if (value.isBranch === false && value.isSelected) {
160-
console.log(value);
161-
}
122+
wrappedOnLoadData.propTypes = {
123+
element: PropTypes.objectOf({ children: PropTypes.array, id: number }).isRequired,
162124
};
163-
164-
const ArrowIcon = ({ isOpen, className }) => {
165-
const baseClass = 'arrow';
166-
const classes = cx(
167-
baseClass,
168-
{ [`${baseClass}--closed`]: !isOpen },
169-
{ [`${baseClass}--open`]: isOpen },
170-
className
171-
);
172-
return <CaretDown16 className={classes} />;
125+
wrappedOnLoadData.defaultProps = {
173126
};
174127

175-
const CheckBoxIcon = ({ variant, ...rest }) => {
176-
switch (variant) {
177-
case 'all':
178-
return <CheckboxCheckedFilled16 />;
179-
case 'none':
180-
return <Checkbox16 />;
181-
case 'some':
182-
return <CheckboxIndeterminateFilled16 />;
183-
default:
184-
return null;
128+
const onSelect = (value) => {
129+
if (value.isBranch === false && value.isSelected) {
130+
setSelectedValue(value);
131+
setDisableSubmit(false);
185132
}
186133
};
187134

@@ -191,76 +138,84 @@ const MultiSelectCheckboxAsync = () => {
191138
) : (
192139
<Folder16 className="icon" />
193140
));
141+
194142
FolderIcon.propTypes = {
195-
isOpen: PropTypes.bool.isRequired,
143+
isOpen: PropTypes.bool,
144+
};
145+
FolderIcon.defaultProps = {
146+
isOpen: false,
196147
};
197148

198149
const FileIcon = () => <Document16 className="icon" />;
199150

200151
return (
201-
<>
202-
<div>
203-
<div className="checkbox">
204-
<TreeView
205-
data={data}
206-
aria-label="Checkbox tree"
207-
onSelect={(value) => onSelect(value)}
208-
onLoadData={wrappedOnLoadData}
209-
togglableSelect
210-
nodeRenderer={({
211-
element,
212-
isBranch,
213-
isExpanded,
214-
isSelected,
215-
isHalfSelected,
216-
getNodeProps,
217-
level,
218-
}) => {
219-
const branchNode = (isExpanded, element) => (isExpanded && element.children.length === 0 ? (
220-
<div className="loadingDiv">
221-
<Loading small withOverlay={false} />
222-
</div>
223-
) : (
224-
null
225-
));
226-
return (
227-
<div
228-
{...getNodeProps()}
229-
style={{ marginLeft: 40 * (level - 1) }}
230-
>
231-
{isBranch && branchNode(isExpanded, element)}
232-
{/* <CheckBoxIcon
233-
className="checkbox-icon"
234-
onClick={(e) => {
235-
handleSelect(e);
236-
e.stopPropagation();
237-
}}
238-
variant={
239-
isHalfSelected ? "some" : isSelected ? "all" : "none"
240-
}
241-
/> */}
242-
{isBranch ? (
243-
<div className="iconDiv">
244-
<FolderIcon isOpen={isExpanded} />
245-
</div>
246-
) : (
247-
<div className="iconDiv">
248-
<FileIcon filename={element.name} />
249-
</div>
250-
)}
251-
<span className="name">{element.name}</span>
252-
</div>
253-
);
254-
}}
255-
/>
152+
<Modal
153+
open={open}
154+
primaryButtonText={__('OK')}
155+
secondaryButtonText={__('Cancel')}
156+
onRequestSubmit={() => {
157+
console.log(selectedValue);
158+
setOpen(false);
159+
}}
160+
onRequestClose={() => {
161+
setOpen(false);
162+
}}
163+
onSecondarySubmit={() => {
164+
setOpen(false);
165+
}}
166+
primaryButtonDisabled={disableSubmit}
167+
>
168+
<ModalBody>
169+
<div>
170+
<div className="checkbox">
171+
<TreeView
172+
data={data}
173+
aria-label="Checkbox tree"
174+
onSelect={(value) => onSelect(value)}
175+
onLoadData={wrappedOnLoadData}
176+
togglableSelect
177+
nodeRenderer={({
178+
element,
179+
isBranch,
180+
isExpanded,
181+
getNodeProps,
182+
level,
183+
}) => {
184+
const branchNode = (isExpanded, element) => (isExpanded && element.children.length === 0 ? (
185+
<div className="loadingDiv">
186+
<Loading small withOverlay={false} />
187+
</div>
188+
) : (
189+
null
190+
));
191+
return (
192+
<div
193+
{...getNodeProps()}
194+
style={{ marginLeft: 40 * (level - 1) }}
195+
>
196+
{isBranch && branchNode(isExpanded, element)}
197+
{isBranch ? (
198+
<div className="iconDiv">
199+
<FolderIcon isOpen={isExpanded} />
200+
</div>
201+
) : (
202+
<div className="iconDiv">
203+
<FileIcon filename={element.name} />
204+
</div>
205+
)}
206+
<span className="name">{element.name}</span>
207+
</div>
208+
);
209+
}}
210+
/>
211+
</div>
256212
</div>
257-
</div>
258-
</>
213+
</ModalBody>
214+
</Modal>
259215
);
260216
};
261217

262218
MultiSelectCheckboxAsync.propTypes = {
263-
isOpen: PropTypes.bool.isRequired,
264219
};
265220

266221
MultiSelectCheckboxAsync.defaultProps = {

0 commit comments

Comments
 (0)