Skip to content

Commit 2ca1e32

Browse files
committed
- simplified router, bumped up version
1 parent 954485c commit 2ca1e32

10 files changed

Lines changed: 65 additions & 78 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ src
9090
9191
└───router
9292
│ │ router.tsx
93-
│ └───routes
93+
│ └───routes.tsx
9494
│ └───utility
9595
9696
└───store

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "docker-react",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "",
55
"author": "Benjamin Brachmann",
66
"license": "MIT",

src/components/basic/route-layout/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useEffect, createRef } from 'react'
44
import './route-layout.scss'
55

66
interface Props {
7-
children: React.ReactChildren
7+
children: React.ReactNode
88
}
99

1010
const RouteLayout: React.FC<Props> = function ({ children }) {

src/router/router.spec.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ describe('router component', () => {
2626

2727
test('the router uses the theme from redux and applies it to the html tag', () => {
2828
const store = mockStore(mockInitialState)
29-
3029
render(
3130
<Provider store={store}>
3231
<AppRouter />

src/router/router.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,21 @@ import { LoadingPage } from '@/components/pages/loading'
1717
import { ErrorPage } from '@/components/pages/error'
1818

1919
/* Routes */
20-
import { LoginRoute } from '@/router/routes/login.route'
21-
import { SettingsRoute } from '@/router/routes/settings.route'
22-
import { MainRoute } from '@/router/routes/main.route'
20+
import { routes } from '@/router/routes'
2321

2422
function AppRoutes() {
2523
const location = useLocation()
2624

2725
return (
2826
<>
29-
<NavigationBar routes={[LoginRoute, SettingsRoute, MainRoute]} />
27+
<NavigationBar routes={routes} />
3028
<TransitionGroup>
3129
<CSSTransition key={location.key} classNames="app-route-change" timeout={1000}>
3230
<Suspense fallback={<LoadingPage />}>
3331
<Switch location={location}>
34-
<Route path={LoginRoute.route} render={LoginRoute.render} />
35-
<Route path={SettingsRoute.route} render={SettingsRoute.render} />
36-
<Route path={MainRoute.route} render={MainRoute.render} />
32+
{routes.map(({ route, render }) => (
33+
<Route key={`route-${route}`} path={route} render={render} />
34+
))}
3735
<Route component={ErrorPage} />
3836
</Switch>
3937
</Suspense>

src/router/routes.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { lazily } from 'react-lazily'
2+
import { createRoutes } from '@/router/utility/router-utility.module'
3+
4+
// order of routes is important, since router will render first match
5+
const routes = createRoutes([
6+
{
7+
route: '/login',
8+
redirection: '/',
9+
dependencies: { 'auth.jwt': (jwt) => jwt === null },
10+
component: lazily(() => import('@/components/pages/login')).LoginPage,
11+
},
12+
{
13+
route: '/settings',
14+
redirection: '/login',
15+
dependencies: { 'auth.jwt': (jwt) => jwt !== null },
16+
component: lazily(() => import('@/components/pages/settings')).SettingsPage,
17+
},
18+
{
19+
route: '/',
20+
redirection: '/login',
21+
dependencies: { 'auth.jwt': (jwt) => jwt !== null },
22+
component: lazily(() => import('@/components/pages/main')).MainPage,
23+
},
24+
])
25+
26+
export { routes }

src/router/routes/login.route.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/router/routes/main.route.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/router/routes/settings.route.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/router/utility/router-utility.module.tsx

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { Redirect, BrowserRouter as PathRouter, HashRouter } from 'react-router-
44
import { store } from '@/store/store'
55
import { ErrorPage } from '@/components/pages/error'
66
import { RouteLayout } from '@/components/basic/route-layout'
7+
import React from 'react'
78

89
type RouteType = 'hash' | 'path'
910

10-
interface RouteParameters {
11+
export interface RouteDescription {
1112
component: React.FC
1213
route: string
1314
redirection: string
@@ -23,46 +24,48 @@ const getRouters = () => ({
2324
Path: PathRouter,
2425
})
2526

26-
const createRoute = ({ component, route, redirection, dependencies }: RouteParameters) => {
27-
const mustRedirect = () =>
28-
!(dependencies
29-
? Object.keys(dependencies).reduce((previous, current) => {
27+
const createRoute = (route: RouteDescription) => {
28+
// Checks if all dependencies are fulfilled to render a route
29+
const mustRedirect = (mustRedirectRoute: RouteDescription) =>
30+
!(mustRedirectRoute.dependencies
31+
? Object.keys(mustRedirectRoute.dependencies).reduce((previous, current) => {
3032
if (previous === false) {
3133
return false
3234
}
3335
const dependencyValue = get(store.getState(), current)
34-
const dependencyComparison = dependencies[current]
35-
36-
// Dependency can be checked by its value or by validator function
36+
const dependencyComparison = mustRedirectRoute.dependencies[current]
3737
return typeof dependencyComparison === 'function' ? !!dependencyComparison(dependencyValue) : dependencyValue === dependencyComparison
3838
}, true)
3939
: true)
4040

41-
// Decide what component to return
42-
const renderFunction = () => {
43-
const Component = component
44-
const redirect = mustRedirect()
45-
if (redirect && !!redirection) {
46-
return <Redirect to={redirection} />
47-
}
48-
if (redirect && !redirection) {
49-
return <ErrorPage />
41+
// Renders route or its replacement/proxy
42+
const createRender = (renderRoute: RouteDescription): React.FC =>
43+
function render() {
44+
const Component: React.FC = renderRoute.component
45+
const redirect = mustRedirect(renderRoute)
46+
if (redirect && !!renderRoute.redirection) {
47+
return <Redirect to={renderRoute.redirection} />
48+
}
49+
if (redirect && !renderRoute.redirection) {
50+
return <ErrorPage />
51+
}
52+
return (
53+
<RouteLayout>
54+
<Component />
55+
</RouteLayout>
56+
)
5057
}
51-
return (
52-
<RouteLayout>
53-
<Component />
54-
</RouteLayout>
55-
)
56-
}
5758

5859
// A function that returns if a route can be displayed
59-
const canRenderFunction = () => !mustRedirect()
60+
const createCanRender = (canRenderRoute: RouteDescription) => () => !mustRedirect(canRenderRoute)
6061

6162
return {
62-
route,
63-
render: renderFunction,
64-
canRender: canRenderFunction,
63+
route: route.route,
64+
render: createRender(route),
65+
canRender: createCanRender(route),
6566
}
6667
}
6768

68-
export { getRouteType, getRouters, createRoute }
69+
const createRoutes = (routes: RouteDescription[]) => routes.map((route) => createRoute(route))
70+
71+
export { getRouteType, getRouters, createRoutes }

0 commit comments

Comments
 (0)