-
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.
Private routes have been reworked in association with react router docs
- Loading branch information
Showing
10 changed files
with
65 additions
and
44 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
File renamed without changes.
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,35 @@ | ||
import React, { | ||
memo, Suspense, useCallback, | ||
} from 'react'; | ||
import { Route, Routes } from 'react-router-dom'; | ||
import { AppRoutesProps, routeConfig } from 'shared/config/routeConfig/routeConfig'; | ||
import { Loader } from 'widgets/Loader'; | ||
import { RequireAuth } from 'app/providers/AppRouter/ui/RequireAuth'; | ||
|
||
const AppRouter = () => { | ||
const renderWithWrapper = useCallback((route: AppRoutesProps) => { | ||
const element = ( | ||
<Suspense fallback={<Loader />}> | ||
<div className="page-wrapper"> | ||
{route.element} | ||
</div> | ||
</Suspense> | ||
); | ||
|
||
return ( | ||
<Route | ||
key={route.path} | ||
path={route.path} | ||
element={route.authOnly ? <RequireAuth>{element}</RequireAuth> : element} | ||
/> | ||
); | ||
}, []); | ||
|
||
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 was deleted.
Oops, something went wrong.
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,3 +1,4 @@ | ||
export { userActions, userReducer } from './model/slice/userSlice'; | ||
export { User, UserSchema } from './model/types/user'; | ||
export { getUserAuthData } from './model/selectors/getUserAuthData/getUserAuthData'; | ||
export { getUserInited } from './model/selectors/getUserInited/getUserInited'; |
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
25f78db
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
social-media-ndg8 – ./
social-media-ndg8-yaazzik.vercel.app
social-media-ndg8.vercel.app
xn--90aeezcbq.xn--p1ai
www.xn--90aeezcbq.xn--p1ai
social-media-ndg8-git-master-yaazzik.vercel.app