From fc9a9dfa3efe2a0653eda3d86927bd1559755dfb Mon Sep 17 00:00:00 2001 From: Eduard Gert Date: Tue, 13 Feb 2024 14:08:43 +0100 Subject: [PATCH] Block application and show loading until users are fetched (#330) * Add option to ignore errors * Block application and show loading until users are fetched --- src/contexts/ApplicationProvider.tsx | 13 ++++++++++--- src/utils/api.tsx | 23 ++++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/contexts/ApplicationProvider.tsx b/src/contexts/ApplicationProvider.tsx index 03ae573..c358db7 100644 --- a/src/contexts/ApplicationProvider.tsx +++ b/src/contexts/ApplicationProvider.tsx @@ -1,4 +1,5 @@ import { useOidcUser } from "@axa-fr/react-oidc"; +import FullScreenLoading from "@components/ui/FullScreenLoading"; import { useApiCall } from "@utils/api"; import { useIsMd } from "@utils/responsive"; import { getLatestNetbirdRelease } from "@utils/version"; @@ -28,10 +29,14 @@ export default function ApplicationProvider({ children }: Props) { const { oidcUser: user } = useOidcUser(); const [mobileNavOpen, setMobileNavOpen] = useState(false); const isMd = useIsMd(); - const userRequest = useApiCall("/users"); + const userRequest = useApiCall("/users", true); + const [show, setShow] = useState(false); useEffect(() => { - userRequest.get().then(); + userRequest + .get() + .then(() => setShow(true)) + .catch(() => setShow(true)); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); @@ -66,12 +71,14 @@ export default function ApplicationProvider({ children }: Props) { setMobileNavOpen(!mobileNavOpen); }; - return ( + return show ? ( {children} + ) : ( + ); } diff --git a/src/utils/api.tsx b/src/utils/api.tsx index 16a3c1c..388047f 100644 --- a/src/utils/api.tsx +++ b/src/utils/api.tsx @@ -38,12 +38,12 @@ async function apiRequest( return (await res.json()) as T; } -export function useNetBirdFetch() { +export function useNetBirdFetch(ignoreError: boolean = false) { const tokenSource = config.tokenSource || "accessToken"; const { idToken } = useOidcIdToken(); const { accessToken } = useOidcAccessToken(); const token = tokenSource.toLowerCase() == "idtoken" ? idToken : accessToken; - const handleErrors = useApiErrorHandling(); + const handleErrors = useApiErrorHandling(ignoreError); const isTokenExpired = async () => { let attempts = 20; @@ -77,9 +77,9 @@ export function useNetBirdFetch() { }; } -export default function useFetchApi(url: string) { - const { fetch } = useNetBirdFetch(); - const handleErrors = useApiErrorHandling(); +export default function useFetchApi(url: string, ignoreError = false) { + const { fetch } = useNetBirdFetch(ignoreError); + const handleErrors = useApiErrorHandling(ignoreError); const { data, error, isLoading, isValidating, mutate } = useSWR( url, @@ -102,9 +102,9 @@ export default function useFetchApi(url: string) { } as const; } -export function useApiCall(url: string) { - const { fetch } = useNetBirdFetch(); - const handleErrors = useApiErrorHandling(); +export function useApiCall(url: string, ignoreError = false) { + const { fetch } = useNetBirdFetch(ignoreError); + const handleErrors = useApiErrorHandling(ignoreError); return { post: async (data: any, suffix = "") => { @@ -130,10 +130,15 @@ export function useApiCall(url: string) { }; } -export function useApiErrorHandling() { +export function useApiErrorHandling(ignoreError = false) { const { login } = useOidc(); const currentPath = usePathname(); const { setError } = useErrorBoundary(); + if (ignoreError) + return (err: ErrorResponse) => { + console.log(err); + return Promise.reject(err); + }; return (err: ErrorResponse) => { if (err.code == 401 && err.message == "no valid authentication provided") {