Fix redirections

pull/2529/head
Jérémy Rialland 2023-03-02 17:15:22 +01:00 committed by Johan Girod
parent 48fcd735a9
commit 2aaf28a470
3 changed files with 107 additions and 50 deletions

View File

@ -3,7 +3,7 @@ import { FallbackRender } from '@sentry/react/types/errorboundary'
import rules from 'modele-social'
import { ComponentProps, StrictMode, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { Navigate, Route, Routes } from 'react-router-dom'
import { Route, Routes } from 'react-router-dom'
import styled, { css } from 'styled-components'
import Route404 from '@/components/Route404'
@ -37,6 +37,7 @@ import Integration from '@/pages/integration/index'
import { useSitePaths } from '@/sitePaths'
import Provider, { ProviderProps } from './Provider'
import Redirections from './Redirections'
type RootProps = {
basename: ProviderProps['basename']
@ -71,51 +72,6 @@ export default function Root({
)
}
const Redirections = ({ children }: { children: React.ReactNode }) => {
const { absoluteSitePaths } = useSitePaths()
const redirections = [
{
paths: ['/stats'],
to: absoluteSitePaths.stats,
},
{
paths: ['/plan-de-site', '/site-map'],
to: absoluteSitePaths.plan,
},
{
paths: [
'/gérer/aide-declaration-independants/beta',
'/manage/declaration-aid-independent/beta',
],
to: absoluteSitePaths.assistants.déclarationIndépendant.index,
},
{
paths: [
'/gérer/aide-declaration-independants',
'/manage/declaration-aid-independent',
],
to: absoluteSitePaths.assistants[
'déclaration-charges-sociales-indépendant'
],
},
] satisfies { paths: string[]; to: string }[]
return (
<Routes>
{redirections.flatMap(({ paths, to }) =>
paths.map((path) => (
<Route
key={path}
path={path}
element={<Navigate to={to} replace />}
/>
))
)}
<Route path="*" element={children} />
</Routes>
)
}
const Router = () => {
const engine = useEngine()

View File

@ -0,0 +1,85 @@
import { useMemo } from 'react'
import { Navigate, Route, Routes, useLocation } from 'react-router-dom'
import { useSitePaths } from '@/sitePaths'
interface Props {
children: React.ReactNode
}
export default function Redirections({ children }: Props) {
const { absoluteSitePaths } = useSitePaths()
const { pathname } = useLocation()
const redirections = useMemo(() => {
return [
{
paths: ['/stats'],
to: absoluteSitePaths.stats,
},
{
paths: ['/plan-de-site', '/site-map'],
to: absoluteSitePaths.plan,
},
{
paths: [
'/gérer/aide-declaration-independants/beta',
'/manage/declaration-aid-independent/beta',
],
to: absoluteSitePaths.assistants.déclarationIndépendant.index,
},
{
paths: [
'/gérer/aide-declaration-independants',
'/manage/declaration-aid-independent',
],
to: absoluteSitePaths.assistants[
'déclaration-charges-sociales-indépendant'
],
},
{
paths: [
'/gérer/aide-declaration-independants',
'/manage/declaration-aid-independent',
],
to: absoluteSitePaths.assistants[
'déclaration-charges-sociales-indépendant'
],
},
{
paths: ['/gérer/*', '/manage/*'],
to: decodeURI(pathname).replace(
/^\/(gérer|manage)/,
absoluteSitePaths.assistants.index
),
},
{
paths: ['/créer/*', '/create/*'],
to: decodeURI(pathname).replace(
/^\/(créer|create)/,
absoluteSitePaths.assistants['choix-du-statut'].index
),
},
] satisfies { paths: string[]; to: string }[]
}, [
absoluteSitePaths.assistants,
absoluteSitePaths.plan,
absoluteSitePaths.stats,
pathname,
])
return (
<Routes>
{redirections.flatMap(({ paths, to }) =>
paths.map((path) => (
<Route
key={path}
path={path}
element={<Navigate to={to} replace />}
/>
))
)}
<Route path="*" element={children} />
</Routes>
)
}

View File

@ -1,9 +1,8 @@
import { Trans } from 'react-i18next'
import { Route, Routes, useLocation } from 'react-router-dom'
import { Navigate, Route, Routes, useLocation } from 'react-router-dom'
import { TrackChapter } from '@/components/ATInternetTracking'
import PageData from '@/components/PageData'
import Route404 from '@/components/Route404'
import { ScrollToTop } from '@/components/utils/Scroll'
import { Link } from '@/design-system/typography/link'
import useSimulatorsData from '@/hooks/useSimulatorsData'
@ -29,7 +28,13 @@ export default function Gérer() {
<ScrollToTop key={location.pathname} />
<Routes>
{/* <Route index element={<Home />} /> Navigate to /simu-et-assist */}
{/* TODO Navigate to /simu-et-assist */}
<Route
index
element={
<Navigate to={absoluteSitePaths.simulateurs.index} replace />
}
/>
<Route
path={relativeSitePaths.assistants.sécuritéSociale}
element={
@ -72,7 +77,18 @@ export default function Gérer() {
}
/>
))}
<Route path="*" element={<Route404 />} />
<Route
path="*"
element={
<Navigate
to={decodeURI(location.pathname).replace(
absoluteSitePaths.assistants.index,
absoluteSitePaths.assistants['pour-mon-entreprise'].index
)}
replace
/>
}
/>
</Routes>
</>
)