refactor typesContext

This commit is contained in:
anovazzi1 2023-02-27 14:25:39 -03:00
commit 019af743b7

View file

@ -1,37 +1,44 @@
import { ReactEventHandler, createContext, useState } from "react"; import { createContext, useState } from "react";
import { ReactFlowInstance } from "reactflow"; import { ReactFlowInstance } from "reactflow";
type typesContextType= //context to share types adn functions from nodes to flow
{ type typesContextType = {
reactFlowInstance: ReactFlowInstance; reactFlowInstance: ReactFlowInstance;
setReactFlowInstance: any; setReactFlowInstance: any;
deleteNode:(idx:number)=>void; deleteNode: (idx: number) => void;
types: {}; types: {};
setTypes:(newState:{})=>void; setTypes: (newState: {}) => void;
};
} const initialValue = {
const initialValue= {
reactFlowInstance: null, reactFlowInstance: null,
setReactFlowInstance: ()=>{}, setReactFlowInstance: () => {},
deleteNode: ()=>{}, deleteNode: () => {},
types: {}, types: {},
setTypes:()=>{}, setTypes: () => {},
} };
export const typesContext = createContext<typesContextType>(initialValue); export const typesContext = createContext<typesContextType>(initialValue);
export function TypesProvider({children}){ export function TypesProvider({ children }) {
const [types, setTypes] = useState({}); const [types, setTypes] = useState({});
const [reactFlowInstance, setReactFlowInstance] = useState(null); const [reactFlowInstance, setReactFlowInstance] = useState(null);
function deleteNode(idx){ function deleteNode(idx) {
reactFlowInstance.setNodes( reactFlowInstance.setNodes(
reactFlowInstance.getNodes().filter((n) => n.id !== idx) reactFlowInstance.getNodes().filter((n) => n.id !== idx)
); );
} }
return ( return (
<typesContext.Provider value={{ types, setTypes, reactFlowInstance, setReactFlowInstance, deleteNode}}> <typesContext.Provider
value={{
types,
setTypes,
reactFlowInstance,
setReactFlowInstance,
deleteNode,
}}
>
{children} {children}
</typesContext.Provider> </typesContext.Provider>
) );
} }