-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
62 lines (54 loc) · 1.43 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { useState } from 'react';
import { GraphJsonEditor } from './GraphJsonEditor';
import { getNextIndex, Graph } from './Graph';
import initial from './sample.json';
import { useLocalStorage } from 'react-use';
export const App = () => {
const [graph, setGraph] = useLocalStorage('graphData', initial);
const [selected, setSelected] = useState();
const createNode = () => {
let newNode = {
id: getNextIndex(graph),
title: 'New Node',
x: 0,
y: 0,
type: 'empty'
};
setGraph({
...graph,
nodes: [
...graph.nodes,
newNode
]
});
};
const createEdge = () => {
let newEdge = {
source: selected.id,
target: graph.nodes[graph.nodes.length - 1].id,
type: 'default',
handleText: 'X'
};
setGraph({
...graph,
edges: [
...graph.edges,
newEdge
]
});
};
const reset = () => {
setGraph(initial);
};
return (
<React.Fragment>
<div className="control__container">
<button onClick={createNode} className="button--primary">Add Node</button>
<button onClick={createEdge} disabled={selected === null}>Add Edge</button>
<button onClick={reset}>Reset</button>
</div>
<Graph graph={graph} setGraph={setGraph} selected={selected} setSelected={setSelected} />
<GraphJsonEditor graph={graph} setGraph={setGraph} />
</React.Fragment>
);
};