From 1a5a6ba35b9d55edc2989b0a49b98720d8afdd51 Mon Sep 17 00:00:00 2001 From: cristhianzl Date: Tue, 20 Feb 2024 19:52:43 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(api.tsx):=20import=20missing?= =?UTF-8?q?=20BuildStatus=20enum=20from=20constants/enums=20to=20fix=20com?= =?UTF-8?q?pilation=20error=20=E2=9C=A8=20feat(api.tsx):=20add=20support?= =?UTF-8?q?=20for=20renewing=20access=20token=20and=20handling=20authentic?= =?UTF-8?q?ation=20errors=20to=20improve=20user=20experience=20=E2=9C=A8?= =?UTF-8?q?=20feat(api.tsx):=20add=20support=20for=20updating=20build=20st?= =?UTF-8?q?atus=20of=20vertices=20to=20keep=20track=20of=20build=20progres?= =?UTF-8?q?s=20=F0=9F=90=9B=20fix(flowStore.ts):=20add=20missing=20updateV?= =?UTF-8?q?erticesBuild=20function=20to=20update=20the=20list=20of=20verti?= =?UTF-8?q?ces=20being=20built=20=F0=9F=90=9B=20fix(flowStore.ts):=20initi?= =?UTF-8?q?alize=20verticesBuild=20array=20to=20an=20empty=20array=20to=20?= =?UTF-8?q?avoid=20undefined=20errors=20=F0=9F=90=9B=20fix(flowStore.ts):?= =?UTF-8?q?=20fix=20typo=20in=20updateVerticesBuild=20function=20name=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(flowStore.ts):=20fix=20typo=20in=20updateBui?= =?UTF-8?q?ldStatus=20function=20parameter=20name=20=F0=9F=90=9B=20fix(flo?= =?UTF-8?q?wStore.ts):=20fix=20typo=20in=20updateBuildStatus=20function=20?= =?UTF-8?q?parameter=20type=20=E2=9C=A8=20feat(flowStore.ts):=20add=20vert?= =?UTF-8?q?icesBuild=20property=20to=20store=20the=20list=20of=20vertices?= =?UTF-8?q?=20being=20built=20=F0=9F=90=9B=20fix(index.ts):=20fix=20typo?= =?UTF-8?q?=20in=20updateBuildStatus=20function=20parameter=20name=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(index.ts):=20fix=20typo=20in=20updateBuildSt?= =?UTF-8?q?atus=20function=20parameter=20type=20=E2=9C=A8=20feat(index.ts)?= =?UTF-8?q?:=20add=20updateVerticesBuild=20function=20to=20update=20the=20?= =?UTF-8?q?list=20of=20vertices=20being=20built=20=F0=9F=90=9B=20fix(build?= =?UTF-8?q?Utils.ts):=20update=20build=20status=20and=20vertices=20build?= =?UTF-8?q?=20list=20when=20building=20vertices=20to=20keep=20track=20of?= =?UTF-8?q?=20build=20progress?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/controllers/API/api.tsx | 76 +++++++++++--------- src/frontend/src/stores/flowStore.ts | 4 ++ src/frontend/src/types/zustand/flow/index.ts | 2 + src/frontend/src/utils/buildUtils.ts | 1 + 4 files changed, 51 insertions(+), 32 deletions(-) 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 = [];