mirror of
https://github.com/netbirdio/dashboard.git
synced 2026-01-26 01:21:04 +00:00
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:
@@ -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<User[]>("/users");
|
||||
const userRequest = useApiCall<User[]>("/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 ? (
|
||||
<ApplicationContext.Provider
|
||||
value={{ latestVersion, toggleMobileNav, latestUrl, mobileNavOpen, user }}
|
||||
>
|
||||
{children}
|
||||
</ApplicationContext.Provider>
|
||||
) : (
|
||||
<FullScreenLoading />
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,12 +38,12 @@ async function apiRequest<T>(
|
||||
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<T>(url: string) {
|
||||
const { fetch } = useNetBirdFetch();
|
||||
const handleErrors = useApiErrorHandling();
|
||||
export default function useFetchApi<T>(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<T>(url: string) {
|
||||
} as const;
|
||||
}
|
||||
|
||||
export function useApiCall<T>(url: string) {
|
||||
const { fetch } = useNetBirdFetch();
|
||||
const handleErrors = useApiErrorHandling();
|
||||
export function useApiCall<T>(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<T>(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") {
|
||||
|
||||
Reference in New Issue
Block a user