-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
45 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,24 @@ | ||
import React, { memo, Suspense, useMemo } from 'react'; | ||
import React, { memo, Suspense, useCallback } from 'react'; | ||
import { Route, Routes } from 'react-router-dom'; | ||
import { routeConfig } from 'shared/config/routeConfig/routeConfig'; | ||
import { | ||
AppRoutesProps, | ||
routeConfig, | ||
} from 'shared/config/routeConfig/routeConfig'; | ||
import { PageLoader } from 'shared/ui/PageLoader/PageLoader'; | ||
import { useSelector } from 'react-redux'; | ||
import { getUserAuthData } from 'entities/User'; | ||
import { RequireAuth } from 'app/providers/router/ui/RequireAuth'; | ||
|
||
const AppRouter = () => { | ||
const isAuth = useSelector(getUserAuthData); | ||
const renderWithWrapper = useCallback((route: AppRoutesProps) => { | ||
const element = ( | ||
<Suspense fallback={<PageLoader />}> | ||
<div className="page-wrapper">{route.element}</div> | ||
</Suspense> | ||
); | ||
|
||
const routes = useMemo(() => Object.values(routeConfig).filter((route) => { | ||
if (route.authOnly && !isAuth) { | ||
return false; | ||
} | ||
return <Route key={route.path} path={route.path} element={route.authOnly ? <RequireAuth>{element}</RequireAuth> : element} />; | ||
}, []); | ||
|
||
return true; | ||
}), [isAuth]); | ||
|
||
return ( | ||
<Routes> | ||
{routes.map(({ element, path }) => ( | ||
<Route | ||
key={path} | ||
path={path} | ||
element={( | ||
<Suspense fallback={<PageLoader />}> | ||
<div className="page-wrapper"> | ||
{element} | ||
</div> | ||
</Suspense> | ||
)} | ||
/> | ||
))} | ||
</Routes> | ||
); | ||
return <Routes>{Object.values(routeConfig).map(renderWithWrapper)}</Routes>; | ||
}; | ||
|
||
export default memo(AppRouter); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { getUserAuthData } from 'entities/User'; | ||
import { Navigate, useLocation } from 'react-router-dom'; | ||
import { RoutePath } from 'shared/config/routeConfig/routeConfig'; | ||
|
||
export function RequireAuth({ children }: { children: JSX.Element }) { | ||
const auth = useSelector(getUserAuthData); | ||
const location = useLocation(); | ||
|
||
if (!auth) { | ||
return <Navigate to={RoutePath.main} state={{ from: location }} replace />; | ||
} | ||
|
||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/entities/User/model/selectors/getUserInited/getUserInited.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
|
||
export const getUserInited = (state: StateSchema) => state?.user._inited; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ export interface User { | |
|
||
export interface UserSchema { | ||
authData?: User; | ||
_inited: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters