Skip to content
Snippets Groups Projects
App.tsx 4.87 KiB
import React, { ReactElement, useEffect } from "react";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import Landing from "./pages/Landing";
import ExternalPageNavBar from "./shared/ExternalPageNavBar";
import GeantFooter from "./components/global/GeantFooter";
import BudgetPage from "./pages/Budget";
import CompendiumData from "./pages/CompendiumData";
import FundingSourcePage from "./pages/FundingSource";
import ChargingStructurePage from "./pages/ChargingStructure";
import StaffGraph from "./pages/StaffGraph";
import StaffGraphAbsolute from "./pages/StaffGraphAbsolute";
import SubOrganisation from "./pages/SubOrganisation";
import ParentOrganisation from "./pages/ParentOrganisation";
import ECProjects from "./pages/ECProjects";
import Providers from "./Providers";
import PolicyPage from "./pages/Policy";
import TrafficVolumePage from "./pages/TrafficVolumePerNren";
import ConnectedInstitutionsURLs from "./pages/ConnectedInstitutionsURLs";
import ServicesPage from "./pages/Services";
import {ConnectivityCategory, ServiceCategory} from "./Schema";
import ConnectedUserPage from "./pages/ConnectedUser";
import FibreLightPage from "./pages/FibreLight";
import MonitoringToolsPage from "./pages/MonitoringTools";

const router = createBrowserRouter([
  { path: "/budget", element: <BudgetPage /> },
  { path: "/funding", element: <FundingSourcePage /> },
  { path: "/data/employment", element: <StaffGraph /> },
  { path: "/data/roles", element: <StaffGraph roles /> },
  { path: "/employee-count", element: <StaffGraphAbsolute /> },
  { path: "/charging", element: <ChargingStructurePage /> },
  { path: "/suborganisations", element: <SubOrganisation /> },
  { path: "/parentorganisation", element: <ParentOrganisation /> },
  { path: "/ec-projects", element: <ECProjects /> },
  { path: "/policy", element: <PolicyPage /> },
  { path: "/traffic-volume", element: <TrafficVolumePage /> },
  { path: "/data", element: <CompendiumData /> },
  { path: "/institutions-urls", element: <ConnectedInstitutionsURLs /> },
  { path: "/connected-proportion", element: <ConnectedUserPage connectivity_category={ConnectivityCategory.ConnectedProportion.toString()} /> },
  { path: "/connectivity-level", element: <ConnectedUserPage connectivity_category={ConnectivityCategory.ConnectivityLevel.toString()} /> },
  { path: "/connectivity-growth", element: <ConnectedUserPage connectivity_category={ConnectivityCategory.ConnectivityGrowth.toString()} /> },
  { path: "/connection-carrier", element: <ConnectedUserPage connectivity_category={ConnectivityCategory.ConnectionCarrier.toString()} /> },
  { path: "/connectivity-load", element: <ConnectedUserPage connectivity_category={ConnectivityCategory.ConnectivityLoad.toString()} /> },
  { path: "/commercial-charging-level", element: <ConnectedUserPage connectivity_category={ConnectivityCategory.CommercialChargingLevel.toString()} /> },
  { path: "/commercial-connectivity", element: <ConnectedUserPage connectivity_category={ConnectivityCategory.CommercialConnectivity.toString()} /> },
  { path: "/network-services", element: <ServicesPage category={ServiceCategory.network_services} /> },
  { path: "/isp-support-services", element: <ServicesPage category={ServiceCategory.isp_support} /> },
  { path: "/security-services", element: <ServicesPage category={ServiceCategory.security} /> },
  { path: "/identity-services", element: <ServicesPage category={ServiceCategory.identity} /> },
  { path: "/collaboration-services", element: <ServicesPage category={ServiceCategory.collaboration} /> },
  { path: "/multimedia-services", element: <ServicesPage category={ServiceCategory.multimedia} /> },
  { path: "/storage-and-hosting-services", element: <ServicesPage category={ServiceCategory.storage_and_hosting} /> },
  { path: "/professional-services", element: <ServicesPage category={ServiceCategory.professional_services} /> },
  { path: "/fibre-light", element: <FibreLightPage /> },
  { path: "/monitoring-tools", element: <MonitoringToolsPage /> },
  { path: "*", element: <Landing /> },
]);


function App(): ReactElement {
  const [isAdmin, setIsAdmin] = React.useState(false);

  useEffect(() => {
    // we have to do this because the user provider is not available before rendering other parts of the app
    // so fetch the user and redirect if not admin as a temporary measure, until it's "generally available"
    fetch('/api/user/').then(data => data.json()).then(user => {
      setIsAdmin(user.permissions.admin)
      if (!user.permissions.admin) {
        window.location.replace('/survey')
      } else {
        console.log('Admin user, not redirecting')
      }
    }).catch(() => {
      window.location.replace('/survey')
    })
  }, [])

  if (!isAdmin) {
    return <></>
  }
  return (
    <div className="app">
      <Providers>
        <ExternalPageNavBar />
        <RouterProvider router={router} />
      </Providers>
      <GeantFooter />
    </div>
  );
}

export default App;