|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import './App.css'; |
| 3 | +import { |
| 4 | + Route, |
| 5 | + withRouter, |
| 6 | + Switch |
| 7 | +} from 'react-router-dom'; |
| 8 | + |
| 9 | +import { getCurrentUser } from '../util/APIUtils'; |
| 10 | +import { ACCESS_TOKEN } from '../constants'; |
| 11 | + |
| 12 | +import PollList from '../poll/PollList'; |
| 13 | +import NewPoll from '../poll/NewPoll'; |
| 14 | +import Login from '../user/login/Login'; |
| 15 | +import Signup from '../user/signup/Signup'; |
| 16 | +import Profile from '../user/profile/Profile'; |
| 17 | +import AppHeader from '../common/AppHeader'; |
| 18 | +import NotFound from '../common/NotFound'; |
| 19 | +import LoadingIndicator from '../common/LoadingIndicator'; |
| 20 | +import PrivateRoute from '../common/PrivateRoute'; |
| 21 | + |
| 22 | +import { Layout, notification } from 'antd'; |
| 23 | +const { Content } = Layout; |
| 24 | + |
| 25 | +class App extends Component { |
| 26 | + constructor(props) { |
| 27 | + super(props); |
| 28 | + this.state = { |
| 29 | + currentUser: null, |
| 30 | + isAuthenticated: false, |
| 31 | + isLoading: false |
| 32 | + } |
| 33 | + this.handleLogout = this.handleLogout.bind(this); |
| 34 | + this.loadCurrentUser = this.loadCurrentUser.bind(this); |
| 35 | + this.handleLogin = this.handleLogin.bind(this); |
| 36 | + |
| 37 | + notification.config({ |
| 38 | + placement: 'topRight', |
| 39 | + top: 70, |
| 40 | + duration: 3, |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + loadCurrentUser() { |
| 45 | + this.setState({ |
| 46 | + isLoading: true |
| 47 | + }); |
| 48 | + getCurrentUser() |
| 49 | + .then(response => { |
| 50 | + this.setState({ |
| 51 | + currentUser: response, |
| 52 | + isAuthenticated: true, |
| 53 | + isLoading: false |
| 54 | + }); |
| 55 | + }).catch(error => { |
| 56 | + this.setState({ |
| 57 | + isLoading: false |
| 58 | + }); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + componentWillMount() { |
| 63 | + this.loadCurrentUser(); |
| 64 | + } |
| 65 | + |
| 66 | + handleLogout(redirectTo="/", notificationType="success", description="You're successfully logged out.") { |
| 67 | + localStorage.removeItem(ACCESS_TOKEN); |
| 68 | + |
| 69 | + this.setState({ |
| 70 | + currentUser: null, |
| 71 | + isAuthenticated: false |
| 72 | + }); |
| 73 | + |
| 74 | + this.props.history.push(redirectTo); |
| 75 | + |
| 76 | + notification[notificationType]({ |
| 77 | + message: 'Polling App', |
| 78 | + description: description, |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + handleLogin() { |
| 83 | + notification.success({ |
| 84 | + message: 'Polling App', |
| 85 | + description: "You're successfully logged in.", |
| 86 | + }); |
| 87 | + this.loadCurrentUser(); |
| 88 | + this.props.history.push("/"); |
| 89 | + } |
| 90 | + |
| 91 | + render() { |
| 92 | + if(this.state.isLoading) { |
| 93 | + return <LoadingIndicator /> |
| 94 | + } |
| 95 | + return ( |
| 96 | + <Layout className="app-container"> |
| 97 | + <AppHeader isAuthenticated={this.state.isAuthenticated} |
| 98 | + currentUser={this.state.currentUser} |
| 99 | + onLogout={this.handleLogout} /> |
| 100 | + |
| 101 | + <Content className="app-content"> |
| 102 | + <div className="container"> |
| 103 | + <Switch> |
| 104 | + <Route exact path="/" |
| 105 | + render={(props) => <PollList isAuthenticated={this.state.isAuthenticated} |
| 106 | + currentUser={this.state.currentUser} handleLogout={this.handleLogout} {...props} />}> |
| 107 | + </Route> |
| 108 | + <Route path="/login" |
| 109 | + render={(props) => <Login onLogin={this.handleLogin} {...props} />}></Route> |
| 110 | + <Route path="/signup" component={Signup}></Route> |
| 111 | + <Route path="/users/:username" |
| 112 | + render={(props) => <Profile isAuthenticated={this.state.isAuthenticated} currentUser={this.state.currentUser} {...props} />}> |
| 113 | + </Route> |
| 114 | + <PrivateRoute authenticated={this.state.isAuthenticated} path="/poll/new" component={NewPoll} handleLogout={this.handleLogout}></PrivateRoute> |
| 115 | + <Route component={NotFound}></Route> |
| 116 | + </Switch> |
| 117 | + </div> |
| 118 | + </Content> |
| 119 | + </Layout> |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +export default withRouter(App); |
0 commit comments