2021-01-17 12:54:49 -08:00
|
|
|
import React from 'react';
|
|
|
|
import { Switch, Route, RouteProps, Redirect } from 'react-router-dom';
|
|
|
|
import { useReactiveVar } from '@apollo/client';
|
2021-01-25 13:29:23 -08:00
|
|
|
import { BrowseResultsPage } from './browse/BrowseResultsPage';
|
2021-01-17 12:54:49 -08:00
|
|
|
import { LogIn } from './auth/LogIn';
|
|
|
|
import { NoPageFound } from './shared/NoPageFound';
|
2021-02-03 11:49:51 -08:00
|
|
|
import { EntityPage } from './entity/EntityPage';
|
2021-01-17 12:54:49 -08:00
|
|
|
import { PageRoutes } from '../conf/Global';
|
2021-02-03 11:49:51 -08:00
|
|
|
import { useEntityRegistry } from './useEntityRegistry';
|
2021-02-23 12:45:42 -08:00
|
|
|
import { HomePage } from './home/HomePage';
|
|
|
|
import { SearchPage } from './search/SearchPage';
|
2021-03-11 13:38:35 -08:00
|
|
|
import { isLoggedInVar } from './auth/checkAuthStatus';
|
2021-05-11 15:41:42 -07:00
|
|
|
import { useTrackPageView } from './analytics';
|
|
|
|
import { AnalyticsPage } from './analyticsDashboard/components/AnalyticsPage';
|
2021-01-17 12:54:49 -08:00
|
|
|
|
|
|
|
const ProtectedRoute = ({
|
|
|
|
isLoggedIn,
|
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
isLoggedIn: boolean;
|
|
|
|
} & RouteProps) => {
|
2021-08-04 11:55:03 -07:00
|
|
|
const currentPath = window.location.pathname + window.location.search;
|
2021-01-17 12:54:49 -08:00
|
|
|
if (!isLoggedIn) {
|
2021-08-04 11:55:03 -07:00
|
|
|
window.location.replace(`${PageRoutes.AUTHENTICATE}?redirect_uri=${encodeURIComponent(currentPath)}`);
|
2021-03-11 13:38:35 -08:00
|
|
|
return null;
|
2021-01-17 12:54:49 -08:00
|
|
|
}
|
|
|
|
return <Route {...props} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Container for all views behind an authentication wall.
|
|
|
|
*/
|
|
|
|
export const Routes = (): JSX.Element => {
|
2021-05-11 15:41:42 -07:00
|
|
|
useTrackPageView();
|
2021-01-17 12:54:49 -08:00
|
|
|
const isLoggedIn = useReactiveVar(isLoggedInVar);
|
2021-02-03 11:49:51 -08:00
|
|
|
const entityRegistry = useEntityRegistry();
|
|
|
|
|
2021-01-17 12:54:49 -08:00
|
|
|
return (
|
2021-08-31 22:00:56 -07:00
|
|
|
<>
|
2021-01-17 12:54:49 -08:00
|
|
|
<Switch>
|
2021-02-23 12:45:42 -08:00
|
|
|
<ProtectedRoute isLoggedIn={isLoggedIn} exact path="/" render={() => <HomePage />} />
|
2021-03-11 13:38:35 -08:00
|
|
|
|
2021-01-17 12:54:49 -08:00
|
|
|
<Route path={PageRoutes.LOG_IN} component={LogIn} />
|
2021-02-03 11:49:51 -08:00
|
|
|
{entityRegistry.getEntities().map((entity) => (
|
|
|
|
<ProtectedRoute
|
2021-02-05 14:21:04 -08:00
|
|
|
key={entity.getPathName()}
|
2021-02-03 11:49:51 -08:00
|
|
|
isLoggedIn={isLoggedIn}
|
|
|
|
path={`/${entity.getPathName()}/:urn`}
|
|
|
|
render={() => <EntityPage entityType={entity.type} />}
|
|
|
|
/>
|
|
|
|
))}
|
2021-02-01 11:50:10 -08:00
|
|
|
<ProtectedRoute
|
|
|
|
isLoggedIn={isLoggedIn}
|
|
|
|
path={PageRoutes.SEARCH_RESULTS}
|
|
|
|
render={() => <SearchPage />}
|
|
|
|
/>
|
2021-01-25 13:29:23 -08:00
|
|
|
<ProtectedRoute
|
|
|
|
isLoggedIn={isLoggedIn}
|
|
|
|
path={PageRoutes.BROWSE_RESULTS}
|
|
|
|
render={() => <BrowseResultsPage />}
|
|
|
|
/>
|
2021-05-11 15:41:42 -07:00
|
|
|
<ProtectedRoute isLoggedIn={isLoggedIn} path={PageRoutes.ANALYTICS} render={() => <AnalyticsPage />} />
|
2021-03-02 10:52:57 -08:00
|
|
|
{/* Starting the react app locally opens /assets by default. For a smoother dev experience, we'll redirect to the homepage */}
|
|
|
|
<Route path={PageRoutes.ASSETS} component={() => <Redirect to="/" />} exact />
|
2021-01-17 12:54:49 -08:00
|
|
|
<Route component={NoPageFound} />
|
|
|
|
</Switch>
|
2021-08-31 22:00:56 -07:00
|
|
|
</>
|
2021-01-17 12:54:49 -08:00
|
|
|
);
|
|
|
|
};
|