Skip to content

Commit

Permalink
Update relationships when fields get reordered
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed Apr 21, 2024
1 parent b9a8cde commit 3a58014
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/components/EditorSidePanel/TablesTab/TableInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import IndexDetails from "./IndexDetails";

export default function TableInfo({ data }) {
const [indexActiveKey, setIndexActiveKey] = useState("");
const { deleteTable, updateTable, updateField } = useTables();
const { deleteTable, updateTable, updateField, setRelationships } =
useTables();
const { setUndoStack, setRedoStack } = useUndoRedo();
const [editField, setEditField] = useState({});
const draggingElementIndex = useRef();
Expand Down Expand Up @@ -79,6 +80,28 @@ export default function TableInfo({ data }) {

updateField(data.id, index, { ...b, id: index });
updateField(data.id, j, { ...a, id: j });

setRelationships((prev) =>
prev.map((e) => {
if (e.startTableId === data.id) {
if (e.startFieldId === index) {
return { ...e, startFieldId: j };
}
if (e.startFieldId === j) {
return { ...e, startFieldId: index };
}
}
if (e.endTableId === data.id) {
if (e.endFieldId === index) {
return { ...e, endFieldId: j };
}
if (e.endFieldId === j) {
return { ...e, endFieldId: index };
}
}
return e;
}),
);
}}
onDragEnd={(e) => {
e.preventDefault();
Expand Down

0 comments on commit 3a58014

Please sign in to comment.