From 8e0b31abd47ca8790e3d88fc447ac4d06f864454 Mon Sep 17 00:00:00 2001
From: Alberto Defendi <1369-ahl-berto@users.noreply.gitlab.inf.unibz.it>
Date: Wed, 14 Apr 2021 11:32:24 +0200
Subject: [PATCH] Rename into api and define permitted routes in enum

---
 src/App.tsx                                            |  9 +++++----
 src/components/LandingPage/LandingPage.tsx             |  3 ++-
 .../{utils => api}/PrivateRoute/PrivateRoute.tsx       |  3 ++-
 src/components/api/routes.ts                           | 10 ++++++++++
 4 files changed, 19 insertions(+), 6 deletions(-)
 rename src/components/{utils => api}/PrivateRoute/PrivateRoute.tsx (89%)
 create mode 100644 src/components/api/routes.ts

diff --git a/src/App.tsx b/src/App.tsx
index 65acb82..0a7b221 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -3,15 +3,16 @@ import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
 import { HomePage } from 'components/HomePage/HomePage';
 import { AuthUser } from 'components/AuthUser/AuthUser';
 import { LandingPage } from 'components/LandingPage/LandingPage';
-import { PrivateRoute } from 'components/utils/PrivateRoute/PrivateRoute';
+import { PrivateRoute } from 'components/api/PrivateRoute/PrivateRoute';
+import { AuthRoutes, NonAuthRoutes } from 'components/api/routes';
 
 export const App: FC = () => (
   <Router>
     <div data-testid="App">
       <Switch>
-        <Route path="/auth" component={AuthUser} />
-        <Route exact path="/" component={LandingPage} />
-        <PrivateRoute path="/home" Component={HomePage} />
+        <Route path={NonAuthRoutes.login} component={AuthUser} />
+        <Route exact path={NonAuthRoutes.home} component={LandingPage} />
+        <PrivateRoute path={AuthRoutes.dashboard} Component={HomePage} />
       </Switch>
     </div>
   </Router>
diff --git a/src/components/LandingPage/LandingPage.tsx b/src/components/LandingPage/LandingPage.tsx
index 01cd0fe..df5cc0b 100644
--- a/src/components/LandingPage/LandingPage.tsx
+++ b/src/components/LandingPage/LandingPage.tsx
@@ -1,11 +1,12 @@
 import React, { FC } from 'react';
 import Button from '@material-ui/core/Button';
 import { Link } from 'react-router-dom';
+import { NonAuthRoutes } from 'components/api/routes';
 
 export const LandingPage: FC = () => (
   <>
     <Button variant="contained">
-      <Link to="/auth">Login</Link>
+      <Link to={NonAuthRoutes.login}>Login</Link>
     </Button>
     <section>
       <h2>What is MoveAid?</h2>
diff --git a/src/components/utils/PrivateRoute/PrivateRoute.tsx b/src/components/api/PrivateRoute/PrivateRoute.tsx
similarity index 89%
rename from src/components/utils/PrivateRoute/PrivateRoute.tsx
rename to src/components/api/PrivateRoute/PrivateRoute.tsx
index 9492da9..89c5962 100644
--- a/src/components/utils/PrivateRoute/PrivateRoute.tsx
+++ b/src/components/api/PrivateRoute/PrivateRoute.tsx
@@ -1,5 +1,6 @@
 import React from 'react';
 import { Route, Redirect, RouteProps } from 'react-router-dom';
+import { NonAuthRoutes } from 'components/api/routes';
 
 /**
  * A wrapper for <Route> that redirects to the login screen if you're not yet authenticated.
@@ -24,7 +25,7 @@ export const PrivateRoute = ({ Component, path }: Props): JSX.Element => {
         ) : (
           <Redirect
             to={{
-              pathname: '/auth',
+              pathname: NonAuthRoutes.login,
               state: {
                 message,
                 requestedPath: path,
diff --git a/src/components/api/routes.ts b/src/components/api/routes.ts
new file mode 100644
index 0000000..52ee598
--- /dev/null
+++ b/src/components/api/routes.ts
@@ -0,0 +1,10 @@
+export enum AuthRoutes {
+  dashboard = '/dashboard',
+  account = '/account',
+}
+
+export enum NonAuthRoutes {
+  home = '/',
+  login = '/login',
+  unauthorized = '/unauthorized',
+}
-- 
GitLab