|
|
@@ -2,51 +2,47 @@ import React, { Component } from 'react';
|
|
|
import styled, { createGlobalStyle } from 'styled-components';
|
|
|
import close from '../assets/close.png';
|
|
|
|
|
|
+import api from '../shared/api';
|
|
|
import { Context } from '../shared/Context';
|
|
|
|
|
|
import Login from './Login';
|
|
|
import Register from './Register';
|
|
|
import Home from './home/Home';
|
|
|
+import { BrowserRouter, Route, Redirect, Switch } from 'react-router-dom';
|
|
|
|
|
|
type PropsType = {
|
|
|
};
|
|
|
|
|
|
type StateType = {
|
|
|
isLoading: boolean,
|
|
|
- isLoggedIn: boolean
|
|
|
- uninitialized: boolean,
|
|
|
+ isLoggedIn: boolean,
|
|
|
+ initialized: boolean,
|
|
|
};
|
|
|
|
|
|
export default class Main extends Component<PropsType, StateType> {
|
|
|
+
|
|
|
state = {
|
|
|
isLoading: false,
|
|
|
- isLoggedIn: true,
|
|
|
- uninitialized: true,
|
|
|
- };
|
|
|
+ isLoggedIn : false,
|
|
|
+ initialized: false
|
|
|
+ }
|
|
|
|
|
|
componentDidMount() {
|
|
|
- // let user = localStorage.getItem('user')
|
|
|
- // console.log(user);
|
|
|
- // // Check if Porter has already been initialized
|
|
|
- // if (user) {
|
|
|
- // this.setState({ uninitialized: true })
|
|
|
- // // Check if user is logged in
|
|
|
- // if (user) {
|
|
|
- // this.setState({ isLoggedIn: true });
|
|
|
- // }
|
|
|
- // }
|
|
|
+ localStorage.getItem("init") == 'true' ? this.setState({initialized: true}) : this.setState({initialized: false})
|
|
|
+ api.checkAuth('', {}, {}, (err: any, res: any) => {
|
|
|
+ if (res.data) {
|
|
|
+ this.setState({ isLoggedIn: true, initialized: true})
|
|
|
+ } else {
|
|
|
+ this.setState({ isLoggedIn: false })
|
|
|
+ }
|
|
|
+
|
|
|
+ // err ? setCurrentError(JSON.stringify(err)) : authenticate();
|
|
|
+ });
|
|
|
+ }
|
|
|
+ initialize = () => {
|
|
|
+ this.setState({isLoggedIn: true, initialized: true});
|
|
|
+ localStorage.setItem('init', 'true');
|
|
|
}
|
|
|
-
|
|
|
- renderContents = (): JSX.Element => {
|
|
|
- if (this.state.isLoading) {
|
|
|
- return <h1>Loading...</h1>
|
|
|
- } else if (this.state.isLoggedIn) {
|
|
|
- return <Home logOut={() => this.setState({ isLoggedIn: false })} />
|
|
|
- } else if (this.state.uninitialized) {
|
|
|
- return <Register authenticate={() => this.setState({ isLoggedIn: true })} />
|
|
|
- }
|
|
|
- return <Login authenticate={() => this.setState({ isLoggedIn: true })} />
|
|
|
- };
|
|
|
|
|
|
renderCurrentError = (): JSX.Element | undefined => {
|
|
|
if (this.context.currentError) {
|
|
|
@@ -63,11 +59,47 @@ export default class Main extends Component<PropsType, StateType> {
|
|
|
|
|
|
render() {
|
|
|
return (
|
|
|
+
|
|
|
<StyledMain>
|
|
|
<GlobalStyle />
|
|
|
- {/* {this.renderContents()} */}
|
|
|
-
|
|
|
- {this.renderCurrentError()}
|
|
|
+ <BrowserRouter>
|
|
|
+ <Switch>
|
|
|
+ <Route path='/login' render={() => {
|
|
|
+ if (this.state.isLoggedIn) {
|
|
|
+ return <Redirect to='/' />
|
|
|
+ } else {
|
|
|
+ return <Login authenticate={() => this.setState({ isLoggedIn: true, initialized: true })} />
|
|
|
+ }
|
|
|
+ }} />
|
|
|
+
|
|
|
+ <Route path='/register' render={() => {
|
|
|
+ if (this.state.initialized) {
|
|
|
+ return <Redirect to='/' />
|
|
|
+ } else {
|
|
|
+ return <Register authenticate={this.initialize} />
|
|
|
+ }
|
|
|
+ }} />
|
|
|
+
|
|
|
+ <Route path='/dashboard' render={() => {
|
|
|
+ if (!this.state.isLoggedIn) {
|
|
|
+ return <Redirect to='/' />
|
|
|
+ } else {
|
|
|
+ return <Home logOut={() => this.setState({ isLoggedIn: false, initialized: true })} />
|
|
|
+ }
|
|
|
+ }}/>
|
|
|
+
|
|
|
+ <Route path='/' render={() => {
|
|
|
+ if (this.state.isLoggedIn) {
|
|
|
+ return <Redirect to='/dashboard'/>
|
|
|
+ } else if (this.state.initialized) {
|
|
|
+ return <Redirect to='/login'/>
|
|
|
+ } else {
|
|
|
+ return <Redirect to='/register' />
|
|
|
+ }
|
|
|
+ }}/>
|
|
|
+ </Switch>
|
|
|
+ </BrowserRouter>
|
|
|
+ {this.renderCurrentError()} */}
|
|
|
</StyledMain>
|
|
|
);
|
|
|
}
|