diff --git a/src/frontend/src/App.tsx b/src/frontend/src/App.tsx index 7ccb9d1c7..3dee06b4d 100644 --- a/src/frontend/src/App.tsx +++ b/src/frontend/src/App.tsx @@ -19,6 +19,9 @@ import { alertContext } from "./contexts/alertContext"; import { locationContext } from "./contexts/locationContext"; import { typesContext } from "./contexts/typesContext"; import Router from "./routes"; +import { AuthContext } from "./contexts/authContext"; +import { FlowsContext } from "./contexts/flowsContext"; +import { getVersion } from "./controllers/API"; export default function App() { let { setCurrent, setShowSideBar, setIsStackedOpen } = @@ -42,7 +45,7 @@ export default function App() { setSuccessOpen, loading, } = useContext(alertContext); - const navigate = useNavigate(); + const { fetchError } = useContext(typesContext); // Initialize state variable for the list of alerts @@ -129,6 +132,21 @@ export default function App() { ); }; + const { getAuthentication } = useContext(AuthContext); + const { refreshFlows, setVersion } = useContext(FlowsContext); + + useEffect(() => { + // If the user is authenticated, fetch the types. This code is important to check if the user is auth because of the execution order of the useEffect hooks. + if (getAuthentication() === true) { + // get data from db + refreshFlows(); + } + + getVersion().then((data) => { + setVersion(data.version); + }); + }, [getAuthentication()]); + return ( //need parent component with width and height
diff --git a/src/frontend/src/contexts/flowsContext.tsx b/src/frontend/src/contexts/flowsContext.tsx index 9d22f2d21..cbb1d4f54 100644 --- a/src/frontend/src/contexts/flowsContext.tsx +++ b/src/frontend/src/contexts/flowsContext.tsx @@ -5,7 +5,6 @@ import { createContext, useCallback, useContext, - useEffect, useRef, useState, } from "react"; @@ -72,6 +71,7 @@ const FlowsContextInitialValue: FlowsContextType = { setTabId: (index: string) => {}, isLoading: true, flows: [], + setVersion: () => {}, removeFlow: (id: string) => {}, addFlow: async ( newProject: boolean, @@ -88,6 +88,7 @@ const FlowsContextInitialValue: FlowsContextType = { saveComponent: async (component: NodeDataType, override: boolean) => "", deleteComponent: (key: string) => {}, version: "", + refreshFlows: () => {}, }; export const FlowsContext = createContext( @@ -101,8 +102,8 @@ export function FlowsProvider({ children }: { children: ReactNode }) { const [tabId, setTabId] = useState(""); const [isLoading, setIsLoading] = useState(false); const [flows, setFlows] = useState>([]); - const { setData } = useContext(typesContext); const [tabsState, setTabsState] = useState({}); + const {setData} = useContext(typesContext); const nodes = useFlowStore((state) => state.nodes); const edges = useFlowStore((state) => state.edges); @@ -123,14 +124,6 @@ export function FlowsProvider({ children }: { children: ReactNode }) { }); } - useEffect(() => { - // If the user is authenticated, fetch the types. This code is important to check if the user is auth because of the execution order of the useEffect hooks. - if (getAuthentication() === true) { - // get data from db - refreshFlows(); - } - }, [getAuthentication(), tabId]); - function getTabsDataFromDB() { //get tabs from db return readFlowsFromDatabase(); @@ -386,7 +379,6 @@ export function FlowsProvider({ children }: { children: ReactNode }) { // updateNodes(data.nodes, data.edges); if (refreshIds) updateIds(data); // Assuming updateIds is defined elsewhere } - return data; }; @@ -506,17 +498,13 @@ export function FlowsProvider({ children }: { children: ReactNode }) { // Initialize state variable for the version const [version, setVersion] = useState(""); - useEffect(() => { - getVersion().then((data) => { - setVersion(data.version); - }); - }, []); return ( >} A promise that resolves to an AxiosResponse containing the version information. */ export async function getVersion() { - const respnose = await api.get(`${BASE_URL_API}version`); - return respnose.data; + const response = await api.get(`${BASE_URL_API}version`); + return response.data; } /** diff --git a/src/frontend/src/stores/flowsManagerStore.ts b/src/frontend/src/stores/flowsManagerStore.ts index 9b07ca5cd..b8b282ffc 100644 --- a/src/frontend/src/stores/flowsManagerStore.ts +++ b/src/frontend/src/stores/flowsManagerStore.ts @@ -1,9 +1,11 @@ import { create } from "zustand"; -import { FlowsContextType } from "../types/tabs"; import { FlowsManagerStoreType } from "../types/zustand/flowsManager"; -const useFlowStore = create((set, get) => ({ +let currentFlowId: string = ""; + +const useFlowsManagerStore = create((set, get) => ({ flows: [], + currentFlow: get().flows[currentFlowId], })); -export default useFlowStore; \ No newline at end of file +export default useFlowsManagerStore; \ No newline at end of file diff --git a/src/frontend/src/types/tabs/index.ts b/src/frontend/src/types/tabs/index.ts index c5c0e680b..d98afd773 100644 --- a/src/frontend/src/types/tabs/index.ts +++ b/src/frontend/src/types/tabs/index.ts @@ -13,6 +13,7 @@ export type FlowsContextType = { setTabId: (index: string) => void; //keep removeFlow: (id: string) => void; + refreshFlows: () => void; //keep addFlow: ( newProject: boolean, @@ -29,6 +30,7 @@ export type FlowsContextType = { downloadFlows: () => void; //keep uploadFlows: () => void; + setVersion: (version: string) => void; uploadFlow: ({ newProject, file, diff --git a/src/frontend/src/types/zustand/flowsManager/index.ts b/src/frontend/src/types/zustand/flowsManager/index.ts index 07001c946..28bd5e32d 100644 --- a/src/frontend/src/types/zustand/flowsManager/index.ts +++ b/src/frontend/src/types/zustand/flowsManager/index.ts @@ -4,4 +4,5 @@ import { FlowsState } from "../../tabs"; export type FlowsManagerStoreType = { flows: Array; + currentFlow: FlowType | undefined; }; \ No newline at end of file