|
1 |
| -import React, { Component } from "react"; |
2 |
| -import { Route, Switch, Redirect } from "react-router-dom"; |
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { Route, Redirect } from "react-router-dom"; |
3 | 3 |
|
4 |
| -import { IonApp, IonRouterOutlet, IonSpinner } from "@ionic/react"; |
| 4 | +import { IonApp, IonSpinner, IonRouterOutlet } from "@ionic/react"; |
5 | 5 | import { IonReactRouter } from "@ionic/react-router";
|
6 | 6 |
|
7 |
| -import PrivateRoute from "./components/PrivateRoute"; |
8 | 7 | import HomePage from "./pages/HomePage";
|
9 | 8 | import LoginPage from "./pages/LoginPage";
|
10 | 9 | import RegistrationPage from "./pages/RegistrationPage";
|
11 |
| -import TabOneDetailPage from "./pages/TabOneDetailPage"; |
12 |
| - |
13 |
| -import { inject, observer } from "mobx-react"; |
14 |
| -class App extends Component { |
15 |
| - render() { |
16 |
| - return !this.props.store.authCheckComplete ? ( |
17 |
| - <div |
18 |
| - style={{ |
19 |
| - position: "absolute", |
20 |
| - left: "50%", |
21 |
| - top: "50%", |
22 |
| - transform: "translate(-50%, -50%)", |
23 |
| - }} |
24 |
| - > |
25 |
| - <IonSpinner name="circles" /> |
26 |
| - </div> |
27 |
| - ) : ( |
28 |
| - <IonReactRouter> |
29 |
| - <IonApp> |
30 |
| - <Switch> |
31 |
| - <Redirect exact from="/" to="home" /> |
32 |
| - <Route path="/login" component={LoginPage} /> |
33 |
| - <IonRouterOutlet> |
34 |
| - <Route path="/register" component={RegistrationPage} /> |
35 |
| - <PrivateRoute name="home" path="/home" component={HomePage} /> |
36 |
| - <PrivateRoute |
37 |
| - path="/tab1-detail/:id" |
38 |
| - component={TabOneDetailPage} |
39 |
| - /> |
40 |
| - </IonRouterOutlet> |
41 |
| - </Switch> |
42 |
| - </IonApp> |
43 |
| - </IonReactRouter> |
44 |
| - ); |
45 |
| - } |
46 |
| -} |
47 |
| - |
48 |
| -export default inject("store")(observer(App)); |
| 10 | + |
| 11 | +import { observer, MobXProviderContext } from "mobx-react"; |
| 12 | +import { autorun } from "mobx"; |
| 13 | + |
| 14 | +const PrivateRoutes = () => { |
| 15 | + return ( |
| 16 | + <IonReactRouter> |
| 17 | + <IonRouterOutlet> |
| 18 | + {/****** AUTH CREATE ACCOUNT */} |
| 19 | + <Route path="/login" component={LoginPage} exact={true} /> |
| 20 | + <Route path="/register" component={RegistrationPage} exact={true} /> |
| 21 | + <Route path="/" render={() => <Redirect to="/login" />} /> |
| 22 | + </IonRouterOutlet> |
| 23 | + </IonReactRouter> |
| 24 | + ); |
| 25 | +}; |
| 26 | +const PublicRoutes = () => { |
| 27 | + return ( |
| 28 | + <IonReactRouter> |
| 29 | + <Route path="/tabs" component={HomePage} /> |
| 30 | + <Route path="/" render={() => <Redirect to="/tabs/home" />} /> |
| 31 | + </IonReactRouter> |
| 32 | + ); |
| 33 | +}; |
| 34 | + |
| 35 | +const App = () => { |
| 36 | + const { store } = React.useContext(MobXProviderContext); |
| 37 | + const [hasUser, setHasUser] = useState(false); |
| 38 | + useEffect(() => { |
| 39 | + autorun(() => { |
| 40 | + setHasUser(store.authenticatedUser !== null); |
| 41 | + }); |
| 42 | + }, [store.authenticatedUser]); |
| 43 | + |
| 44 | + console.log(hasUser); |
| 45 | + |
| 46 | + return !store.authCheckComplete ? ( |
| 47 | + <div |
| 48 | + style={{ |
| 49 | + position: "absolute", |
| 50 | + left: "50%", |
| 51 | + top: "50%", |
| 52 | + transform: "translate(-50%, -50%)", |
| 53 | + }} |
| 54 | + > |
| 55 | + <IonSpinner name="circles" /> |
| 56 | + </div> |
| 57 | + ) : ( |
| 58 | + <IonApp>{hasUser ? <PublicRoutes /> : <PrivateRoutes />}</IonApp> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +export default observer(App); |
0 commit comments