Skip to content

Commit

Permalink
Router refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nothing9537 committed Oct 23, 2023
1 parent 86dd421 commit b79a1f0
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 46 deletions.
32 changes: 21 additions & 11 deletions src/app/providers/RouterProvider/config/routeConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,65 @@ import { AdminPanelPage } from '@/pages/AdminPanelPage';
import { ForbiddenPage } from '@/pages/ForbiddenPage';
import { UserRole } from '@/entities/User';
import { AppRouteProps } from '@/shared/types/router';
import { RoutePath } from '@/shared/consts/router';
import {
getRouteMain,
getRouteAbout,
getRouteProfile,
getRouteArticles,
getRouteArticleEdit,
getRouteArticleCreate,
getRouteAdminPanel,
getRouteArticleDetails,
getRouteForbidden,
} from '@/shared/consts/router';

export const routeConfig: AppRouteProps[] = [
{
path: RoutePath.main,
path: getRouteMain(),
element: <MainPage />,
},
{
path: RoutePath.about,
path: getRouteAbout(),
element: <AboutPage />,
},
{
path: `${RoutePath.profile}:id`,
path: getRouteProfile(':id'),
element: <ProfilePage />,
authOnly: true,
},
{
path: RoutePath.articles,
path: getRouteArticles(),
element: <ArticlesPage />,
authOnly: true,
},
{
path: RoutePath.article_edit,
path: getRouteArticleEdit(':id'),
element: <ArticleEditPage />,
authOnly: true,
},
{
path: RoutePath.article_create,
path: getRouteArticleCreate(),
element: <ArticleCreatePage />,
authOnly: true,
},
{
path: RoutePath.admin_panel,
path: getRouteAdminPanel(),
element: <AdminPanelPage />,
authOnly: true,
roles: [UserRole.ADMIN, UserRole.MANAGER],
},
{
path: RoutePath.forbidden,
path: getRouteForbidden(),
element: <ForbiddenPage />,
authOnly: true,
},
{
path: `${RoutePath.article_details}:id`,
path: getRouteArticleDetails(':id'),
element: <ArticleDetailsPage />,
authOnly: true,
},
{
path: RoutePath.not_found,
path: '*',
element: <NotFoundPage />,
},
];
4 changes: 2 additions & 2 deletions src/app/providers/RouterProvider/ui/RequireAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ReactNode, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { User } from '@/entities/User';
import { RoutePath } from '@/shared/consts/router';
import { getRouteMain } from '@/shared/consts/router';

interface RequireAuthProps {
children: ReactNode;
Expand All @@ -13,7 +13,7 @@ export const RequireAuth = ({ children, authData }: RequireAuthProps) => {

useEffect(() => {
if (!authData) {
navigate(RoutePath.main);
navigate(getRouteMain());
}
}, [authData, navigate]);

Expand Down
4 changes: 2 additions & 2 deletions src/app/providers/RouterProvider/ui/RequireRole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ReactNode, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { User, UserRole } from '@/entities/User';
import { RoutePath } from '@/shared/consts/router';
import { getRouteForbidden } from '@/shared/consts/router';

interface RequireRoleProps {
roles: UserRole[];
Expand All @@ -23,7 +23,7 @@ export const RequireRole = ({ roles, authData, children }: RequireRoleProps) =>
});

if (!canVisit) {
navigate(RoutePath.forbidden);
navigate(getRouteForbidden());
}
}, [authData, navigate, roles]);

Expand Down
4 changes: 2 additions & 2 deletions src/entities/Comment/ui/CommentCard/CommentCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, memo } from 'react';
import { RoutePath } from '@/shared/consts/router';
import { getRouteProfile } from '@/shared/consts/router';
import { classNames } from '@/shared/lib/classNames/classNames';
import { AppLink } from '@/shared/ui/AppLink';
import { Avatar, AvatarSize } from '@/shared/ui/Avatar';
Expand Down Expand Up @@ -38,7 +38,7 @@ export const CommentCard: FC<CommentCardProps> = memo(({ className, comment, isL
return (
<section className={classNames(cls.CommentCard, {}, [className])}>
<div className={cls['with-timestamp']}>
<AppLink to={`${RoutePath.profile}${user.id}`}>
<AppLink to={getRouteProfile(user.id)}>
<div className={cls['user-header']}>
{user?.avatar
? <Avatar src={user?.avatar} alt="User Avatar" size={AvatarSize.NANO} borderRadius="50%" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FC, HTMLAttributeAnchorTarget, memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { ViewsIcon } from '@/shared/assets/icons';
import { RoutePath } from '@/shared/consts/router';
import { getRouteArticleDetails, getRouteProfile } from '@/shared/consts/router';
import { classNames } from '@/shared/lib/classNames/classNames';
import { AppLink } from '@/shared/ui/AppLink';
import { Avatar, AvatarSize } from '@/shared/ui/Avatar';
Expand Down Expand Up @@ -50,7 +50,7 @@ export const ArticlesListItem: FC<ArticlesListItemProps> = memo(({ className, ar
<div className={classNames(cls.ArticlesListItem, {}, [className, cls[view]])}>
<Card>
<div className={cls.header}>
<AppLink to={`${RoutePath.profile}${article.user.id}`}>
<AppLink to={getRouteProfile(article.user.id)}>
<div className={cls.avatar}>
<Avatar size={AvatarSize.NANO} alt="User Avatar" src={article.user.avatar} />
<Text text={article.user.username} />
Expand All @@ -66,7 +66,7 @@ export const ArticlesListItem: FC<ArticlesListItemProps> = memo(({ className, ar
)}
<div className={cls.footer}>
<AppLink
to={`${RoutePath.articles}/${article.id}`}
to={getRouteArticleDetails(article.id)}
target={target}
>
<Button onClick={onReadMoreClick}>
Expand All @@ -82,7 +82,7 @@ export const ArticlesListItem: FC<ArticlesListItemProps> = memo(({ className, ar

return (
<AppLink
to={`${RoutePath.articles}/${article.id}`}
to={getRouteArticleDetails(article.id)}
target={target}
className={classNames(cls.ArticlesListItem, {}, [className, cls[view]])}
>
Expand Down
6 changes: 3 additions & 3 deletions src/features/AvatarDropdown/ui/AvatarDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC, memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { RoutePath } from '@/shared/consts/router';
import { getRouteAdminPanel, getRouteArticleCreate } from '@/shared/consts/router';
import { useAppDispatch } from '@/shared/lib/hooks/useAppDispatch';
import { useAppSelector } from '@/shared/lib/hooks/useAppSelector';
import { TranslationNamespacesKeys } from '@/shared/types';
Expand Down Expand Up @@ -42,10 +42,10 @@ export const AvatarDropdown: FC<AvatarDropdownProps> = memo(({ className, transl
items={[
...(isAdminPanelAvailable ? [{
label: t('admin-panel'),
href: RoutePath.admin_panel,
href: getRouteAdminPanel(),
}] : []),
{ label: t('logout'), action: onLogoutHandler },
{ label: t('create-article'), href: RoutePath.article_create },
{ label: t('create-article'), href: getRouteArticleCreate() },
]}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FC, memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { getCanEditArticle } from '@/entities/Article';
import { RoutePath } from '@/shared/consts/router';
import { getRouteArticleEdit } from '@/shared/consts/router';
import { useAppSelector } from '@/shared/lib/hooks/useAppSelector';
import { AppLink } from '@/shared/ui/AppLink';
import { Button } from '@/shared/ui/Button';
Expand All @@ -28,7 +28,7 @@ export const ArticleDetailsPageHeader: FC<ArticleDetailsPageHeaderProps> = memo(
{t('return-back')}
</Button>
{canEdit && (
<AppLink to={`${RoutePath.articles}/${id}/edit`}>
<AppLink to={getRouteArticleEdit(id!)}>
<Button>
{t('can-edit')}
</Button>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/ForbiddenPage/ui/ForbiddenPage/ForbiddenPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { VStack } from '@/shared/ui/Stack';
import { Text, TextSize, TextTheme } from '@/shared/ui/Text';
import { Button } from '@/shared/ui/Button';
import { AppLink } from '@/shared/ui/AppLink';
import { RoutePath } from '@/shared/consts/router';
import { getRouteMain } from '@/shared/consts/router';

interface ForbiddenPageProps {
className?: string;
Expand All @@ -22,7 +22,7 @@ const ForbiddenPage: FC<ForbiddenPageProps> = () => {
theme={TextTheme.ERROR}
size={TextSize.L}
/>
<AppLink to={RoutePath.main}>
<AppLink to={getRouteMain()}>
<Button>
{t('return-to-main')}
</Button>
Expand Down
22 changes: 9 additions & 13 deletions src/shared/consts/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ export enum AppRoutes {
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/create',
[AppRoutes.ARTICLE_EDIT]: '/articles/:id/edit',
[AppRoutes.ADMIN_PANEL]: '/admin-panel',
[AppRoutes.FORBIDDEN_PAGE]: '/forbidden',
// * Not found page
[AppRoutes.NOT_FOUND]: '*',
};
export const getRouteMain = () => '/';
export const getRouteAbout = () => '/about';
export const getRouteProfile = (id: string) => `/profile/${id}`;
export const getRouteArticles = () => '/articles';
export const getRouteArticleDetails = (id: string) => `/articles/${id}`;
export const getRouteArticleCreate = () => '/articles/create';
export const getRouteArticleEdit = (id: string) => `/articles/${id}/edit`;
export const getRouteAdminPanel = () => '/admin-panel';
export const getRouteForbidden = () => '/forbidden';
10 changes: 5 additions & 5 deletions src/widgets/Aside/model/selectors/getAsideItems/getAsideItems.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSelector } from '@reduxjs/toolkit';
import { getUserAuthData } from '@/entities/User';
import { RoutePath } from '@/shared/consts/router';
import { getRouteAbout, getRouteArticles, getRouteMain, getRouteProfile } from '@/shared/consts/router';
import { MainPageIcon, AboutPageIcon, ProfilePageIcon, ArticlesPageIcon } from '@/shared/assets/icons';
import { AsideItemType } from '../../types/asideItems';

Expand All @@ -9,12 +9,12 @@ export const getAsideItems = createSelector(
(userData) => {
const asideItemsList: AsideItemType[] = [
{
path: RoutePath.main,
path: getRouteMain(),
Icon: MainPageIcon,
text: 'main-link',
},
{
path: RoutePath.about,
path: getRouteAbout(),
Icon: AboutPageIcon,
text: 'about-link',
},
Expand All @@ -23,13 +23,13 @@ export const getAsideItems = createSelector(
if (userData) {
asideItemsList.push(
{
path: `${RoutePath.profile}${userData?.id}`,
path: getRouteProfile(userData.id),
Icon: ProfilePageIcon,
text: 'profile-link',
authOnly: true,
},
{
path: RoutePath.articles,
path: getRouteArticles(),
Icon: ArticlesPageIcon,
text: 'articles-link',
authOnly: true,
Expand Down

0 comments on commit b79a1f0

Please sign in to comment.