ruv commited on
Commit
7aa3f89
·
verified ·
1 Parent(s): 66e1dc0

Update src/App.js

Browse files
Files changed (1) hide show
  1. src/App.js +15 -132
src/App.js CHANGED
@@ -1,142 +1,25 @@
1
- import React, { useState, useEffect, useCallback } from 'react';
2
- import ReactFlow, { useNodesState, useEdgesState, addEdge, MiniMap, Controls } from 'reactflow';
3
  import 'reactflow/dist/style.css';
 
4
 
5
- import ColorSelectorNode from './ColorSelectorNode';
6
-
7
- import './index.css';
8
-
9
- const initBgColor = '#1A192B';
10
-
11
- const connectionLineStyle = { stroke: '#fff' };
12
- const snapGrid = [20, 20];
13
- const nodeTypes = {
14
- selectorNode: ColorSelectorNode,
15
- };
16
-
17
- const defaultViewport = { x: 0, y: 0, zoom: 1.5 };
18
-
19
- const CustomNodeFlow = () => {
20
- const [nodes, setNodes, onNodesChange] = useNodesState([]);
21
- const [edges, setEdges, onEdgesChange] = useEdgesState([]);
22
- const [bgColor, setBgColor] = useState(initBgColor);
23
 
24
  useEffect(() => {
25
- const onChange = (event) => {
26
- setNodes((nds) =>
27
- nds.map((node) => {
28
- if (node.id !== '2') {
29
- return node;
30
- }
31
-
32
- const color = event.target.value;
33
-
34
- setBgColor(color);
35
-
36
- return {
37
- ...node,
38
- data: {
39
- ...node.data,
40
- color,
41
- },
42
- };
43
- })
44
- );
45
- };
46
-
47
- setNodes([
48
- {
49
- id: '1',
50
- type: 'input',
51
- data: { label: 'An input node' },
52
- position: { x: 0, y: 50 },
53
- sourcePosition: 'right',
54
- },
55
- {
56
- id: '2',
57
- type: 'selectorNode',
58
- data: { onChange: onChange, color: initBgColor },
59
- style: { border: '1px solid #777', padding: 10 },
60
- position: { x: 300, y: 50 },
61
- },
62
- {
63
- id: '3',
64
- type: 'output',
65
- data: { label: 'Output A' },
66
- position: { x: 650, y: 25 },
67
- targetPosition: 'left',
68
- },
69
- {
70
- id: '4',
71
- type: 'output',
72
- data: { label: 'Output B' },
73
- position: { x: 650, y: 100 },
74
- targetPosition: 'left',
75
- },
76
- ]);
77
-
78
- setEdges([
79
- {
80
- id: 'e1-2',
81
- source: '1',
82
- target: '2',
83
- animated: true,
84
- style: { stroke: '#fff' },
85
- },
86
- {
87
- id: 'e2a-3',
88
- source: '2',
89
- target: '3',
90
- sourceHandle: 'a',
91
- animated: true,
92
- style: { stroke: '#fff' },
93
- },
94
- {
95
- id: 'e2b-4',
96
- source: '2',
97
- target: '4',
98
- sourceHandle: 'b',
99
- animated: true,
100
- style: { stroke: '#fff' },
101
- },
102
- ]);
103
  }, []);
104
 
105
- const onConnect = useCallback(
106
- (params) =>
107
- setEdges((eds) => addEdge({ ...params, animated: true, style: { stroke: '#fff' } }, eds)),
108
- []
109
- );
110
  return (
111
- <ReactFlow
112
- nodes={nodes}
113
- edges={edges}
114
- onNodesChange={onNodesChange}
115
- onEdgesChange={onEdgesChange}
116
- onConnect={onConnect}
117
- style={{ background: bgColor }}
118
- nodeTypes={nodeTypes}
119
- connectionLineStyle={connectionLineStyle}
120
- snapToGrid={true}
121
- snapGrid={snapGrid}
122
- defaultViewport={defaultViewport}
123
- fitView
124
- attributionPosition="bottom-left"
125
- >
126
- <MiniMap
127
- nodeStrokeColor={(n) => {
128
- if (n.type === 'input') return '#0041d0';
129
- if (n.type === 'selectorNode') return bgColor;
130
- if (n.type === 'output') return '#ff0072';
131
- }}
132
- nodeColor={(n) => {
133
- if (n.type === 'selectorNode') return bgColor;
134
- return '#fff';
135
- }}
136
- />
137
- <Controls />
138
- </ReactFlow>
139
  );
140
  };
141
 
142
- export default CustomNodeFlow;
 
1
+ import React, { useEffect, useState } from 'react';
2
+ import ReactFlow from 'reactflow';
3
  import 'reactflow/dist/style.css';
4
+ import './styles.css';
5
 
6
+ const App = () => {
7
+ const [elements, setElements] = useState([]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  useEffect(() => {
10
+ fetch('http://localhost:5000/api/data')
11
+ .then(response => response.json())
12
+ .then(data => {
13
+ setElements(data.nodes.concat(data.edges));
14
+ })
15
+ .catch(error => console.error('Error:', error));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  }, []);
17
 
 
 
 
 
 
18
  return (
19
+ <div className="app">
20
+ <ReactFlow elements={elements} />
21
+ </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  );
23
  };
24
 
25
+ export default App;