-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathappRouter.tsx
99 lines (90 loc) · 2.36 KB
/
appRouter.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { ReactElement } from 'react'
import { Navigate, createBrowserRouter } from 'react-router-dom'
import { featureToggleLoader } from '@/entities/featureToggle'
import { selectIsAuthorized } from '@/entities/session'
import { CartPage } from '@/pages/cart'
import { CategoryPage } from '@/pages/category'
import { LoginPage } from '@/pages/login'
import { MainPage } from '@/pages/main'
import { ProductPage } from '@/pages/product'
import { WishlistPage } from '@/pages/wishlist'
import { useAppSelector } from '@/shared/model'
import { appStore } from './appStore'
import { baseLayout } from './layouts/baseLayout'
import { layoutWithSidebar } from './layouts/layoutWithSidebar'
type GuestGuardProps = {
children: ReactElement
}
function GuestGuard({ children }: GuestGuardProps) {
const isAuthorized = useAppSelector(selectIsAuthorized)
if (!isAuthorized)
return <Navigate to="/login" />
return children
}
type AuthGuardProps = {
children: ReactElement
}
function AuthGuard({ children }: AuthGuardProps) {
const isAuthorized = useAppSelector(selectIsAuthorized)
if (isAuthorized)
return <Navigate to="/" />
return children
}
export function appRouter() {
return createBrowserRouter([
{
element: baseLayout,
errorElement: <div>Error happened</div>,
loader: async () => {
return await featureToggleLoader(appStore.dispatch)
},
children: [
{
path: '/login',
element: (
<AuthGuard>
<LoginPage />
</AuthGuard>
),
},
{
path: '/user/wishlist',
element: (
<GuestGuard>
<WishlistPage />
</GuestGuard>
),
},
{
path: '/user/cart',
element: (
<GuestGuard>
<CartPage />
</GuestGuard>
),
},
{
path: '/category/:categoryId',
element: <CategoryPage />,
},
{
path: '/product/:productId',
element: <ProductPage />,
},
],
},
{
element: layoutWithSidebar,
errorElement: <div>error</div>,
loader: async () => {
return await featureToggleLoader(appStore.dispatch)
},
children: [
{
path: '/',
element: <MainPage />,
},
],
},
])
}