Created
October 21, 2016 20:06
-
-
Save bnhansn/c9c52d15d7e298896190496f54168de9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @flow | |
| import React, { Component } from 'react'; | |
| import { BrowserRouter, Miss } from 'react-router'; | |
| import { connect } from 'react-redux'; | |
| import { authenticate, unauthenticate, logout } from '../../actions/session'; | |
| import Home from '../Home'; | |
| import NotFound from '../../components/NotFound'; | |
| import Login from '../Login'; | |
| import Signup from '../Signup'; | |
| import MatchAuthenticated from '../../components/MatchAuthenticated'; | |
| import RedirectAuthenticated from '../../components/RedirectAuthenticated'; | |
| import Sidebar from '../../components/Sidebar'; | |
| import Room from '../Room'; | |
| type Props = { | |
| authenticate: () => void, | |
| unauthenticate: () => void, | |
| isAuthenticated: boolean, | |
| willAuthenticate: boolean, | |
| logout: () => void, | |
| currentUserRooms: Array, | |
| } | |
| class App extends Component { | |
| componentDidMount() { | |
| const token = localStorage.getItem('token'); | |
| if (token) { | |
| this.props.authenticate(); | |
| } else { | |
| this.props.unauthenticate(); | |
| } | |
| } | |
| props: Props | |
| handleLogout = router => this.props.logout(router); | |
| render() { | |
| const { isAuthenticated, willAuthenticate, currentUserRooms } = this.props; | |
| const authProps = { isAuthenticated, willAuthenticate }; | |
| return ( | |
| <BrowserRouter> | |
| {({ router }) => ( | |
| <div style={{ display: 'flex', flex: '1' }}> | |
| {isAuthenticated && | |
| <Sidebar | |
| router={router} | |
| rooms={currentUserRooms} | |
| onLogoutClick={this.handleLogout} | |
| /> | |
| } | |
| <MatchAuthenticated exactly pattern="/" component={Home} {...authProps} /> | |
| <RedirectAuthenticated pattern="/login" component={Login} {...authProps} /> | |
| <RedirectAuthenticated pattern="/signup" component={Signup} {...authProps} /> | |
| <MatchAuthenticated pattern="/r/:id" component={Room} {...authProps} /> | |
| <Miss component={NotFound} /> | |
| </div> | |
| )} | |
| </BrowserRouter> | |
| ); | |
| } | |
| } | |
| export default connect( | |
| state => ({ | |
| isAuthenticated: state.session.isAuthenticated, | |
| willAuthenticate: state.session.willAuthenticate, | |
| currentUserRooms: state.rooms.currentUserRooms, | |
| }), | |
| { authenticate, unauthenticate, logout } | |
| )(App); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment