Block application and show loading until users are fetched (#330)

* Add option to ignore errors

* Block application and show loading until users are fetched
This commit is contained in:
Eduard Gert
2024-02-13 14:08:43 +01:00
committed by GitHub
parent 093efc08b3
commit fc9a9dfa3e
2 changed files with 24 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
import { useOidcUser } from "@axa-fr/react-oidc"; import { useOidcUser } from "@axa-fr/react-oidc";
import FullScreenLoading from "@components/ui/FullScreenLoading";
import { useApiCall } from "@utils/api"; import { useApiCall } from "@utils/api";
import { useIsMd } from "@utils/responsive"; import { useIsMd } from "@utils/responsive";
import { getLatestNetbirdRelease } from "@utils/version"; import { getLatestNetbirdRelease } from "@utils/version";
@@ -28,10 +29,14 @@ export default function ApplicationProvider({ children }: Props) {
const { oidcUser: user } = useOidcUser(); const { oidcUser: user } = useOidcUser();
const [mobileNavOpen, setMobileNavOpen] = useState(false); const [mobileNavOpen, setMobileNavOpen] = useState(false);
const isMd = useIsMd(); const isMd = useIsMd();
const userRequest = useApiCall<User[]>("/users"); const userRequest = useApiCall<User[]>("/users", true);
const [show, setShow] = useState(false);
useEffect(() => { useEffect(() => {
userRequest.get().then(); userRequest
.get()
.then(() => setShow(true))
.catch(() => setShow(true));
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, []);
@@ -66,12 +71,14 @@ export default function ApplicationProvider({ children }: Props) {
setMobileNavOpen(!mobileNavOpen); setMobileNavOpen(!mobileNavOpen);
}; };
return ( return show ? (
<ApplicationContext.Provider <ApplicationContext.Provider
value={{ latestVersion, toggleMobileNav, latestUrl, mobileNavOpen, user }} value={{ latestVersion, toggleMobileNav, latestUrl, mobileNavOpen, user }}
> >
{children} {children}
</ApplicationContext.Provider> </ApplicationContext.Provider>
) : (
<FullScreenLoading />
); );
} }

View File

@@ -38,12 +38,12 @@ async function apiRequest<T>(
return (await res.json()) as T; return (await res.json()) as T;
} }
export function useNetBirdFetch() { export function useNetBirdFetch(ignoreError: boolean = false) {
const tokenSource = config.tokenSource || "accessToken"; const tokenSource = config.tokenSource || "accessToken";
const { idToken } = useOidcIdToken(); const { idToken } = useOidcIdToken();
const { accessToken } = useOidcAccessToken(); const { accessToken } = useOidcAccessToken();
const token = tokenSource.toLowerCase() == "idtoken" ? idToken : accessToken; const token = tokenSource.toLowerCase() == "idtoken" ? idToken : accessToken;
const handleErrors = useApiErrorHandling(); const handleErrors = useApiErrorHandling(ignoreError);
const isTokenExpired = async () => { const isTokenExpired = async () => {
let attempts = 20; let attempts = 20;
@@ -77,9 +77,9 @@ export function useNetBirdFetch() {
}; };
} }
export default function useFetchApi<T>(url: string) { export default function useFetchApi<T>(url: string, ignoreError = false) {
const { fetch } = useNetBirdFetch(); const { fetch } = useNetBirdFetch(ignoreError);
const handleErrors = useApiErrorHandling(); const handleErrors = useApiErrorHandling(ignoreError);
const { data, error, isLoading, isValidating, mutate } = useSWR( const { data, error, isLoading, isValidating, mutate } = useSWR(
url, url,
@@ -102,9 +102,9 @@ export default function useFetchApi<T>(url: string) {
} as const; } as const;
} }
export function useApiCall<T>(url: string) { export function useApiCall<T>(url: string, ignoreError = false) {
const { fetch } = useNetBirdFetch(); const { fetch } = useNetBirdFetch(ignoreError);
const handleErrors = useApiErrorHandling(); const handleErrors = useApiErrorHandling(ignoreError);
return { return {
post: async (data: any, suffix = "") => { post: async (data: any, suffix = "") => {
@@ -130,10 +130,15 @@ export function useApiCall<T>(url: string) {
}; };
} }
export function useApiErrorHandling() { export function useApiErrorHandling(ignoreError = false) {
const { login } = useOidc(); const { login } = useOidc();
const currentPath = usePathname(); const currentPath = usePathname();
const { setError } = useErrorBoundary(); const { setError } = useErrorBoundary();
if (ignoreError)
return (err: ErrorResponse) => {
console.log(err);
return Promise.reject(err);
};
return (err: ErrorResponse) => { return (err: ErrorResponse) => {
if (err.code == 401 && err.message == "no valid authentication provided") { if (err.code == 401 && err.message == "no valid authentication provided") {