Skip to content
Snippets Groups Projects
Verified Commit 64933eed authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Display progress while the async call is running.

This prevents the Children to display while the calls are performing
parent d7531276
No related branches found
No related tags found
2 merge requests!56Refined auth flow and new website pages.,!44New route type (RestrictedRoute) and better api calls.
...@@ -2,13 +2,13 @@ import React, { useState, useEffect, useContext } from 'react'; ...@@ -2,13 +2,13 @@ import React, { useState, useEffect, useContext } from 'react';
import { Route, Redirect, RouteProps } from 'react-router-dom'; import { Route, Redirect, RouteProps } from 'react-router-dom';
import { AuthRoutes, NonAuthRoutes } from 'api/routes'; import { AuthRoutes, NonAuthRoutes } from 'api/routes';
import { isAuthenticated } from 'api/isAuthenticated'; import { isAuthenticated } from 'api/isAuthenticated';
import { CircularProgress } from '@material-ui/core';
/** /**
* *
* */ * */
type Props = { type Props = {
Component: React.FC<RouteProps>; Component: React.FC<RouteProps>;
restricted: boolean;
path: string; path: string;
}; };
...@@ -22,22 +22,25 @@ type Props = { ...@@ -22,22 +22,25 @@ type Props = {
/* eslint-disable react/jsx-props-no-spreading */ /* eslint-disable react/jsx-props-no-spreading */
export const RestrictedRoute = ({ Component, path }: Props): JSX.Element => { export const RestrictedRoute = ({ Component, path }: Props): JSX.Element => {
const [authUser, setAuthUser] = useState<boolean>(false); const [authUser, setAuthUser] = useState<boolean>(false);
const [isLoading, setLoading] = useState<boolean>(false);
useEffect(() => { useEffect(() => {
isAuthenticated(setAuthUser); isAuthenticated(setAuthUser).then(() => setLoading(true));
}); });
return ( return !isLoading ? (
<CircularProgress />
) : (
<Route <Route
path={path} path={path}
render={(props: RouteProps) => render={(props: RouteProps) =>
authUser ? ( !authUser ? (
// Redirect to component.
<Component {...props} />
) : (
// Redirect to homepage. // Redirect to homepage.
<Redirect <Redirect
to={{ pathname: `${AuthRoutes.dashboard}${AuthRoutes.home}` }} to={{ pathname: `${AuthRoutes.dashboard}${AuthRoutes.home}` }}
/> />
) : (
// Redirect to component.
<Component {...props} />
) )
} }
/> />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment