Merge branch 'cz/fixFetchData' into dev
This commit is contained in:
commit
69887a6433
3 changed files with 46 additions and 48 deletions
|
|
@ -1,9 +1,8 @@
|
||||||
import { useContext, useEffect, useState } from "react";
|
import { useContext, useEffect, useState } from "react";
|
||||||
import "reactflow/dist/style.css";
|
|
||||||
import "./App.css";
|
|
||||||
|
|
||||||
import { ErrorBoundary } from "react-error-boundary";
|
import { ErrorBoundary } from "react-error-boundary";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import "reactflow/dist/style.css";
|
||||||
|
import "./App.css";
|
||||||
import ErrorAlert from "./alerts/error";
|
import ErrorAlert from "./alerts/error";
|
||||||
import NoticeAlert from "./alerts/notice";
|
import NoticeAlert from "./alerts/notice";
|
||||||
import SuccessAlert from "./alerts/success";
|
import SuccessAlert from "./alerts/success";
|
||||||
|
|
@ -15,7 +14,7 @@ import {
|
||||||
FETCH_ERROR_MESSAGE,
|
FETCH_ERROR_MESSAGE,
|
||||||
} from "./constants/constants";
|
} from "./constants/constants";
|
||||||
import { AuthContext } from "./contexts/authContext";
|
import { AuthContext } from "./contexts/authContext";
|
||||||
import { getGlobalVariables, getHealth } from "./controllers/API";
|
import { autoLogin, getGlobalVariables, getHealth } from "./controllers/API";
|
||||||
import Router from "./routes";
|
import Router from "./routes";
|
||||||
import useAlertStore from "./stores/alertStore";
|
import useAlertStore from "./stores/alertStore";
|
||||||
import { useDarkStore } from "./stores/darkStore";
|
import { useDarkStore } from "./stores/darkStore";
|
||||||
|
|
@ -38,8 +37,10 @@ export default function App() {
|
||||||
removeFromTempNotificationList(id);
|
removeFromTempNotificationList(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
const { isAuthenticated } = useContext(AuthContext);
|
const { isAuthenticated, login, setUserData, setAutoLogin, getUser } =
|
||||||
|
useContext(AuthContext);
|
||||||
const refreshFlows = useFlowsManagerStore((state) => state.refreshFlows);
|
const refreshFlows = useFlowsManagerStore((state) => state.refreshFlows);
|
||||||
|
const setLoading = useAlertStore((state) => state.setLoading);
|
||||||
const fetchApiData = useStoreStore((state) => state.fetchApiData);
|
const fetchApiData = useStoreStore((state) => state.fetchApiData);
|
||||||
const getTypes = useTypesStore((state) => state.getTypes);
|
const getTypes = useTypesStore((state) => state.getTypes);
|
||||||
const refreshVersion = useDarkStore((state) => state.refreshVersion);
|
const refreshVersion = useDarkStore((state) => state.refreshVersion);
|
||||||
|
|
@ -53,23 +54,46 @@ export default function App() {
|
||||||
const [isLoadingHealth, setIsLoadingHealth] = useState(false);
|
const [isLoadingHealth, setIsLoadingHealth] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
refreshStars();
|
const isLoginPage = location.pathname.includes("login");
|
||||||
refreshVersion();
|
|
||||||
|
|
||||||
// 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.
|
autoLogin()
|
||||||
if (isAuthenticated === true) {
|
.then(async (user) => {
|
||||||
// get data from db
|
if (user && user["access_token"]) {
|
||||||
getTypes().then(() => {
|
user["refresh_token"] = "auto";
|
||||||
refreshFlows();
|
login(user["access_token"]);
|
||||||
|
setUserData(user);
|
||||||
|
setAutoLogin(true);
|
||||||
|
setLoading(false);
|
||||||
|
await Promise.all([refreshStars(), refreshVersion(), fetchData()]);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(async () => {
|
||||||
|
setAutoLogin(false);
|
||||||
|
if (isAuthenticated && !isLoginPage) {
|
||||||
|
getUser();
|
||||||
|
await Promise.all([refreshStars(), refreshVersion(), fetchData()]);
|
||||||
|
} else {
|
||||||
|
setLoading(false);
|
||||||
|
useFlowsManagerStore.setState({ isLoading: false });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
getGlobalVariables().then((res) => {
|
|
||||||
setGlobalVariables(res);
|
|
||||||
});
|
|
||||||
checkHasStore();
|
|
||||||
fetchApiData();
|
|
||||||
}
|
|
||||||
}, [isAuthenticated]);
|
}, [isAuthenticated]);
|
||||||
|
|
||||||
|
const fetchData = async () => {
|
||||||
|
if (isAuthenticated) {
|
||||||
|
try {
|
||||||
|
await getTypes();
|
||||||
|
refreshFlows();
|
||||||
|
const res = await getGlobalVariables();
|
||||||
|
setGlobalVariables(res);
|
||||||
|
checkHasStore();
|
||||||
|
fetchApiData();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to fetch data:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
checkApplicationHealth();
|
checkApplicationHealth();
|
||||||
// Timer to call getHealth every 5 seconds
|
// Timer to call getHealth every 5 seconds
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,8 @@
|
||||||
import { createContext, useEffect, useState } from "react";
|
import { createContext, useEffect, useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import Cookies from "universal-cookie";
|
import Cookies from "universal-cookie";
|
||||||
import {
|
import { getLoggedUser, requestLogout } from "../controllers/API";
|
||||||
autoLogin as autoLoginApi,
|
|
||||||
getLoggedUser,
|
|
||||||
requestLogout,
|
|
||||||
} from "../controllers/API";
|
|
||||||
import useAlertStore from "../stores/alertStore";
|
import useAlertStore from "../stores/alertStore";
|
||||||
import useFlowsManagerStore from "../stores/flowsManagerStore";
|
|
||||||
import { Users } from "../types/api";
|
import { Users } from "../types/api";
|
||||||
import { AuthContextType } from "../types/contexts/auth";
|
import { AuthContextType } from "../types/contexts/auth";
|
||||||
|
|
||||||
|
|
@ -26,6 +21,7 @@ const initialValue: AuthContextType = {
|
||||||
setApiKey: () => {},
|
setApiKey: () => {},
|
||||||
apiKey: null,
|
apiKey: null,
|
||||||
storeApiKey: () => {},
|
storeApiKey: () => {},
|
||||||
|
getUser: () => {},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const AuthContext = createContext<AuthContextType>(initialValue);
|
export const AuthContext = createContext<AuthContextType>(initialValue);
|
||||||
|
|
@ -61,30 +57,6 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const isLoginPage = location.pathname.includes("login");
|
|
||||||
|
|
||||||
autoLoginApi()
|
|
||||||
.then((user) => {
|
|
||||||
if (user && user["access_token"]) {
|
|
||||||
user["refresh_token"] = "auto";
|
|
||||||
login(user["access_token"]);
|
|
||||||
setUserData(user);
|
|
||||||
setAutoLogin(true);
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
setAutoLogin(false);
|
|
||||||
if (isAuthenticated && !isLoginPage) {
|
|
||||||
getUser();
|
|
||||||
} else {
|
|
||||||
setLoading(false);
|
|
||||||
useFlowsManagerStore.setState({ isLoading: false });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, [autoLogin]);
|
|
||||||
|
|
||||||
function getUser() {
|
function getUser() {
|
||||||
getLoggedUser()
|
getLoggedUser()
|
||||||
.then((user) => {
|
.then((user) => {
|
||||||
|
|
@ -145,6 +117,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
setApiKey,
|
setApiKey,
|
||||||
apiKey,
|
apiKey,
|
||||||
storeApiKey,
|
storeApiKey,
|
||||||
|
getUser,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,5 @@ export type AuthContextType = {
|
||||||
apiKey: string | null;
|
apiKey: string | null;
|
||||||
setApiKey: (apiKey: string | null) => void;
|
setApiKey: (apiKey: string | null) => void;
|
||||||
storeApiKey: (apiKey: string) => void;
|
storeApiKey: (apiKey: string) => void;
|
||||||
|
getUser: () => void;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue