Removed useEffects from flowsContext

This commit is contained in:
Lucas Oliveira 2024-01-05 12:41:45 -03:00
commit df12f33e86
6 changed files with 34 additions and 22 deletions

View file

@ -19,6 +19,9 @@ import { alertContext } from "./contexts/alertContext";
import { locationContext } from "./contexts/locationContext"; import { locationContext } from "./contexts/locationContext";
import { typesContext } from "./contexts/typesContext"; import { typesContext } from "./contexts/typesContext";
import Router from "./routes"; import Router from "./routes";
import { AuthContext } from "./contexts/authContext";
import { FlowsContext } from "./contexts/flowsContext";
import { getVersion } from "./controllers/API";
export default function App() { export default function App() {
let { setCurrent, setShowSideBar, setIsStackedOpen } = let { setCurrent, setShowSideBar, setIsStackedOpen } =
@ -42,7 +45,7 @@ export default function App() {
setSuccessOpen, setSuccessOpen,
loading, loading,
} = useContext(alertContext); } = useContext(alertContext);
const navigate = useNavigate();
const { fetchError } = useContext(typesContext); const { fetchError } = useContext(typesContext);
// Initialize state variable for the list of alerts // 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 ( return (
//need parent component with width and height //need parent component with width and height
<div className="flex h-full flex-col"> <div className="flex h-full flex-col">

View file

@ -5,7 +5,6 @@ import {
createContext, createContext,
useCallback, useCallback,
useContext, useContext,
useEffect,
useRef, useRef,
useState, useState,
} from "react"; } from "react";
@ -72,6 +71,7 @@ const FlowsContextInitialValue: FlowsContextType = {
setTabId: (index: string) => {}, setTabId: (index: string) => {},
isLoading: true, isLoading: true,
flows: [], flows: [],
setVersion: () => {},
removeFlow: (id: string) => {}, removeFlow: (id: string) => {},
addFlow: async ( addFlow: async (
newProject: boolean, newProject: boolean,
@ -88,6 +88,7 @@ const FlowsContextInitialValue: FlowsContextType = {
saveComponent: async (component: NodeDataType, override: boolean) => "", saveComponent: async (component: NodeDataType, override: boolean) => "",
deleteComponent: (key: string) => {}, deleteComponent: (key: string) => {},
version: "", version: "",
refreshFlows: () => {},
}; };
export const FlowsContext = createContext<FlowsContextType>( export const FlowsContext = createContext<FlowsContextType>(
@ -101,8 +102,8 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
const [tabId, setTabId] = useState(""); const [tabId, setTabId] = useState("");
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [flows, setFlows] = useState<Array<FlowType>>([]); const [flows, setFlows] = useState<Array<FlowType>>([]);
const { setData } = useContext(typesContext);
const [tabsState, setTabsState] = useState<FlowsState>({}); const [tabsState, setTabsState] = useState<FlowsState>({});
const {setData} = useContext(typesContext);
const nodes = useFlowStore((state) => state.nodes); const nodes = useFlowStore((state) => state.nodes);
const edges = useFlowStore((state) => state.edges); 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() { function getTabsDataFromDB() {
//get tabs from db //get tabs from db
return readFlowsFromDatabase(); return readFlowsFromDatabase();
@ -386,7 +379,6 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
// updateNodes(data.nodes, data.edges); // updateNodes(data.nodes, data.edges);
if (refreshIds) updateIds(data); // Assuming updateIds is defined elsewhere if (refreshIds) updateIds(data); // Assuming updateIds is defined elsewhere
} }
return data; return data;
}; };
@ -506,17 +498,13 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
// Initialize state variable for the version // Initialize state variable for the version
const [version, setVersion] = useState(""); const [version, setVersion] = useState("");
useEffect(() => {
getVersion().then((data) => {
setVersion(data.version);
});
}, []);
return ( return (
<FlowsContext.Provider <FlowsContext.Provider
value={{ value={{
version, version,
setVersion,
flows, flows,
saveFlow, saveFlow,
tabId, tabId,
@ -529,6 +517,7 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
uploadFlow, uploadFlow,
tabsState, tabsState,
setTabsState, setTabsState,
refreshFlows,
isLoading, isLoading,
saveComponent, saveComponent,
deleteComponent, deleteComponent,

View file

@ -297,8 +297,8 @@ export async function saveFlowStyleToDatabase(flowStyle: FlowStyleType) {
* @returns {Promise<AxiosResponse<any>>} A promise that resolves to an AxiosResponse containing the version information. * @returns {Promise<AxiosResponse<any>>} A promise that resolves to an AxiosResponse containing the version information.
*/ */
export async function getVersion() { export async function getVersion() {
const respnose = await api.get(`${BASE_URL_API}version`); const response = await api.get(`${BASE_URL_API}version`);
return respnose.data; return response.data;
} }
/** /**

View file

@ -1,9 +1,11 @@
import { create } from "zustand"; import { create } from "zustand";
import { FlowsContextType } from "../types/tabs";
import { FlowsManagerStoreType } from "../types/zustand/flowsManager"; import { FlowsManagerStoreType } from "../types/zustand/flowsManager";
const useFlowStore = create<FlowsManagerStoreType>((set, get) => ({ let currentFlowId: string = "";
const useFlowsManagerStore = create<FlowsManagerStoreType>((set, get) => ({
flows: [], flows: [],
currentFlow: get().flows[currentFlowId],
})); }));
export default useFlowStore; export default useFlowsManagerStore;

View file

@ -13,6 +13,7 @@ export type FlowsContextType = {
setTabId: (index: string) => void; setTabId: (index: string) => void;
//keep //keep
removeFlow: (id: string) => void; removeFlow: (id: string) => void;
refreshFlows: () => void;
//keep //keep
addFlow: ( addFlow: (
newProject: boolean, newProject: boolean,
@ -29,6 +30,7 @@ export type FlowsContextType = {
downloadFlows: () => void; downloadFlows: () => void;
//keep //keep
uploadFlows: () => void; uploadFlows: () => void;
setVersion: (version: string) => void;
uploadFlow: ({ uploadFlow: ({
newProject, newProject,
file, file,

View file

@ -4,4 +4,5 @@ import { FlowsState } from "../../tabs";
export type FlowsManagerStoreType = { export type FlowsManagerStoreType = {
flows: Array<FlowType>; flows: Array<FlowType>;
currentFlow: FlowType | undefined;
}; };