Skip to content

Commit

Permalink
fix: refactor routes files
Browse files Browse the repository at this point in the history
  • Loading branch information
TomatoVan committed Mar 10, 2024
1 parent c3323ba commit f012a6b
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,15 @@
import { RouteProps } from 'react-router-dom';
import { MainPage } from '@/pages/MainPage';
import { AboutPage } from '@/pages/AboutPage';
import { NotFoundPage } from '@/pages/NotFoundPage';
import { ProfilePage } from '@/pages/ProfilePage';
import { ArticlesPage } from '@/pages/ArticlesPage';
import { ArticlesDetailsPage } from '@/pages/ArticlesDetailsPage';
import { ArticleEditPage } from '@/pages/ArticleEditPage';
import { AdminPanelPage } from '@/pages/AdminPanelPage';
import { UserRole } from '@/entities/User';
import { ForbiddenPage } from '@/pages/ForbiddenPage';
import { UserRole } from '../../../entities/User';

export type AppRoutesProps = RouteProps & {
authOnly?: boolean;
roles?: UserRole[];
}

export enum AppRoutes {
MAIN = 'main',
ABOUT = 'about',
PROFILE = 'profile',
ARTICLES = 'articles',
ARTICLE_DETAILS = 'article_details',
ARTICLE_CREATE = 'article_create',
ARTICLE_EDIT = 'article_edit',
ADMIN_PANEL = 'admin_panel',
FORBIDDEN = 'forbidden',
NOT_FOUND = 'not_found',
}

export const RoutePath: Record<AppRoutes, string> = {
[AppRoutes.MAIN]: '/',
[AppRoutes.ABOUT]: '/about',
[AppRoutes.PROFILE]: '/profile/', // + :id
[AppRoutes.ARTICLES]: '/articles',
[AppRoutes.ARTICLE_DETAILS]: '/articles/', // + :id
[AppRoutes.ARTICLE_CREATE]: '/articles/new',
[AppRoutes.ARTICLE_EDIT]: '/articles/:id/edit',
[AppRoutes.ADMIN_PANEL]: '/admin',
[AppRoutes.FORBIDDEN]: '/forbidden',
[AppRoutes.NOT_FOUND]: '*',
};
import { NotFoundPage } from '@/pages/NotFoundPage';
import { AppRoutes, RoutePath } from '@/shared/const/router';
import { AppRoutesProps } from '@/shared/types/router';

export const routeConfig: Record<AppRoutes, AppRoutesProps> = {
[AppRoutes.MAIN]: {
Expand Down Expand Up @@ -89,5 +59,4 @@ export const routeConfig: Record<AppRoutes, AppRoutesProps> = {
path: RoutePath.not_found,
element: <NotFoundPage />,
},

};
6 changes: 2 additions & 4 deletions src/app/providers/router/ui/AppRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React, { memo, Suspense, useCallback } from 'react';
import { Route, Routes } from 'react-router-dom';
import {
AppRoutesProps,
routeConfig,
} from '@/shared/config/routeConfig/routeConfig';
import { RequireAuth } from '@/app/providers/router/ui/RequireAuth';
import { PageLoader } from '@/widgets/PageLoader';
import { routeConfig } from '@/app/providers/router/config/routeConfig';
import { AppRoutesProps } from '@/shared/types/router';

const AppRouter = () => {
const renderWithWrapper = useCallback((route: AppRoutesProps) => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/providers/router/ui/RequireAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useSelector } from 'react-redux';
import { Navigate, useLocation } from 'react-router-dom';
import { useMemo } from 'react';
import { RoutePath } from '@/shared/config/routeConfig/routeConfig';
import { getUserAuthData, getUserRoles, UserRole } from '../../../../entities/User';
import { RoutePath } from '@/shared/const/router';

interface RequireAuthProps {
children: JSX.Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import EyeIcon from '@/shared/assets/icons/eye-20-20.svg';
import { Card } from '@/shared/ui/Card/Card';
import { Avatar } from '@/shared/ui/Avatar/Avatar';
import { Button, ButtonTheme } from '@/shared/ui/Button/Button';
import { RoutePath } from '@/shared/config/routeConfig/routeConfig';
import { AppLink } from '@/shared/ui/AppLink/AppLink';
import { ArticleBlockType, ArticleView } from '../../model/consts/consts';
import { ArticleTextBlockComponent } from '../ArticleTextBlockComponent/ArticleTextBlockComponent';
Expand All @@ -17,6 +16,7 @@ import {

} from '../../model/types/article';
import cls from './ArticleListItem.module.scss';
import { RoutePath } from '@/shared/const/router';

interface ArticleListItemProps {
className?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/entities/Comment/ui/CommentCard/CommentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { useTranslation } from 'react-i18next';
import { memo } from 'react';
import { classNames } from '@/shared/lib/classNames/classNames';
import { AppLink } from '@/shared/ui/AppLink/AppLink';
import { RoutePath } from '@/shared/config/routeConfig/routeConfig';
import { VStack } from '@/shared/ui/Stack';
import cls from './CommentCard.module.scss';
import { Comment } from '../../model/types/comment';
import { Avatar } from '../../../../shared/ui/Avatar/Avatar';
import { Text } from '../../../../shared/ui/Text/Text';
import { Skeleton } from '../../../../shared/ui/Skeleton/Skeleton';
import { RoutePath } from '@/shared/const/router';

interface CommentCardProps {
className?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { Dropdown } from '@/shared/ui/Popups';
import { RoutePath } from '@/shared/config/routeConfig/routeConfig';
import { Avatar } from '@/shared/ui/Avatar/Avatar';
import { useAppDispatch } from '@/shared/lib/hook/useAppDispatch/useAppDispatch';
import {
Expand All @@ -11,6 +10,7 @@ import {
isUserManager,
userActions,
} from '../../../../entities/User';
import { RoutePath } from '@/shared/const/router';

export const AvatarDropDown = memo(() => {
const { t } = useTranslation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { memo, useCallback } from 'react';
import { useNavigate } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { Button, ButtonTheme } from '@/shared/ui/Button/Button';
import { RoutePath } from '@/shared/config/routeConfig/routeConfig';
import { classNames } from '@/shared/lib/classNames/classNames';
import { HStack } from '@/shared/ui/Stack';
import { getCanEditArticle } from '../../model/selectors/article';
import { getArticlesDetailsData } from '../../../../entities/Article';
import { RoutePath } from '@/shared/const/router';

interface ArticlesDetailsPageHeaderProps {
className?: string;
Expand Down
25 changes: 25 additions & 0 deletions src/shared/const/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export enum AppRoutes {
MAIN = 'main',
ABOUT = 'about',
PROFILE = 'profile',
ARTICLES = 'articles',
ARTICLE_DETAILS = 'article_details',
ARTICLE_CREATE = 'article_create',
ARTICLE_EDIT = 'article_edit',
ADMIN_PANEL = 'admin_panel',
FORBIDDEN = 'forbidden',
NOT_FOUND = 'not_found',
}

export const RoutePath: Record<AppRoutes, string> = {
[AppRoutes.MAIN]: '/',
[AppRoutes.ABOUT]: '/about',
[AppRoutes.PROFILE]: '/profile/', // + :id
[AppRoutes.ARTICLES]: '/articles',
[AppRoutes.ARTICLE_DETAILS]: '/articles/', // + :id
[AppRoutes.ARTICLE_CREATE]: '/articles/new',
[AppRoutes.ARTICLE_EDIT]: '/articles/:id/edit',
[AppRoutes.ADMIN_PANEL]: '/admin',
[AppRoutes.FORBIDDEN]: '/forbidden',
[AppRoutes.NOT_FOUND]: '*',
};
7 changes: 7 additions & 0 deletions src/shared/types/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { RouteProps } from 'react-router-dom';
import { UserRole } from '@/entities/User';

export type AppRoutesProps = RouteProps & {
authOnly?: boolean;
roles?: UserRole[];
};
2 changes: 1 addition & 1 deletion src/widgets/Navbar/ui/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { Button, ButtonTheme } from '@/shared/ui/Button/Button';
import { LoginModal } from '@/features/AuthByUsername';
import { Text, TextTheme } from '@/shared/ui/Text/Text';
import { AppLink, AppLinkTheme } from '@/shared/ui/AppLink/AppLink';
import { RoutePath } from '@/shared/config/routeConfig/routeConfig';
import { HStack } from '@/shared/ui/Stack';
import { NotificationButton } from '@/features/notificationButton';
import { AvatarDropDown } from '@/features/avatarDropdown';
import { getUserAuthData } from '../../../entities/User';
import cls from './Navbar.module.scss';
import { RoutePath } from '@/shared/const/router';

interface NavbarProps {
className?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/Sidebar/model/selectors/getSidebarItems.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createSelector } from '@reduxjs/toolkit';
import { RoutePath } from '@/shared/config/routeConfig/routeConfig';
import MainIcon from '@/shared/assets/icons/main-20-20.svg';
import AboutIcon from '@/shared/assets/icons/about-20-20.svg';
import ProfileIcon from '@/shared/assets/icons/profile-20-20.svg';
import ArticleIcon from '@/shared/assets/icons/article-20-20.svg';
import { getUserAuthData } from '../../../../entities/User';
import { SidebarItemType } from '../types/sidebar';
import { RoutePath } from '@/shared/const/router';

export const getSidebarItems = createSelector(getUserAuthData, (userData) => {
const sidebarItemsList: SidebarItemType[] = [
Expand Down

0 comments on commit f012a6b

Please sign in to comment.