|
1 | | -import React, { Component } from 'react'; |
2 | | -import { Route, Switch, Redirect, withRouter } from 'react-router-dom'; |
3 | | - |
4 | | -import withState from './../utils/withState'; |
5 | | - |
6 | | -import PrivateRoute from './utils/PrivateRoute'; |
7 | | - |
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import { Route, Switch, Redirect, useHistory } from 'react-router-dom'; |
| 3 | +import { useAuthState, useAuthDispatch } from './providers/authProvider'; |
| 4 | +import PrivateRoute from './components/PrivateRoute'; |
8 | 5 | import Header from './components/Header'; |
9 | | -import Dashboard from './pages/Dashboard/'; |
10 | | -import Landing from './pages/Landing/'; |
11 | | -import Login from './pages/Login/'; |
12 | | -import Register from './pages/Register/'; |
13 | | -import NotFound from './pages/NotFound/'; |
14 | | - |
15 | | -class App extends Component { |
16 | | - componentDidMount() { |
17 | | - this.unlisten = this.props.history.listen((location, action) => { |
18 | | - if (this.props.store.error !== '') { |
19 | | - this.props.actions.clearErrors(); |
20 | | - } |
21 | | - }); |
22 | | - } |
23 | | - |
24 | | - componentWillUnmount() { |
25 | | - this.unlisten(); |
26 | | - } |
| 6 | +import Dashboard from './pages/Dashboard'; |
| 7 | +import Landing from './pages/Landing/landing'; |
| 8 | +import Login from './pages/Login'; |
| 9 | +import Register from './pages/Register'; |
| 10 | +import NotFound from './pages/NotFound/notFound'; |
27 | 11 |
|
28 | | - render() { |
29 | | - const { |
30 | | - store: { isLoggedIn } |
31 | | - } = this.props; |
| 12 | +const App = () => { |
| 13 | + const { onClearErrors } = useAuthDispatch(); |
| 14 | + const { isLoggedIn, error } = useAuthState(); |
32 | 15 |
|
33 | | - return ( |
34 | | - <div className="App"> |
35 | | - <Header /> |
| 16 | + const history = useHistory(); |
36 | 17 |
|
37 | | - <Switch> |
38 | | - <Route exact path="/" component={Landing} /> |
39 | | - <PrivateRoute path="/dashboard" component={Dashboard} /> |
40 | | - <Route |
41 | | - path="/login" |
42 | | - render={() => |
43 | | - isLoggedIn ? ( |
44 | | - <Redirect to="/dashboard" /> |
45 | | - ) : ( |
46 | | - <Login /> |
47 | | - ) |
48 | | - } |
49 | | - /> |
50 | | - <Route |
51 | | - path="/register" |
52 | | - render={() => |
53 | | - isLoggedIn ? ( |
54 | | - <Redirect to="/dashboard" /> |
55 | | - ) : ( |
56 | | - <Register /> |
57 | | - ) |
58 | | - } |
59 | | - /> |
60 | | - |
61 | | - <NotFound /> |
62 | | - </Switch> |
63 | | - </div> |
64 | | - ); |
65 | | - } |
66 | | -} |
| 18 | + useEffect(() => { |
| 19 | + const unlisten = history.listen(() => { |
| 20 | + error && onClearErrors(); |
| 21 | + }); |
67 | 22 |
|
68 | | -export default withRouter(withState(App)); |
| 23 | + return () => unlisten(); |
| 24 | + }, [error]); |
| 25 | + |
| 26 | + return ( |
| 27 | + <main className="App"> |
| 28 | + <Header /> |
| 29 | + <Switch> |
| 30 | + <Route exact path="/" component={Landing} /> |
| 31 | + <PrivateRoute path="/dashboard" redirectTo="/"> |
| 32 | + <Dashboard /> |
| 33 | + </PrivateRoute> |
| 34 | + <Route |
| 35 | + path="/login" |
| 36 | + render={() => |
| 37 | + isLoggedIn ? <Redirect to="/dashboard" /> : <Login /> |
| 38 | + } |
| 39 | + /> |
| 40 | + <Route |
| 41 | + path="/register" |
| 42 | + render={() => |
| 43 | + isLoggedIn ? <Redirect to="/dashboard" /> : <Register /> |
| 44 | + } |
| 45 | + /> |
| 46 | + <NotFound /> |
| 47 | + </Switch> |
| 48 | + </main> |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +export default App; |
0 commit comments