Fixed delete not updating node internal

This commit is contained in:
Lucas Oliveira 2023-11-29 14:46:40 -03:00
commit 61504d8702
2 changed files with 22 additions and 41 deletions

View file

@ -6,12 +6,13 @@ import {
useEffect, useEffect,
useState, useState,
} from "react"; } from "react";
import { Edge, Node, ReactFlowInstance } from "reactflow"; import { ReactFlowInstance } from "reactflow";
import { getAll, getHealth } from "../controllers/API"; import { getAll, getHealth } from "../controllers/API";
import { APIKindType } from "../types/api"; import { APIKindType } from "../types/api";
import { typesContextType } from "../types/typesContext"; import { typesContextType } from "../types/typesContext";
import { alertContext } from "./alertContext"; import { alertContext } from "./alertContext";
import { AuthContext } from "./authContext"; import { AuthContext } from "./authContext";
import { undoRedoContext } from "./undoRedoContext";
//context to share types adn functions from nodes to flow //context to share types adn functions from nodes to flow
@ -43,6 +44,7 @@ export function TypesProvider({ children }: { children: ReactNode }) {
const [fetchError, setFetchError] = useState(false); const [fetchError, setFetchError] = useState(false);
const { setLoading } = useContext(alertContext); const { setLoading } = useContext(alertContext);
const { getAuthentication } = useContext(AuthContext); const { getAuthentication } = useContext(AuthContext);
const { takeSnapshot } = useContext(undoRedoContext);
const [getFilterEdge, setFilterEdge] = useState([]); const [getFilterEdge, setFilterEdge] = useState([]);
useEffect(() => { useEffect(() => {
@ -98,31 +100,27 @@ export function TypesProvider({ children }: { children: ReactNode }) {
} }
function deleteNode(idx: string | Array<string>) { function deleteNode(idx: string | Array<string>) {
reactFlowInstance!.setNodes( takeSnapshot();
reactFlowInstance! if (reactFlowInstance === null) return;
.getNodes() const edges = reactFlowInstance!
.filter((node: Node) => .getEdges()
typeof idx === "string" ? node.id !== idx : !idx.includes(node.id) .filter((edge) =>
) typeof idx === "string"
); ? edge.source == idx || edge.target == idx
reactFlowInstance!.setEdges( : idx.includes(edge.source) || idx.includes(edge.target)
reactFlowInstance! );
.getEdges() reactFlowInstance!.deleteElements({
.filter((edge) => nodes:
typeof idx === "string" typeof idx === "string" ? [{ id: idx }] : idx.map((id) => ({ id })),
? edge.source !== idx && edge.target !== idx edges,
: !idx.includes(edge.source) && !idx.includes(edge.target) });
)
);
} }
function deleteEdge(idx: string | Array<string>) { function deleteEdge(idx: string | Array<string>) {
reactFlowInstance!.setEdges( takeSnapshot();
reactFlowInstance! reactFlowInstance!.deleteElements({
.getEdges() edges:
.filter((edge: Edge) => typeof idx === "string" ? [{ id: idx }] : idx.map((id) => ({ id })),
typeof idx === "string" ? edge.id !== idx : !idx.includes(edge.id) });
)
);
} }
return ( return (

View file

@ -13,7 +13,6 @@ import ReactFlow, {
Controls, Controls,
Edge, Edge,
EdgeChange, EdgeChange,
Node,
NodeChange, NodeChange,
NodeDragHandler, NodeDragHandler,
OnEdgesDelete, OnEdgesDelete,
@ -377,21 +376,6 @@ export default function Page({
}; };
}, []); }, []);
const onDelete = useCallback(
(mynodes: Node[]) => {
takeSnapshot();
setEdges(
edges.filter(
(edge) =>
!mynodes.some(
(node) => edge.source === node.id || edge.target === node.id
)
)
);
},
[takeSnapshot, edges, setEdges]
);
const onEdgeUpdateStart = useCallback(() => { const onEdgeUpdateStart = useCallback(() => {
edgeUpdateSuccessful.current = false; edgeUpdateSuccessful.current = false;
}, []); }, []);
@ -482,7 +466,6 @@ export default function Page({
onDragOver={onDragOver} onDragOver={onDragOver}
onDrop={onDrop} onDrop={onDrop}
onSelectionChange={onSelectionChange} onSelectionChange={onSelectionChange}
onNodesDelete={onDelete}
deleteKeyCode={[]} deleteKeyCode={[]}
className="theme-attribution" className="theme-attribution"
minZoom={0.01} minZoom={0.01}