From a30d6b76d3b72e6b60bd7da7a5a38a0961d95671 Mon Sep 17 00:00:00 2001 From: William Petit Date: Sun, 9 Aug 2020 10:51:02 +0200 Subject: [PATCH] feat(ui): client side private routes --- client/src/components/App.tsx | 25 +++++++++++++------ .../components/DashboardPage/Dashboard.tsx | 19 ++++++++++++++ .../DashboardPage/DashboardPage.tsx | 13 ++++++++++ .../DashboardPage/EstimationPanel.tsx | 20 +++++++++++++++ .../{HomePage => DashboardPage}/ItemPanel.tsx | 0 client/src/components/HomePage/Dashboard.tsx | 15 ----------- client/src/components/HomePage/HomePage.tsx | 20 ++++++++------- .../components/HomePage/WelcomeContent.tsx | 2 +- client/src/components/Navbar.tsx | 7 +++--- client/src/components/PrivateRoute.tsx | 18 +++++++++++++ .../components/ProfilePage/ProfilePage.tsx | 14 ++++++----- client/src/hooks/useLoggedIn.tsx | 7 ++++++ 12 files changed, 119 insertions(+), 41 deletions(-) create mode 100644 client/src/components/DashboardPage/Dashboard.tsx create mode 100644 client/src/components/DashboardPage/DashboardPage.tsx create mode 100644 client/src/components/DashboardPage/EstimationPanel.tsx rename client/src/components/{HomePage => DashboardPage}/ItemPanel.tsx (100%) delete mode 100644 client/src/components/HomePage/Dashboard.tsx create mode 100644 client/src/components/PrivateRoute.tsx create mode 100644 client/src/hooks/useLoggedIn.tsx diff --git a/client/src/components/App.tsx b/client/src/components/App.tsx index af9e243..2bbe2ed 100644 --- a/client/src/components/App.tsx +++ b/client/src/components/App.tsx @@ -1,18 +1,29 @@ -import React from 'react'; +import React, { FunctionComponent, useState, useEffect } from 'react'; import { BrowserRouter, Route, Redirect, Switch } from "react-router-dom"; import { HomePage } from './HomePage/HomePage'; import { ProfilePage } from './ProfilePage/ProfilePage'; +import { DashboardPage } from './DashboardPage/DashboardPage'; +import { PrivateRoute } from './PrivateRoute'; +import { useLoggedIn, LoggedInContext } from '../hooks/useLoggedIn'; +import { useUserProfile } from '../gql/queries/user'; -export class App extends React.Component { - render() { - return ( +export interface AppProps { + +} + +export const App: FunctionComponent = () => { + const { user } = useUserProfile(); + + return ( + - + + } /> - ); - } + + ); } \ No newline at end of file diff --git a/client/src/components/DashboardPage/Dashboard.tsx b/client/src/components/DashboardPage/Dashboard.tsx new file mode 100644 index 0000000..8a87750 --- /dev/null +++ b/client/src/components/DashboardPage/Dashboard.tsx @@ -0,0 +1,19 @@ +import React from 'react'; +import { EstimationPanel } from './EstimationPanel'; + +export function Dashboard() { + return ( +
+
+
+ +
+
+
+
+ +
+
+
+ ); +} \ No newline at end of file diff --git a/client/src/components/DashboardPage/DashboardPage.tsx b/client/src/components/DashboardPage/DashboardPage.tsx new file mode 100644 index 0000000..a7a3509 --- /dev/null +++ b/client/src/components/DashboardPage/DashboardPage.tsx @@ -0,0 +1,13 @@ +import React from 'react'; +import { Page } from '../Page'; +import { Dashboard } from './Dashboard'; + +export function DashboardPage() { + return ( + +
+ +
+
+ ); +} \ No newline at end of file diff --git a/client/src/components/DashboardPage/EstimationPanel.tsx b/client/src/components/DashboardPage/EstimationPanel.tsx new file mode 100644 index 0000000..7090716 --- /dev/null +++ b/client/src/components/DashboardPage/EstimationPanel.tsx @@ -0,0 +1,20 @@ +import React, { FunctionComponent } from "react"; +import { ItemPanel } from "./ItemPanel"; + +export interface EstimationPanelProps { + +} + +export const EstimationPanel: FunctionComponent = () => { + return ( + { return item.id }} + itemLabel={(item) => { return item.id }} + itemUrl={(item) => { return `/estimations/${item.id}` }} + /> + ); +}; \ No newline at end of file diff --git a/client/src/components/HomePage/ItemPanel.tsx b/client/src/components/DashboardPage/ItemPanel.tsx similarity index 100% rename from client/src/components/HomePage/ItemPanel.tsx rename to client/src/components/DashboardPage/ItemPanel.tsx diff --git a/client/src/components/HomePage/Dashboard.tsx b/client/src/components/HomePage/Dashboard.tsx deleted file mode 100644 index b7a1c6e..0000000 --- a/client/src/components/HomePage/Dashboard.tsx +++ /dev/null @@ -1,15 +0,0 @@ -import React from 'react'; - -export function Dashboard() { - return ( -
-
-
-
-
-
- -
-
- ); -} \ No newline at end of file diff --git a/client/src/components/HomePage/HomePage.tsx b/client/src/components/HomePage/HomePage.tsx index 2f7cb1f..60762be 100644 --- a/client/src/components/HomePage/HomePage.tsx +++ b/client/src/components/HomePage/HomePage.tsx @@ -1,20 +1,22 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { Page } from '../Page'; -import { Dashboard } from './Dashboard'; import { useUserProfile } from '../../gql/queries/user'; import { WelcomeContent } from './WelcomeContent'; +import { useHistory } from 'react-router'; +import { useLoggedIn } from '../../hooks/useLoggedIn'; export function HomePage() { - const { user, loading } = useUserProfile(); + const loggedIn = useLoggedIn(); + const history = useHistory(); + + useEffect(() => { + if (loggedIn) history.push('/dashboard'); + }, [loggedIn]); return ( - +
- { - user.id ? - : - - } +
); diff --git a/client/src/components/HomePage/WelcomeContent.tsx b/client/src/components/HomePage/WelcomeContent.tsx index b126de0..f9be7bc 100644 --- a/client/src/components/HomePage/WelcomeContent.tsx +++ b/client/src/components/HomePage/WelcomeContent.tsx @@ -61,7 +61,7 @@ export const WelcomeContent: FunctionComponent = () => {
-

Partager simplement vos estimations.

+

Partagez simplement vos estimations.

Une adresse courriel et votre estimation est partagée ! Le niveau d'accès (lecture seule, écriture, vue partielle...) peut être défini pour chaque partage.

{/*

En savoir plus

*/}
diff --git a/client/src/components/Navbar.tsx b/client/src/components/Navbar.tsx index 4e9f700..fa8a634 100644 --- a/client/src/components/Navbar.tsx +++ b/client/src/components/Navbar.tsx @@ -3,9 +3,10 @@ import { Config } from '../config'; import { Link } from 'react-router-dom'; import { useUserProfile } from '../gql/queries/user'; import { WithLoader } from './WithLoader'; +import { useLoggedIn } from '../hooks/useLoggedIn'; export function Navbar() { - const { user, loading } = useUserProfile(); + const loggedIn = useLoggedIn(); const [ isActive, setActive ] = useState(false); const toggleMenu = () => { @@ -16,7 +17,7 @@ export function Navbar() {