|
| 1 | +import { useState } from 'react'; |
| 2 | +import { v4 as uuidv4 } from 'uuid'; |
| 3 | +import Form from 'react-bootstrap/Form'; |
| 4 | +import Modal from 'react-bootstrap/Modal'; |
| 5 | +import Button from 'react-bootstrap/Button'; |
| 6 | + |
| 7 | +import { Client, gql } from '../api'; |
| 8 | + |
| 9 | + |
| 10 | +function generateRandomString(length: number) { |
| 11 | + const array = new Uint8Array(length); |
| 12 | + window.crypto.getRandomValues(array); |
| 13 | + return Array.from(array, byte => ('0' + byte.toString(16)).slice(-2)).join(''); |
| 14 | +} |
| 15 | + |
| 16 | +// Get the current node and edge types to render new node forms |
| 17 | +let nodeTypes: any = 0; |
| 18 | +function getNodeTypes() { |
| 19 | + let query = gql`{ |
| 20 | + getNodeTypes { |
| 21 | + success |
| 22 | + message |
| 23 | + errors |
| 24 | + nodeTypes { |
| 25 | + id |
| 26 | + name |
| 27 | + description |
| 28 | + fields |
| 29 | + created_at |
| 30 | + updated_at |
| 31 | + } |
| 32 | + } |
| 33 | + }`; |
| 34 | + Client(query).then((data: any) => { |
| 35 | + nodeTypes = data.getNodeTypes.nodeTypes; |
| 36 | + }) |
| 37 | +} |
| 38 | +getNodeTypes(); |
| 39 | + |
| 40 | +function createNode(node: any, activeFlow: any, node_type_id: any) { |
| 41 | + let mutation = gql`mutation { |
| 42 | + createNode(node: "${encodeURIComponent(JSON.stringify(node))}", nid: "${node.id}", flow_id: ${ parseInt(activeFlow.id)}, workspace_id: ${parseInt(activeFlow.workspace_id)}, node_type_id: ${node_type_id + 1}) { |
| 43 | + success |
| 44 | + message |
| 45 | + errors |
| 46 | + node { |
| 47 | + nid |
| 48 | + node |
| 49 | + flow_id |
| 50 | + workspace_id |
| 51 | + node_type_id |
| 52 | + created_at |
| 53 | + updated_at |
| 54 | + } |
| 55 | + } |
| 56 | + }`; |
| 57 | + |
| 58 | + Client(mutation) |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +export function RenderNodeTypeForm(field: any, value: any) { |
| 63 | + return ( |
| 64 | + <Form.Group |
| 65 | + className="mb-3" |
| 66 | + controlId="NewModal.{field}" |
| 67 | + key={field} |
| 68 | + > |
| 69 | + <Form.Label>{field}</Form.Label> |
| 70 | + {value === 'textarea' ? ( |
| 71 | + <Form.Control name={field} as={value} rows={3} /> |
| 72 | + ) : value === 'text' ? ( |
| 73 | + <Form.Control name={field} type={value} /> |
| 74 | + ) : value === 'number' ? ( |
| 75 | + <Form.Control name={field} type={value} /> |
| 76 | + ) : ( |
| 77 | + <Form.Control name={field} type='text' /> |
| 78 | + )} |
| 79 | + </Form.Group> |
| 80 | + ) |
| 81 | +}; |
| 82 | + |
| 83 | + |
| 84 | +function NewSubNode({ show, setShow, setNodes, activeFlow, node }: { show: boolean, setShow: any, setNodes: any, activeFlow: any, node: any }) { |
| 85 | + let fieldIdx = 0; |
| 86 | + const handleClose = () => setShow(false); |
| 87 | + const [selectValue, setSelectValue] = useState(0); |
| 88 | + const nodes: any = ['input', 'output', 'source', 'card', 'default', 'subflow']; |
| 89 | + const [nodeType, setNodeType] = useState<any>({ 'id': 1, 'fields': { 'label': 'textarea' } }); |
| 90 | + |
| 91 | + function handleSubmit(e: any) { |
| 92 | + e.preventDefault(); |
| 93 | + |
| 94 | + // Loop over the form fields and add them to the node data field |
| 95 | + const formData: { [key: string]: string } = {}; |
| 96 | + for (let i = 2; i < e.target.length-2; i++) { |
| 97 | + const fieldName = e.target[i].name; |
| 98 | + const fieldValue = e.target[i].value; |
| 99 | + formData[fieldName] = fieldValue; |
| 100 | + } |
| 101 | + |
| 102 | + // If the node type is a subflow, add the flow slug to the node data |
| 103 | + if (selectValue === 5) { |
| 104 | + formData['slug'] = generateRandomString(3); |
| 105 | + } |
| 106 | + |
| 107 | + const newNode = { |
| 108 | + id: uuidv4(), |
| 109 | + position: { |
| 110 | + x: node.width/2, |
| 111 | + y: node.height/2 |
| 112 | + }, |
| 113 | + data: formData, |
| 114 | + origin: [0.5, 0.5], |
| 115 | + type: nodes[e.target[2].value], |
| 116 | + extent: "parent", |
| 117 | + parentId: node.id |
| 118 | + }; |
| 119 | + |
| 120 | + // Update the node list |
| 121 | + setNodes((nodes: any) => nodes.concat(newNode)) |
| 122 | + |
| 123 | + console.log('node type', nodes); |
| 124 | + // Send node data to the server |
| 125 | + createNode(newNode, activeFlow, parseInt(e.target[2].value)); |
| 126 | + |
| 127 | + // Close the modal |
| 128 | + handleClose(); |
| 129 | + }; |
| 130 | + |
| 131 | + return ( |
| 132 | + <> |
| 133 | + <Modal show={show} onHide={handleClose}> |
| 134 | + <Form name='NewModal' onSubmit={e => {handleSubmit(e)}}> |
| 135 | + <Modal.Header closeButton> |
| 136 | + <Modal.Title>Create Node</Modal.Title> |
| 137 | + </Modal.Header> |
| 138 | + <Modal.Body> |
| 139 | + <Form.Group className="mb-3" controlId="NewModal.NodeType"> |
| 140 | + <Form.Label>Parent Node Id</Form.Label> |
| 141 | + <Form.Control name="parentId" type="text" value={node?.id} disabled/> |
| 142 | + <Form.Label>Select Node Type:</Form.Label> |
| 143 | + <Form.Select aria-label="Default select example" title="SelectNodeType" onChange={(e) => { |
| 144 | + setNodeType(nodeTypes[e.target.value]); |
| 145 | + setSelectValue(parseInt(e.target.value)); |
| 146 | + }} value={selectValue}> |
| 147 | + {nodeTypes && nodeTypes.map((nType: any) => { |
| 148 | + fieldIdx++; |
| 149 | + return (<option key={fieldIdx} value={nType.id-1}>{nType.name}</option>); |
| 150 | + }) |
| 151 | + } |
| 152 | + </Form.Select> |
| 153 | + </Form.Group> |
| 154 | + {nodeTypes && Object.keys(nodeType.fields).map((key) => { |
| 155 | + return (RenderNodeTypeForm(key, nodeType.fields[key])); |
| 156 | + })} |
| 157 | + </Modal.Body> |
| 158 | + <Modal.Footer> |
| 159 | + <Button variant="secondary" onClick={handleClose}> |
| 160 | + Close |
| 161 | + </Button> |
| 162 | + <Button variant="primary" type='submit'> |
| 163 | + Create |
| 164 | + </Button> |
| 165 | + </Modal.Footer> |
| 166 | + </Form> |
| 167 | + </Modal> |
| 168 | + </> |
| 169 | + ); |
| 170 | +} |
| 171 | + |
| 172 | +export default NewSubNode; |
0 commit comments