Skip to content

Commit ad7c5e8

Browse files
committed
Fix the app navigation
1 parent 07aafc3 commit ad7c5e8

4 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/components/AppRouter.tsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
import { lazy, Suspense } from 'react';
2-
import { useRoutes } from 'react-router-dom';
2+
import { Navigate, useRoutes } from 'react-router-dom';
33
import LoginPage from '../pages/Login/Login';
44
import CircularProgress from '@mui/material/CircularProgress';
55

66
const LazyHomePage = lazy(() => import('../pages/Home/Home'));
77

8-
export enum RouteName {
9-
Home = '/home',
10-
}
8+
export const START_PAGE = '/github-react-app-ui';
119

1210
export default function AppRouter() {
1311
return useRoutes([
1412
{
15-
index: true,
16-
element: <LoginPage />,
13+
path: START_PAGE,
14+
children: [
15+
{
16+
index: true,
17+
element: <LoginPage />,
18+
},
19+
{
20+
path: 'home',
21+
element: (
22+
<Suspense fallback={<CircularProgress />}>
23+
<LazyHomePage />
24+
</Suspense>
25+
),
26+
},
27+
{
28+
path: '*',
29+
element: <Navigate to={START_PAGE} />,
30+
},
31+
],
1732
},
1833
{
19-
path: RouteName.Home,
20-
element: (
21-
<Suspense fallback={<CircularProgress />}>
22-
<LazyHomePage />
23-
</Suspense>
24-
),
34+
path: '*',
35+
element: <Navigate to={START_PAGE} />,
2536
},
2637
]);
2738
}

src/components/Navbar/Navbar.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { User } from '../../models/user';
33
import { signOut } from '../../app/auth';
44
import { useNavigate } from 'react-router-dom';
55
import { useGetCurrentUserQuery } from '../../features/user/userAPI';
6+
import { START_PAGE } from '../AppRouter';
67
import AppBar from '@mui/material/AppBar';
78
import Box from '@mui/material/Box';
89
import Toolbar from '@mui/material/Toolbar';
@@ -26,7 +27,7 @@ export default function Navbar() {
2627

2728
const handleUserSignOut = () => {
2829
signOut();
29-
navigate('/');
30+
navigate(START_PAGE);
3031
};
3132

3233
return (

src/hooks/useAuth.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useLazyGetAccessTokenByCodeQuery } from '../features/auth/authAPI';
22
import { useEffect } from 'react';
33
import { useLocation, useNavigate } from 'react-router-dom';
44
import { AccessTokenResponse, isSignedIn, setAccessToken } from '../app/auth';
5-
import { RouteName } from '../components/AppRouter';
5+
import { START_PAGE } from '../components/AppRouter';
66
import type { SerializedError } from '@reduxjs/toolkit';
77
import type { FetchBaseQueryError } from '@reduxjs/toolkit/dist/query';
88
import type { Optional } from '../app/types';
@@ -24,10 +24,13 @@ export function useAuth(): HookReturnType {
2424
const code = searchParams.get('code');
2525

2626
if (code) {
27-
trigger(code).then(({ data }) => setAccessToken(data?.accessToken.access_token ?? ''));
28-
}
29-
if (isSignedIn()) {
30-
navigate(RouteName.Home);
27+
trigger(code)
28+
.then(({ data }) => setAccessToken(data?.accessToken.access_token ?? ''))
29+
.then(() => {
30+
if (isSignedIn()) {
31+
navigate(START_PAGE + '/home');
32+
}
33+
});
3134
}
3235
}, [search, pathname, trigger, navigate]);
3336

src/pages/Home/Home.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState } from 'react';
22
import { Navigate } from 'react-router-dom';
33
import { isSignedIn } from '../../app/auth';
4+
import { START_PAGE } from '../../components/AppRouter';
45
import Box from '@mui/material/Box';
56
import Navbar from '../../components/Navbar/Navbar';
67
import RepositoryList from '../../components/RepositoryList/RepositoryList';
@@ -12,7 +13,7 @@ export default function HomePage() {
1213
const [org, setOrg] = useState('');
1314

1415
if (!isSignedIn()) {
15-
return <Navigate to="/" />;
16+
return <Navigate to={START_PAGE} />;
1617
}
1718

1819
const handleOrgInput = (e: React.ChangeEvent<HTMLInputElement>) => {

0 commit comments

Comments
 (0)