Skip to content

Commit

Permalink
Private routes have been reworked in association with react router docs
Browse files Browse the repository at this point in the history
  • Loading branch information
yaazzik committed Jun 28, 2023
1 parent 2a0eea8 commit 25f78db
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 44 deletions.
10 changes: 5 additions & 5 deletions src/app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { classNames } from 'shared/lib/classNames/classNames';
import { AppRouter } from 'app/providers/AppRuoter';
import { AppRouter } from 'app/providers/AppRouter';
import { Navbar } from 'widgets/Navbar';
import { useTheme } from 'app/providers/ThemeProvider';
import { Sidebar } from 'widgets/Sidebar';
import React, { Suspense, useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { userActions } from 'entities/User';
import { useDispatch, useSelector } from 'react-redux';
import { getUserInited, userActions } from 'entities/User';

export const App = () => {
const { theme } = useTheme();

const inited = useSelector(getUserInited);
const dispatch = useDispatch();

useEffect(() => {
Expand All @@ -27,7 +27,7 @@ export const App = () => {
<Navbar />
<div className="content-page">
<Sidebar />
<AppRouter />
{inited && <AppRouter />}
</div>
</Suspense>
</div>
Expand Down
File renamed without changes.
35 changes: 35 additions & 0 deletions src/app/providers/AppRouter/ui/AppRouter.tsx
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);
15 changes: 15 additions & 0 deletions src/app/providers/AppRouter/ui/RequireAuth.tsx
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;
}
37 changes: 0 additions & 37 deletions src/app/providers/AppRuoter/ui/AppRouter.tsx

This file was deleted.

1 change: 1 addition & 0 deletions src/entities/User/index.ts
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';
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;
5 changes: 4 additions & 1 deletion src/entities/User/model/slice/userSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { USER_LOCALSTORAGE_KEY } from 'shared/const/localstorage';
import { User, UserSchema } from '../types/user';

const initialState: UserSchema = {};
const initialState: UserSchema = {
_inited: false,
};

export const userSlice = createSlice({
name: 'user',
Expand All @@ -16,6 +18,7 @@ export const userSlice = createSlice({
if (user) {
state.authData = JSON.parse(user);
}
state._inited = true;
},
logout: (state) => {
state.authData = undefined;
Expand Down
1 change: 1 addition & 0 deletions src/entities/User/model/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface User {

export interface UserSchema {
authData?: User;
_inited: boolean;
}
2 changes: 1 addition & 1 deletion src/shared/config/routeConfig/routeConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AboutPage } from 'pages/AboutPage';
import { PageNotFound } from 'pages/PageNotFound';
import { ProfilePage } from 'pages/ProfilePage';

type AppRoutesProps = RouteProps & {
export type AppRoutesProps = RouteProps & {
authOnly?: boolean;
}
export enum AppRoute {
Expand Down

1 comment on commit 25f78db

@vercel
Copy link

@vercel vercel bot commented on 25f78db Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.