Skip to content

Commit fc9a9df

Browse files
authored
Block application and show loading until users are fetched (#330)
* Add option to ignore errors * Block application and show loading until users are fetched
1 parent 093efc0 commit fc9a9df

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/contexts/ApplicationProvider.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useOidcUser } from "@axa-fr/react-oidc";
2+
import FullScreenLoading from "@components/ui/FullScreenLoading";
23
import { useApiCall } from "@utils/api";
34
import { useIsMd } from "@utils/responsive";
45
import { getLatestNetbirdRelease } from "@utils/version";
@@ -28,10 +29,14 @@ export default function ApplicationProvider({ children }: Props) {
2829
const { oidcUser: user } = useOidcUser();
2930
const [mobileNavOpen, setMobileNavOpen] = useState(false);
3031
const isMd = useIsMd();
31-
const userRequest = useApiCall<User[]>("/users");
32+
const userRequest = useApiCall<User[]>("/users", true);
33+
const [show, setShow] = useState(false);
3234

3335
useEffect(() => {
34-
userRequest.get().then();
36+
userRequest
37+
.get()
38+
.then(() => setShow(true))
39+
.catch(() => setShow(true));
3540
// eslint-disable-next-line react-hooks/exhaustive-deps
3641
}, []);
3742

@@ -66,12 +71,14 @@ export default function ApplicationProvider({ children }: Props) {
6671
setMobileNavOpen(!mobileNavOpen);
6772
};
6873

69-
return (
74+
return show ? (
7075
<ApplicationContext.Provider
7176
value={{ latestVersion, toggleMobileNav, latestUrl, mobileNavOpen, user }}
7277
>
7378
{children}
7479
</ApplicationContext.Provider>
80+
) : (
81+
<FullScreenLoading />
7582
);
7683
}
7784

src/utils/api.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ async function apiRequest<T>(
3838
return (await res.json()) as T;
3939
}
4040

41-
export function useNetBirdFetch() {
41+
export function useNetBirdFetch(ignoreError: boolean = false) {
4242
const tokenSource = config.tokenSource || "accessToken";
4343
const { idToken } = useOidcIdToken();
4444
const { accessToken } = useOidcAccessToken();
4545
const token = tokenSource.toLowerCase() == "idtoken" ? idToken : accessToken;
46-
const handleErrors = useApiErrorHandling();
46+
const handleErrors = useApiErrorHandling(ignoreError);
4747

4848
const isTokenExpired = async () => {
4949
let attempts = 20;
@@ -77,9 +77,9 @@ export function useNetBirdFetch() {
7777
};
7878
}
7979

80-
export default function useFetchApi<T>(url: string) {
81-
const { fetch } = useNetBirdFetch();
82-
const handleErrors = useApiErrorHandling();
80+
export default function useFetchApi<T>(url: string, ignoreError = false) {
81+
const { fetch } = useNetBirdFetch(ignoreError);
82+
const handleErrors = useApiErrorHandling(ignoreError);
8383

8484
const { data, error, isLoading, isValidating, mutate } = useSWR(
8585
url,
@@ -102,9 +102,9 @@ export default function useFetchApi<T>(url: string) {
102102
} as const;
103103
}
104104

105-
export function useApiCall<T>(url: string) {
106-
const { fetch } = useNetBirdFetch();
107-
const handleErrors = useApiErrorHandling();
105+
export function useApiCall<T>(url: string, ignoreError = false) {
106+
const { fetch } = useNetBirdFetch(ignoreError);
107+
const handleErrors = useApiErrorHandling(ignoreError);
108108

109109
return {
110110
post: async (data: any, suffix = "") => {
@@ -130,10 +130,15 @@ export function useApiCall<T>(url: string) {
130130
};
131131
}
132132

133-
export function useApiErrorHandling() {
133+
export function useApiErrorHandling(ignoreError = false) {
134134
const { login } = useOidc();
135135
const currentPath = usePathname();
136136
const { setError } = useErrorBoundary();
137+
if (ignoreError)
138+
return (err: ErrorResponse) => {
139+
console.log(err);
140+
return Promise.reject(err);
141+
};
137142

138143
return (err: ErrorResponse) => {
139144
if (err.code == 401 && err.message == "no valid authentication provided") {

0 commit comments

Comments
 (0)