-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
75 lines (62 loc) · 1.91 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, { useEffect, Suspense } from 'react';
import { Route, Switch, withRouter, Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import * as actions from './store/actions/index';
import Layout from './hoc/Layout/Layout';
import BurgerBuilder from './containers/BurgerBuilder/BurgerBuilder';
import Logout from './containers/Auth/Logout/Logout';
const Checkout = React.lazy(() => {
return import('./containers/Checkout/Checkout');
})
const Orders = React.lazy(() => {
return import('./containers/Orders/Orders');
})
const Auth = React.lazy(() => {
return import('./containers/Auth/Auth');
})
const app = props => {
const { onTryAutoSignup } = props;
useEffect(() => {
onTryAutoSignup();
}, [onTryAutoSignup]);
let routes = (
<Switch>
<Route path="/auth" render={props => <Auth {...props} />} />
<Route path="/" exact component={BurgerBuilder} />
<Redirect to="/" />
</Switch>
);
if(props.isAuthenticated) {
routes = (
<Switch>
<Route path="/checkout" render={props => <Checkout {...props}/>} />
<Route path="/orders" render={props => <Orders {...props}/>} />
<Route path="/logout" component={Logout} />
<Route path="/auth" render={props => <Auth {...props}/>} />
<Route path="/" exact component={BurgerBuilder} />
<Redirect to="/" />
</Switch>
);
}
return (
<div>
<Layout>
<Suspense fallback={<p>Loading...</p>}>
{routes}
</Suspense>
</Layout>
</div>
);
}
const mapStateToProps = state => {
return {
isAuthenticated: state.auth.token !== null
}
}
const mapDispatchToProps = dispatch => {
return {
onTryAutoSignup: () => dispatch(actions.authCheckState())
}
}
// export default withRouter(connect(mapStateToProps, mapDispatchToProps)(App));
export default connect(mapStateToProps, mapDispatchToProps)(app);