Skip to content

Commit 768c8c3

Browse files
committed
saves the details and nutrients on edit
1 parent 8134ced commit 768c8c3

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

src/features/modals/NodeModal/index.tsx

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ import useJson from "../../../store/useJson";
77
import type { NodeData } from "../../../types/graph";
88
import useGraph from "../../editor/views/GraphView/stores/useGraph";
99

10+
// get the actual value from the full JSON at the given path
11+
const getValueAtPath = (json: string, path?: (string | number)[]): string => {
12+
try {
13+
const parsed = JSON.parse(json);
14+
if (!path || path.length === 0) return JSON.stringify(parsed, null, 2);
15+
16+
let current = parsed;
17+
for (const segment of path) {
18+
current = current[segment];
19+
}
20+
return JSON.stringify(current, null, 2);
21+
} catch {
22+
return "{}";
23+
}
24+
};
25+
1026
// return object from json removing array and object fields
1127
const normalizeNodeData = (nodeRows: NodeData["text"]) => {
1228
if (!nodeRows || nodeRows.length === 0) return "{}";
@@ -43,13 +59,19 @@ export const NodeModal = ({ opened, onClose }: ModalProps) => {
4359
// keep edited content in sync when node changes
4460
React.useEffect(() => {
4561
setEditing(false);
46-
setEditedContent(normalizeNodeData(nodeData?.text ?? []));
62+
// use the actual value from JSON at the path to preserve nested details
63+
const fullJson = useJson.getState().json;
64+
const path = nodeData?.path ?? [];
65+
setEditedContent(getValueAtPath(fullJson, path));
4766
}, [nodeData]);
4867

4968
const handleEdit = () => setEditing(true);
5069

5170
const handleCancel = () => {
52-
setEditedContent(normalizeNodeData(nodeData?.text ?? []));
71+
// restore to the full JSON value at the path
72+
const fullJson = useJson.getState().json;
73+
const path = nodeData?.path ?? [];
74+
setEditedContent(getValueAtPath(fullJson, path));
5375
setEditing(false);
5476
};
5577

@@ -100,22 +122,20 @@ export const NodeModal = ({ opened, onClose }: ModalProps) => {
100122
<Text fz="xs" fw={500}>
101123
Content
102124
</Text>
103-
<Flex gap="xs" align="center">
104-
{!editing ? (
105-
<Button size="xs" variant="light" onClick={handleEdit}>
106-
Edit
125+
{!editing ? (
126+
<Button size="xs" variant="light" onClick={handleEdit}>
127+
Edit
128+
</Button>
129+
) : (
130+
<Flex gap="xs" align="center">
131+
<Button size="xs" color="green" onClick={handleSave}>
132+
Save
133+
</Button>
134+
<Button size="xs" variant="light" color="gray" onClick={handleCancel}>
135+
Cancel
107136
</Button>
108-
) : (
109-
<>
110-
<Button size="xs" color="green" onClick={handleSave}>
111-
Save
112-
</Button>
113-
<Button size="xs" variant="light" color="gray" onClick={handleCancel}>
114-
Cancel
115-
</Button>
116-
</>
117-
)}
118-
</Flex>
137+
</Flex>
138+
)}
119139
</Flex>
120140

121141
{!editing ? (

0 commit comments

Comments
 (0)