-
Notifications
You must be signed in to change notification settings - Fork 2
/
EditNodeModal.js
112 lines (93 loc) · 2.98 KB
/
EditNodeModal.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React, { useState, useEffect } from "react";
import Modal from "react-modal";
Modal.setAppElement("#root");
const EditNodeModal = ({ isOpen, nodeData, onConfirm, onCancel, setGraphData, writeGraphToDatabase }) => {
const [labels, setLabels] = useState(nodeData ? [nodeData.label] : []);
const [properties, setProperties] = useState(nodeData ? nodeData.properties || {} : {});
useEffect(() => {
if (nodeData) {
setLabels([nodeData.label]);
setProperties(nodeData.properties || {});
}
}, [nodeData]);
const handleConfirm = () => {
const newNodeData = { ...nodeData, label: labels.join(", "), properties: properties };
onConfirm(newNodeData);
};
const addLabel = () => {
setLabels([...labels, ""]);
};
const updateLabel = (index, value) => {
setLabels(labels.map((label, i) => (i === index ? value : label)));
};
const removeLabel = (index) => {
setLabels(labels.filter((_, i) => i !== index));
};
const handleKeyChange = (oldKey) => (event) => {
const newKey = event.target.value;
setProperties((prevProperties) => {
const newProperties = { ...prevProperties };
newProperties[newKey] = newProperties[oldKey];
delete newProperties[oldKey];
return newProperties;
});
};
const handleValueChange = (key) => (event) => {
const newValue = event.target.value;
setProperties((prevProperties) => ({
...prevProperties,
[key]: newValue,
}));
};
const handleAddProperty = () => {
setProperties(prevProperties => ({
...prevProperties,
newProperty: '',
}));
};
const handleRemoveProperty = (key) => () => {
setProperties(prevProperties => {
const newProperties = {...prevProperties};
delete newProperties[key];
return newProperties;
});
};
return (
<Modal isOpen={isOpen} onRequestClose={onCancel}>
<h2>Edit Node</h2>
<div>
Labels:
{labels.map((label, index) => (
<div key={index}>
<input
type="text"
value={label}
onChange={(e) => updateLabel(index, e.target.value)}
/>
<button onClick={() => removeLabel(index)}>Remove</button>
</div>
))}
<button onClick={addLabel}>Add Label</button>
</div>
<br />
{Object.entries(properties).map(([key, value]) => (
<div key={key}>
<label>
Key:
<input type="text" defaultValue={key} onBlur={handleKeyChange(key)} />
</label>
<label>
Value:
<input type="text" value={value} onChange={handleValueChange(key)} />
</label>
<button onClick={handleRemoveProperty(key)}>Remove property</button>
</div>
))}
<button onClick={handleAddProperty}>Add property</button>
<br />
<button onClick={handleConfirm}>Confirm</button>
<button onClick={onCancel}>Cancel</button>
</Modal>
);
};
export default EditNodeModal;