From 52e4525970bf476396721582b0b2d6bd5a61d7be Mon Sep 17 00:00:00 2001 From: "Samuel T." Date: Wed, 4 Dec 2024 16:48:52 -0500 Subject: [PATCH] Add more paths handling (#315) - Make user management tabs actual pages - Standalone terms-and-conditions page - Auto redirect non-existing page - Change "CANOPEUM" text for username in user management sidenav --- .../src/components/MainLayout.tsx | 11 +++- canopeum_frontend/src/components/Navbar.tsx | 4 +- .../src/constants/routes.constant.ts | 10 +++- .../src/pages/UserManagement.tsx | 53 ++++++++++--------- 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/canopeum_frontend/src/components/MainLayout.tsx b/canopeum_frontend/src/components/MainLayout.tsx index 1c80dd410..690399657 100644 --- a/canopeum_frontend/src/components/MainLayout.tsx +++ b/canopeum_frontend/src/components/MainLayout.tsx @@ -3,6 +3,7 @@ import { Navigate, Outlet, Route, Routes } from 'react-router-dom' import { AuthenticationContext } from './context/AuthenticationContext' import Navbar from './Navbar' +import TermsAndPolicies from '@components/settings/TermsAndPolicies' import { appRoutes } from '@constants/routes.constant' import Analytics from '@pages/Analytics' import AnalyticsSite from '@pages/AnalyticsSite' @@ -68,15 +69,21 @@ const MainLayout = () => { } path='/home' /> } path='/sites' /> } path='/sites/:siteId' /> - } path='/user-management' /> + } path='/user-management/*' /> } path='/utilities' /> - } path='*' /> {/* The following routes are accessible to Visitors without any authentication */} + } path='/terms-and-policies' /> } path='/sites/:siteId/social' /> } path='/posts/:postId' /> } path='/map' /> + + {/* Fallback route */} + } + path='*' + /> ) diff --git a/canopeum_frontend/src/components/Navbar.tsx b/canopeum_frontend/src/components/Navbar.tsx index 4f6c7d3b0..eec70785a 100644 --- a/canopeum_frontend/src/components/Navbar.tsx +++ b/canopeum_frontend/src/components/Navbar.tsx @@ -112,11 +112,11 @@ const Navbar = () => { {isAuthenticated && ( account_circle {translate('navbar.settings')} diff --git a/canopeum_frontend/src/constants/routes.constant.ts b/canopeum_frontend/src/constants/routes.constant.ts index 5e2fbf66f..37f6c5d40 100644 --- a/canopeum_frontend/src/constants/routes.constant.ts +++ b/canopeum_frontend/src/constants/routes.constant.ts @@ -6,7 +6,13 @@ export const appRoutes = { sites: '/sites', site: (siteId: number) => `/sites/${siteId}`, siteSocial: (siteId: number) => `/sites/${siteId}/social`, - userManagment: '/user-management', + termsAndPolicies: '/terms-and-policies', + userManagment: { + '': '/user-management', + myProfile: '/user-management/my-profile', + manageAdmins: '/user-management/manage-admins', + termsAndPolicies: '/user-management/terms-and-policies', + }, // utilities: '/utilities', // For development purposes map: '/map', -} +} as const diff --git a/canopeum_frontend/src/pages/UserManagement.tsx b/canopeum_frontend/src/pages/UserManagement.tsx index 33d30d871..35660fa67 100644 --- a/canopeum_frontend/src/pages/UserManagement.tsx +++ b/canopeum_frontend/src/pages/UserManagement.tsx @@ -1,29 +1,33 @@ import './UserManagement.scss' -import { useContext, useState } from 'react' +import { useContext } from 'react' import { useTranslation } from 'react-i18next' +import { Navigate, Route, Routes, useLocation, useNavigate } from 'react-router-dom' import { AuthenticationContext } from '@components/context/AuthenticationContext' import EditProfile from '@components/settings/EditProfile' import ManageAdmins from '@components/settings/ManageAdmins' import SettingsTab from '@components/settings/SettingsTab' import TermsAndPolicies from '@components/settings/TermsAndPolicies' +import { appRoutes } from '@constants/routes.constant' import type { RoleEnum } from '@services/api' -type UserManagementTab = 'editProfile' | 'logout' | 'manageAdmins' | 'termsAndPolicies' +type UserManagementTab = + | (typeof appRoutes.userManagment)[keyof Omit] + | 'logout' const tabs: { type: UserManagementTab, translationKey: string, roles?: RoleEnum[] }[] = [ { - type: 'editProfile', + type: appRoutes.userManagment.myProfile, translationKey: 'settings.tabs.edit-profile', }, { - type: 'manageAdmins', + type: appRoutes.userManagment.manageAdmins, translationKey: 'settings.tabs.manage-admins', roles: ['MegaAdmin' as RoleEnum], }, { - type: 'termsAndPolicies', + type: appRoutes.userManagment.termsAndPolicies, translationKey: 'settings.tabs.terms-and-policies', }, { @@ -35,28 +39,17 @@ const tabs: { type: UserManagementTab, translationKey: string, roles?: RoleEnum[ const UserManagement = () => { const { t: translate } = useTranslation() const { logout, currentUser } = useContext(AuthenticationContext) - const [selectedTab, setSelectedTab] = useState('editProfile') + const navigate = useNavigate() + const location = useLocation() - const displayTabContent = () => { - if (selectedTab === 'termsAndPolicies') { - return - } - - if (selectedTab === 'manageAdmins' && currentUser?.role === 'MegaAdmin') { - return - } - - return - } - - const onTabClick = (tabType: UserManagementTab) => { - if (tabType === 'logout') { + const onTabClick = (tabPath: UserManagementTab) => { + if (tabPath === 'logout') { logout() return } - setSelectedTab(tabType) + navigate(tabPath) } const tabsDisplay = () => @@ -66,7 +59,7 @@ const UserManagement = () => { onTabClick(tab.type)} - selected={selectedTab === tab.type} + selected={location.pathname === tab.type} > {translate(tab.translationKey)} @@ -78,7 +71,11 @@ const UserManagement = () => {
-

CANOPEUM

+

+ {currentUser?.role === 'MegaAdmin' + ? 'CANOPEUM' + : currentUser?.username} +

{tabsDisplay()}
@@ -86,7 +83,15 @@ const UserManagement = () => {
- {displayTabContent()} + + } path='/my-profile' /> + } path='/manage-admins' /> + } path='/terms-and-policies' /> + } + path='*' + /> +