diff --git a/src/frontend/src/controllers/API/api.tsx b/src/frontend/src/controllers/API/api.tsx index c6786cc1c..b659333a1 100644 --- a/src/frontend/src/controllers/API/api.tsx +++ b/src/frontend/src/controllers/API/api.tsx @@ -3,8 +3,10 @@ import { useContext, useEffect } from "react"; import { Cookies } from "react-cookie"; import { useNavigate } from "react-router-dom"; import { renewAccessToken } from "."; +import { BuildStatus } from "../../constants/enums"; import { AuthContext } from "../../contexts/authContext"; import useAlertStore from "../../stores/alertStore"; +import useFlowStore from "../../stores/flowStore"; // Create a new Axios instance const api: AxiosInstance = axios.create({ @@ -24,45 +26,20 @@ function ApiInterceptor() { async (error: AxiosError) => { if (error.response?.status === 401) { const accessToken = cookies.get("access_token_lf"); + if (accessToken && !autoLogin) { - authenticationErrorCount = authenticationErrorCount + 1; - if (authenticationErrorCount > 3) { - authenticationErrorCount = 0; - logout(); - } - try { - const res = await renewAccessToken(); - if (res?.data?.access_token && res?.data?.refresh_token) { - login(res?.data?.access_token); - } - if (error?.config?.headers) { - delete error.config.headers["Authorization"]; - error.config.headers["Authorization"] = `Bearer ${cookies.get( - "access_token_lf" - )}`; - const response = await axios.request(error.config); - return response; - } - } catch (error) { - if (axios.isAxiosError(error) && error.response?.status === 401) { - logout(); - } else { - console.error(error); - logout(); - } - } + checkErrorCount(); + await tryToRenewAccessToken(error); } if (!accessToken && error?.config?.url?.includes("login")) { return Promise.reject(error); - } else { - logout(); } - } else { - // if (URL_EXCLUDED_FROM_ERROR_RETRIES.includes(error.config?.url)) { - return Promise.reject(error); - // } + + return logout(); } + await clearBuildVerticesState(error); + return Promise.reject(error); } ); @@ -115,6 +92,41 @@ function ApiInterceptor() { }; }, [accessToken, setErrorData]); + function checkErrorCount() { + authenticationErrorCount = authenticationErrorCount + 1; + + if (authenticationErrorCount > 3) { + authenticationErrorCount = 0; + logout(); + } + } + + async function tryToRenewAccessToken(error: AxiosError) { + try { + const res = await renewAccessToken(); + if (res?.data?.access_token && res?.data?.refresh_token) { + login(res?.data?.access_token); + } + if (error?.config?.headers) { + delete error.config.headers["Authorization"]; + error.config.headers["Authorization"] = `Bearer ${cookies.get( + "access_token_lf" + )}`; + const response = await axios.request(error.config); + return response; + } + } catch (error) { + logout(); + } + } + + async function clearBuildVerticesState(error) { + if (error?.response?.status === 500) { + const vertices = useFlowStore.getState().verticesBuild; + useFlowStore.getState().updateBuildStatus(vertices, BuildStatus.BUILT); + } + } + return null; } diff --git a/src/frontend/src/stores/flowStore.ts b/src/frontend/src/stores/flowStore.ts index ea424d6fb..398b2b3f3 100644 --- a/src/frontend/src/stores/flowStore.ts +++ b/src/frontend/src/stores/flowStore.ts @@ -431,6 +431,10 @@ const useFlowStore = create((set, get) => ({ } }); }, + updateVerticesBuild: (vertices: string[]) => { + set({ verticesBuild: vertices }); + }, + verticesBuild: [], })); export default useFlowStore; diff --git a/src/frontend/src/types/zustand/flow/index.ts b/src/frontend/src/types/zustand/flow/index.ts index 429379155..f0bad1578 100644 --- a/src/frontend/src/types/zustand/flow/index.ts +++ b/src/frontend/src/types/zustand/flow/index.ts @@ -87,4 +87,6 @@ export type FlowStoreType = { buildFlow: (nodeId?: string) => Promise; getFlow: () => { nodes: Node[]; edges: Edge[]; viewport: Viewport }; updateBuildStatus: (nodeId: string[], status: BuildStatus) => void; + updateVerticesBuild: (vertices: string[]) => void; + verticesBuild: string[]; }; diff --git a/src/frontend/src/utils/buildUtils.ts b/src/frontend/src/utils/buildUtils.ts index 8e91e9bac..638d35886 100644 --- a/src/frontend/src/utils/buildUtils.ts +++ b/src/frontend/src/utils/buildUtils.ts @@ -47,6 +47,7 @@ export async function buildVertices({ const verticesIds = vertices.flatMap((v) => v); useFlowStore.getState().updateBuildStatus(verticesIds, BuildStatus.TO_BUILD); + useFlowStore.getState().updateVerticesBuild(verticesIds); // Set each vertex state to building const buildResults: Array = [];