From da2476e2bdc4f85288e96b02df73c2123ad28e89 Mon Sep 17 00:00:00 2001 From: Onitoxan Date: Mon, 16 Dec 2024 11:09:55 +0100 Subject: [PATCH] Refactor `BrowserRouter` to `RouterProvider` for hpc-cdm https://reactrouter.com/6.28.0/components/routes#routes In `hpc-cdm` there are many `` elements, for the way is built it makes sense to let these `` where they are instead of briging all of them to `main.tsx` since we have logic inside these ``. In documentation they say that from `v6` and on, it is an 'uncommon practice', but for the way this was thought out, it would need a major rewrite or to let as it is for readability and maintenence ease purpose. --- apps/hpc-cdm/src/app/app.tsx | 34 +----------- apps/hpc-cdm/src/app/pages/admin.tsx | 82 +++++++++++++++------------- apps/hpc-cdm/src/main.tsx | 43 ++++++++++++--- 3 files changed, 81 insertions(+), 78 deletions(-) diff --git a/apps/hpc-cdm/src/app/app.tsx b/apps/hpc-cdm/src/app/app.tsx index 989cb596..1bdfe9e7 100644 --- a/apps/hpc-cdm/src/app/app.tsx +++ b/apps/hpc-cdm/src/app/app.tsx @@ -1,5 +1,5 @@ import React, { useState, useEffect } from 'react'; -import { Navigate, Route, Routes } from 'react-router'; +import { Outlet } from 'react-router'; import { ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; @@ -11,13 +11,8 @@ import { LANGUAGE_CHOICE, LanguageKey, t } from '../i18n'; import { Z_INDEX } from './layout'; import * as paths from './paths'; import PageMeta from './components/page-meta'; -import { RouteParamsValidator } from './components/route-params-validator'; -import PageAdmin from './pages/admin'; import PageNotLoggedIn from './pages/not-logged-in'; -import PageNotFound from './pages/not-found'; -import PageOperationsList from './pages/operations-list'; -import PageOperation from './pages/operation'; const environmentWarning = (env: Environment, lang: LanguageKey) => { const warning = env.getDevHeaderWarning(lang); @@ -143,32 +138,7 @@ export const App = () => { : []), ]} /> - - } - /> - } - /> - } - routeParam="id" - /> - } - /> - {canModifyGlobalUserAccess && ( - } - /> - )} - } /> - + ) : ( <> diff --git a/apps/hpc-cdm/src/app/pages/admin.tsx b/apps/hpc-cdm/src/app/pages/admin.tsx index 5e1db298..9d43ffa2 100644 --- a/apps/hpc-cdm/src/app/pages/admin.tsx +++ b/apps/hpc-cdm/src/app/pages/admin.tsx @@ -12,44 +12,50 @@ import PageMeta from '../components/page-meta'; const PageAdmin = () => { return ( - {({ lang }) => ( -
- s.navigation.admin)]} /> - s.navigation.manageAccess), - path: paths.adminAccess(), - }, - ]} - > - - } - /> - - } - /> - s.components.notFound)} - /> - } - /> - - -
- )} + {({ lang, access }) => { + const { canModifyGlobalUserAccess } = access().permissions; + if (!canModifyGlobalUserAccess) { + return ; + } + return ( +
+ s.navigation.admin)]} /> + s.navigation.manageAccess), + path: paths.adminAccess(), + }, + ]} + > + + } + /> + + } + /> + s.components.notFound)} + /> + } + /> + + +
+ ); + }}
); }; diff --git a/apps/hpc-cdm/src/main.tsx b/apps/hpc-cdm/src/main.tsx index 8e1a4d8f..6b599fa8 100644 --- a/apps/hpc-cdm/src/main.tsx +++ b/apps/hpc-cdm/src/main.tsx @@ -1,10 +1,17 @@ import React from 'react'; import ReactDOM from 'react-dom/client'; -import { BrowserRouter } from 'react-router'; +import { createBrowserRouter, Navigate, RouterProvider } from 'react-router'; import { ThemeProvider } from '@unocha/hpc-ui'; import App from './app/app'; import './assets/styles/enketo.css'; +import { RouteParamsValidator } from './app/components/route-params-validator'; +import * as paths from './app/paths'; + +import PageAdmin from './app/pages/admin'; +import PageNotFound from './app/pages/not-found'; +import PageOperationsList from './app/pages/operations-list'; +import PageOperation from './app/pages/operation'; const rootElement = document.getElementById('root'); if (!rootElement) { @@ -12,10 +19,30 @@ if (!rootElement) { } const root = ReactDOM.createRoot(rootElement); -root.render( - - - - - -); +const router = createBrowserRouter([ + { + path: paths.home(), + element: ( + + + + ), + children: [ + { path: paths.home(), element: }, + { path: paths.operations(), element: }, + { + path: paths.operationRoot(), + element: ( + } routeParam="id" /> + ), + }, + { + path: paths.adminRoot(), + element: , + }, + { path: paths.root(), element: }, + ], + }, +]); + +root.render();