Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
EuJinnLucaShow committed Jun 28, 2023
1 parent c4d75ea commit e5162ad
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ const ContactsPage = lazy(() => import('../pages/Contacts'));

function App() {
const dispatch = useDispatch();

const { isRefreshing } = useAuth();

useEffect(() => {
dispatch(refreshUser());
}, [dispatch]);

return isRefreshing ? (
<Loader />
return isRefreshing ? ( // if isRefreshing is true, then render Loader, else render Container
<Loader /> // Loader - spinner
) : (
<Container>
<Routes>
Expand Down
2 changes: 0 additions & 2 deletions src/components/Layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ export const Layout = () => {
position="top-center"
toastOptions={{
duration: 4000,

// Custom Icon
icon: '🤬',

// Styling
style: {
color: '#fa0000',
Expand Down
8 changes: 4 additions & 4 deletions src/components/PrivateRoute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Navigate } from 'react-router-dom';

import { useAuth } from './hooks';

export const PrivateRoute = ({ component: Component, redirectTo = '/' }) => {
const { isLoggedIn, isRefreshing } = useAuth();
const shouldRedirect = !isLoggedIn && !isRefreshing;
return shouldRedirect ? <Navigate to={redirectTo} /> : Component;
export const PrivateRoute = ({ component: Component, redirectTo = '/' }) => { // component - ContactsPage
const { isLoggedIn, isRefreshing } = useAuth(); // isLoggedIn - true or false
const shouldRedirect = !isLoggedIn && !isRefreshing; // if isLoggedIn is false and isRefreshing is false, then shouldRedirect is true
return shouldRedirect ? <Navigate to={redirectTo} /> : Component; // if shouldRedirect is true, then redirect to redirectTo, else render Component
};
5 changes: 3 additions & 2 deletions src/components/RestrictedRoute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Navigate } from 'react-router-dom';
import { useAuth } from './hooks';

export const RestrictedRoute = ({ component: Component, redirectTo = '/' }) => {
const { isLoggedIn } = useAuth();
return isLoggedIn ? <Navigate to={redirectTo} /> : Component;
// component - RegisterPage or LoginPage
const { isLoggedIn } = useAuth(); // isLoggedIn - true or false
return isLoggedIn ? <Navigate to={redirectTo} /> : Component; // if isLoggedIn is true, then redirect to redirectTo, else render Component
};

0 comments on commit e5162ad

Please sign in to comment.