|
1 | | -import React from 'react'; |
| 1 | +import React, {Component} from 'react'; |
2 | 2 | import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; |
3 | 3 | import ProductShowPage from './ProductShowPage'; |
4 | 4 | import ProductIndexPage from './ProductIndexPage'; |
5 | 5 | import NewProductPage from './NewProductPage'; |
6 | 6 | import SignInPage from './SignInPage'; |
7 | 7 | import HomePage from './HomePage'; |
8 | 8 | import NavBar from './NavBar'; |
| 9 | +import { User } from '../requests' |
9 | 10 |
|
10 | | -const App = () => { |
11 | | - return ( |
12 | | - <Router> |
13 | | - <div className="App"> |
14 | | - <NavBar /> |
15 | | - <Switch> |
16 | | - <Route path="/session/new" exact component={SignInPage} /> |
17 | | - <Route path="/products/new" component={NewProductPage} /> |
18 | | - <Route path="/products/:id" component={ProductShowPage} /> |
19 | | - <Route path="/products" exact component={ProductIndexPage} /> |
20 | | - <Route path="/" exact component={HomePage} /> |
21 | | - </Switch> |
22 | | - </div> |
23 | | - </Router> |
24 | | - ); |
| 11 | +class App extends Component { |
| 12 | + constructor(props) { |
| 13 | + super(props); |
| 14 | + this.state = { |
| 15 | + currentUser: null |
| 16 | + } |
| 17 | + this.getUser = this.getUser.bind(this); |
| 18 | + } |
| 19 | + |
| 20 | + getUser() { |
| 21 | + User.current().then(currentUser => { |
| 22 | + if (currentUser.id) { |
| 23 | + this.setState({ currentUser }); |
| 24 | + } |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + componentDidMount() { |
| 29 | + this.getUser(); |
| 30 | + } |
| 31 | + |
| 32 | + render() { |
| 33 | + const { currentUser } = this.state; |
| 34 | + return ( |
| 35 | + <Router> |
| 36 | + <div className="App"> |
| 37 | + <NavBar currentUser={currentUser} /> |
| 38 | + <Switch> |
| 39 | + <Route path="/session/new" exact render={ |
| 40 | + (routeProps) => <SignInPage {...routeProps} onSignIn={this.getUser} />} |
| 41 | + /> |
| 42 | + <Route path="/products/new" component={NewProductPage} /> |
| 43 | + <Route path="/products/:id" component={ProductShowPage} /> |
| 44 | + <Route path="/products" exact component={ProductIndexPage} /> |
| 45 | + <Route path="/" exact component={HomePage} /> |
| 46 | + </Switch> |
| 47 | + </div> |
| 48 | + </Router> |
| 49 | + ); |
| 50 | + } |
25 | 51 | }; |
26 | 52 |
|
27 | 53 | export default App; |
0 commit comments