This commit is contained in:
Gabriel Almeida 2023-05-19 10:06:59 -03:00
commit 0d43252478

View file

@ -7,83 +7,83 @@ import { APIKindType } from "../types/api";
//context to share types adn functions from nodes to flow //context to share types adn functions from nodes to flow
const initialValue: typesContextType = { const initialValue: typesContextType = {
reactFlowInstance: null, reactFlowInstance: null,
setReactFlowInstance: () => {}, setReactFlowInstance: () => {},
deleteNode: () => {}, deleteNode: () => {},
types: {}, types: {},
setTypes: () => {}, setTypes: () => {},
templates: {}, templates: {},
setTemplates: () => {}, setTemplates: () => {},
data: {}, data: {},
setData: () => {}, setData: () => {},
}; };
export const typesContext = createContext<typesContextType>(initialValue); export const typesContext = createContext<typesContextType>(initialValue);
export function TypesProvider({ children }: { children: ReactNode }) { export function TypesProvider({ children }: { children: ReactNode }) {
const [types, setTypes] = useState({}); const [types, setTypes] = useState({});
const [reactFlowInstance, setReactFlowInstance] = useState(null); const [reactFlowInstance, setReactFlowInstance] = useState(null);
const [templates, setTemplates] = useState({}); const [templates, setTemplates] = useState({});
const [data, setData] = useState({}); const [data, setData] = useState({});
useEffect(() => { useEffect(() => {
async function getTypes(): Promise<void> { async function getTypes(): Promise<void> {
// Make an asynchronous API call to retrieve all data. // Make an asynchronous API call to retrieve all data.
let result = await getAll(); let result = await getAll();
// Update the state of the component with the retrieved data. // Update the state of the component with the retrieved data.
setData(result.data); setData(result.data);
setTemplates( setTemplates(
Object.keys(result.data).reduce((acc, curr) => { Object.keys(result.data).reduce((acc, curr) => {
Object.keys(result.data[curr]).forEach((c: keyof APIKindType) => { Object.keys(result.data[curr]).forEach((c: keyof APIKindType) => {
acc[c] = result.data[curr][c]; acc[c] = result.data[curr][c];
}); });
return acc; return acc;
}, {}) }, {})
); );
// Set the types by reducing over the keys of the result data and updating the accumulator. // Set the types by reducing over the keys of the result data and updating the accumulator.
setTypes( setTypes(
Object.keys(result.data).reduce((acc, curr) => { Object.keys(result.data).reduce((acc, curr) => {
Object.keys(result.data[curr]).forEach((c: keyof APIKindType) => { Object.keys(result.data[curr]).forEach((c: keyof APIKindType) => {
acc[c] = curr; acc[c] = curr;
// Add the base classes to the accumulator as well. // Add the base classes to the accumulator as well.
result.data[curr][c].base_classes?.forEach((b) => { result.data[curr][c].base_classes?.forEach((b) => {
acc[b] = curr; acc[b] = curr;
}); });
}); });
return acc; return acc;
}, {}) }, {})
); );
} }
// Call the getTypes function. // Call the getTypes function.
getTypes(); getTypes();
}, [setTypes]); }, [setTypes]);
function deleteNode(idx: string) { function deleteNode(idx: string) {
reactFlowInstance.setNodes( reactFlowInstance.setNodes(
reactFlowInstance.getNodes().filter((n: Node) => n.id !== idx) reactFlowInstance.getNodes().filter((n: Node) => n.id !== idx)
); );
reactFlowInstance.setEdges( reactFlowInstance.setEdges(
reactFlowInstance reactFlowInstance
.getEdges() .getEdges()
.filter((ns) => ns.source !== idx && ns.target !== idx) .filter((ns) => ns.source !== idx && ns.target !== idx)
); );
} }
return ( return (
<typesContext.Provider <typesContext.Provider
value={{ value={{
types, types,
setTypes, setTypes,
reactFlowInstance, reactFlowInstance,
setReactFlowInstance, setReactFlowInstance,
deleteNode, deleteNode,
setTemplates, setTemplates,
templates, templates,
data, data,
setData, setData,
}} }}
> >
{children} {children}
</typesContext.Provider> </typesContext.Provider>
); );
} }