From ec9c1d13d992b8881b3a90d6bd5b248433e2dd1a Mon Sep 17 00:00:00 2001
From: Alberto Defendi <1369-ahl-berto@users.noreply.gitlab.inf.unibz.it>
Date: Thu, 27 May 2021 14:41:25 +0200
Subject: [PATCH] Try to wait until datafetch before rendering.

---
 src/components/Authorization/Authorization.tsx | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/components/Authorization/Authorization.tsx b/src/components/Authorization/Authorization.tsx
index 3a03f1f..f00d076 100644
--- a/src/components/Authorization/Authorization.tsx
+++ b/src/components/Authorization/Authorization.tsx
@@ -6,6 +6,7 @@ import { Unauthorized } from 'components/NonAuthUser/Unauthorized/Unauthorized';
 import { useAuth } from 'hooks/useAuth';
 import { useRole } from 'hooks/useRole';
 import { Redirect } from 'react-router-dom';
+import { CircularProgressClassKey } from '@material-ui/core';
 
 const HandleIsAuth: FC<{ isAuth: boolean }> = ({ isAuth }) =>
   isAuth ? (
@@ -41,14 +42,16 @@ export const withAuthorization = <T extends WithAuthProps = WithAuthProps>(
     const { allowedRoles } = props as T;
 
     const { role, isAuth } = useContext(AuthContext);
-
     console.log(`ROLE ${role} AUTH ${isAuth}`);
 
-    // props comes afterwards so the can override the default ones.
-    return allowedRoles.includes(role) && isAuth ? (
+    /* eslint-disable no-nested-ternary */
+    return typeof isAuth === null || role === null ? (
+      <BlurCircular />
+    ) : // props comes afterwards so the can override the default ones.
+    allowedRoles.includes(role) && isAuth ? (
       <WrappedComponent {...(props as T)} />
     ) : (
-      <HandleIsAuth isAuth={isAuth} />
+      <HandleIsAuth isAuth={!!isAuth} />
     );
   };
 
-- 
GitLab