Advertisement
Guest User

Untitled

a guest
May 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. import * as React from 'react';
  2. import { BrowserRouter, Route, Link, history } from "react-router-dom";
  3. import PrivateRoute from '../helper/PrivateRoute';
  4. import { User } from '../class/User';
  5. import { UserType } from '../enums/UserType';
  6. import axios from 'axios';
  7. import NotificationSystem from 'react-notification-system'
  8.  
  9. // Services
  10. import UserService from '../service/UserService';
  11.  
  12. // Homepage
  13. import Header from './header/header';
  14. import Footer from './footer';
  15. import Home from './home';
  16. import Register from './register';
  17. import ForgotPassword from './forgotPassword';
  18. import CoachPanel from './coach/coachPanel';
  19. import CustomerPanel from './customer/customerPanel';
  20. import AdminPanel from './admin/adminPanel';
  21. import AccountingPanel from './accounting/accountingPanel';
  22.  
  23. class Layout extends React.Component<any, any> {
  24. constructor(props) {
  25. super(props);
  26.  
  27. this.state = {
  28. user: null,
  29. notify: null
  30. }
  31. }
  32.  
  33. componentDidMount() {
  34.  
  35. // Notify system ini
  36. this.setState({ notify: this.refs.notificationSystem });
  37.  
  38. // Try to remember the user
  39. var userJson = null;
  40. var user = localStorage.getItem("user");
  41. if (user !== null && user !== "undefined") {
  42. userJson = JSON.parse(user);
  43. this.setState({ user: userJson });
  44. console.log('Found user: ', userJson);
  45.  
  46. axios.defaults.headers.common['Authorization'] = 'Bearer ' + userJson.token;
  47. }
  48. else {
  49. console.log('No user found in local storage');
  50. }
  51. }
  52.  
  53. login = async (username: string, password: string) => {
  54. let service = new UserService(this.state.notify);
  55. var user = await service.login(username, password);
  56. console.log('Login User', user);
  57. this.setState({ user: user });
  58. }
  59.  
  60. logout = () => {
  61. let service = new UserService(this.state.notify);
  62. service.logout();
  63. this.setState({ user: null });
  64. console.log("b", this.context.router);
  65. }
  66.  
  67. public render() {
  68. return (
  69. <BrowserRouter>
  70. <div>
  71. <NotificationSystem ref="notificationSystem" />
  72. <Header user={this.state.user} login={this.login.bind(this)} logout={this.logout.bind(this)} />
  73.  
  74. <Route exact path="/" component={Home} />
  75. <Route path="/register" component={Register} />
  76. <Route path="/forgotPassword" component={ForgotPassword} />
  77. <PrivateRoute path="/coachPanel" component={CoachPanel} redirectTo="/" userType={UserType.Coach} user={this.state.user} />
  78. <PrivateRoute path="/adminPanel" component={AdminPanel} redirectTo="/" userType={UserType.Admin} user={this.state.user} />
  79. <PrivateRoute path="/customerPanel" component={CustomerPanel} redirectTo="/" userType={UserType.Customer} user={this.state.user} />
  80. <PrivateRoute path="/accountingPanel" component={AccountingPanel} redirectTo="/" userType={UserType.Accounting} user={this.state.user} />
  81.  
  82. <Footer />
  83. </div>
  84. </BrowserRouter>
  85. )
  86. }
  87. }
  88.  
  89. export default Layout;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement