From 7e1d361c90bec2512717b1d08a87f36e22a11ba4 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Wed, 19 Apr 2023 23:19:17 -0300 Subject: [PATCH] templates from backend stored in context --- src/frontend/src/contexts/index.tsx | 15 ++++++++------- src/frontend/src/contexts/tabsContext.tsx | 8 ++++++-- src/frontend/src/contexts/templatesContext.tsx | 3 +-- .../components/extraSidebarComponent/index.tsx | 15 +++++++++++++-- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/frontend/src/contexts/index.tsx b/src/frontend/src/contexts/index.tsx index 310606ea5..d2ad1f06f 100644 --- a/src/frontend/src/contexts/index.tsx +++ b/src/frontend/src/contexts/index.tsx @@ -5,6 +5,7 @@ import { LocationProvider } from "./locationContext"; import PopUpProvider from "./popUpContext"; import { TabsProvider } from "./tabsContext"; import { TypesProvider } from "./typesContext"; +import { TemplatesProvider } from "./templatesContext"; export default function ContextWrapper({ children }: { children: ReactNode }) { //element to wrap all context @@ -13,13 +14,13 @@ export default function ContextWrapper({ children }: { children: ReactNode }) { - - - - {children} - - - + + + + {children} + + + diff --git a/src/frontend/src/contexts/tabsContext.tsx b/src/frontend/src/contexts/tabsContext.tsx index a11f0339d..a2bb79081 100644 --- a/src/frontend/src/contexts/tabsContext.tsx +++ b/src/frontend/src/contexts/tabsContext.tsx @@ -3,6 +3,7 @@ import { FlowType } from "../types/flow"; import { TabsContextType } from "../types/tabs"; import { normalCaseToSnakeCase } from "../utils"; import { alertContext } from "./alertContext"; +import { TemplatesContext } from "./templatesContext"; const TabsContextInitialValue: TabsContextType = { save:()=>{}, @@ -30,6 +31,7 @@ export function TabsProvider({ children }: { children: ReactNode }) { const [flows, setFlows] = useState>([]); const [id, setId] = useState(0); const [lockChat, setLockChat] = useState(false); + const {templates} = useContext(TemplatesContext) const newNodeId = useRef(0); function incrementNodeId() { @@ -53,14 +55,16 @@ export function TabsProvider({ children }: { children: ReactNode }) { useEffect(() => { //get tabs locally saved let cookie = window.localStorage.getItem("tabsData"); - if (cookie) { + if (cookie && Object.keys(templates).length>0) { + console.log(templates) + console.log(Object.keys(templates).length) let cookieObject = JSON.parse(cookie); setTabIndex(cookieObject.tabIndex); setFlows(cookieObject.flows); setId(cookieObject.id); newNodeId.current = cookieObject.nodeId; } - }, []); + }, [templates]); function hardReset(){ newNodeId.current=0; setTabIndex(0);setFlows([]);setId(0); diff --git a/src/frontend/src/contexts/templatesContext.tsx b/src/frontend/src/contexts/templatesContext.tsx index f8727f459..44e8a3463 100644 --- a/src/frontend/src/contexts/templatesContext.tsx +++ b/src/frontend/src/contexts/templatesContext.tsx @@ -1,5 +1,4 @@ import { createContext, ReactNode, useState } from "react"; -import { Node } from "reactflow"; import { TemplateContextType } from "../types/templatesContext"; //context to share types adn functions from nodes to flow @@ -11,7 +10,7 @@ const initialValue: TemplateContextType = { export const TemplatesContext = createContext(initialValue); -export function TypesProvider({ children }: { children: ReactNode }) { +export function TemplatesProvider({ children }: { children: ReactNode }) { const [templates, setTemplates] = useState({}); return ( diff --git a/src/frontend/src/pages/FlowPage/components/extraSidebarComponent/index.tsx b/src/frontend/src/pages/FlowPage/components/extraSidebarComponent/index.tsx index 0eb3876f1..5130a5ee2 100644 --- a/src/frontend/src/pages/FlowPage/components/extraSidebarComponent/index.tsx +++ b/src/frontend/src/pages/FlowPage/components/extraSidebarComponent/index.tsx @@ -9,10 +9,12 @@ import { APIKindType, APIObjectType, } from "../../../../types/api"; +import { TemplatesContext } from "../../../../contexts/templatesContext"; export default function ExtraSidebar() { const [data, setData] = useState({}); const { setTypes } = useContext(typesContext); + const { setTemplates } = useContext(TemplatesContext); useEffect(() => { async function getTypes(): Promise { @@ -21,7 +23,14 @@ export default function ExtraSidebar() { // Update the state of the component with the retrieved data. setData(result.data); - + setTemplates( + Object.keys(result.data).reduce((acc, curr) => { + Object.keys(result.data[curr]).forEach((c: keyof APIKindType)=>{ + acc[c] = result.data[curr][c] + }) + return acc; + },{}) + ); // Set the types by reducing over the keys of the result data and updating the accumulator. setTypes( Object.keys(result.data).reduce((acc, curr) => { @@ -84,7 +93,9 @@ export default function ExtraSidebar() { ))} - {Object.keys(data[d]).length===0 &&
Coming soon
} + {Object.keys(data[d]).length === 0 && ( +
Coming soon
+ )} ))}